blob: 259f71efb860ed3a7a0df89d2d1743a773eb8c45 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02002 * HTTP/1 mux-demux for connections
Christopher Faulet51dbc942018-09-13 09:05:15 +02003 *
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>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020018#include <haproxy/dynbuf.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020019#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020020#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020021#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020023#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/istbuf.h>
25#include <haproxy/log.h>
Christopher Faulet18ad15f2022-09-15 10:51:26 +020026#include <haproxy/mux_h1-t.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020027#include <haproxy/pipe-t.h>
Willy Tarreauadc02402021-05-08 20:28:54 +020028#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020029#include <haproxy/session-t.h>
Christopher Fauletb4c584e2021-11-26 17:37:07 +010030#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020031#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/stream.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020033#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020034
Christopher Faulet51dbc942018-09-13 09:05:15 +020035/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020036struct h1c {
37 struct connection *conn;
38 struct proxy *px;
39 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +020040 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +020041 struct buffer ibuf; /* Input buffer to store data before parsing */
42 struct buffer obuf; /* Output buffer to store data after reformatting */
43
44 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
45 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
46
47 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010048 struct task *task; /* timeout management task */
Christopher Faulet563c3452021-11-26 17:37:51 +010049 struct h1_counters *px_counters; /* h1 counters attached to proxy */
Christopher Fauletdbe57792020-10-05 17:50:58 +020050 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
51 int timeout; /* client/server timeout duration */
52 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +020053};
54
55/* H1 stream descriptor */
56struct h1s {
57 struct h1c *h1c;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +020058 struct sedesc *sd;
Christopher Fauletfeb11742018-11-29 15:12:34 +010059 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020060
Willy Tarreau4596fe22022-05-17 19:07:51 +020061 struct wait_event *subs; /* Address of the wait_event the stream connector associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020062
Olivier Houchardf502aca2018-12-14 19:42:40 +010063 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +020064 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +020065 struct h1m req;
66 struct h1m res;
67
Ilya Shipitsin47d17182020-06-21 21:42:57 +050068 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +020069 uint16_t status; /* HTTP response status */
Amaury Denoyellec1938232020-12-11 17:53:03 +010070
71 char ws_key[25]; /* websocket handshake key */
Christopher Faulet51dbc942018-09-13 09:05:15 +020072};
73
Christopher Faulet98fbe952019-07-22 16:18:24 +020074/* Map of headers used to convert outgoing headers */
75struct h1_hdrs_map {
76 char *name;
77 struct eb_root map;
78};
79
80/* An entry in a headers map */
81struct h1_hdr_entry {
82 struct ist name;
83 struct ebpt_node node;
84};
85
86/* Declare the headers map */
87static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
Christopher Faulet0f9c0f52022-05-13 09:20:13 +020088static int accept_payload_with_any_method = 0;
Christopher Faulet98fbe952019-07-22 16:18:24 +020089
Christopher Faulet6b81df72019-10-01 22:08:43 +020090/* trace source and events */
91static void h1_trace(enum trace_level level, uint64_t mask,
92 const struct trace_source *src,
93 const struct ist where, const struct ist func,
94 const void *a1, const void *a2, const void *a3, const void *a4);
95
96/* The event representation is split like this :
97 * h1c - internal H1 connection
98 * h1s - internal H1 stream
99 * strm - application layer
100 * rx - data receipt
101 * tx - data transmission
102 *
103 */
104static const struct trace_event h1_trace_events[] = {
105#define H1_EV_H1C_NEW (1ULL << 0)
106 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
107#define H1_EV_H1C_RECV (1ULL << 1)
108 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
109#define H1_EV_H1C_SEND (1ULL << 2)
110 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
111#define H1_EV_H1C_BLK (1ULL << 3)
112 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
113#define H1_EV_H1C_WAKE (1ULL << 4)
114 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
115#define H1_EV_H1C_END (1ULL << 5)
116 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
117#define H1_EV_H1C_ERR (1ULL << 6)
118 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
119
120#define H1_EV_RX_DATA (1ULL << 7)
121 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
122#define H1_EV_RX_EOI (1ULL << 8)
123 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
124#define H1_EV_RX_HDRS (1ULL << 9)
125 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
126#define H1_EV_RX_BODY (1ULL << 10)
127 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
128#define H1_EV_RX_TLRS (1ULL << 11)
129 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
130
131#define H1_EV_TX_DATA (1ULL << 12)
132 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
133#define H1_EV_TX_EOI (1ULL << 13)
134 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
135#define H1_EV_TX_HDRS (1ULL << 14)
136 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
137#define H1_EV_TX_BODY (1ULL << 15)
138 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
139#define H1_EV_TX_TLRS (1ULL << 16)
140 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
141
142#define H1_EV_H1S_NEW (1ULL << 17)
143 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
144#define H1_EV_H1S_BLK (1ULL << 18)
145 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
146#define H1_EV_H1S_END (1ULL << 19)
147 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
148#define H1_EV_H1S_ERR (1ULL << 20)
149 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
150
151#define H1_EV_STRM_NEW (1ULL << 21)
152 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
153#define H1_EV_STRM_RECV (1ULL << 22)
154 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
155#define H1_EV_STRM_SEND (1ULL << 23)
156 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
157#define H1_EV_STRM_WAKE (1ULL << 24)
158 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
159#define H1_EV_STRM_SHUT (1ULL << 25)
160 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
161#define H1_EV_STRM_END (1ULL << 26)
162 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
163#define H1_EV_STRM_ERR (1ULL << 27)
164 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
165
166 { }
167};
168
169static const struct name_desc h1_trace_lockon_args[4] = {
170 /* arg1 */ { /* already used by the connection */ },
171 /* arg2 */ { .name="h1s", .desc="H1 stream" },
172 /* arg3 */ { },
173 /* arg4 */ { }
174};
175
176static const struct name_desc h1_trace_decoding[] = {
177#define H1_VERB_CLEAN 1
178 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
179#define H1_VERB_MINIMAL 2
180 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
181#define H1_VERB_SIMPLE 3
182 { .name="simple", .desc="add request/response status line or htx info when available" },
183#define H1_VERB_ADVANCED 4
184 { .name="advanced", .desc="add header fields or frame decoding when available" },
185#define H1_VERB_COMPLETE 5
186 { .name="complete", .desc="add full data dump when available" },
187 { /* end */ }
188};
189
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200190static struct trace_source trace_h1 __read_mostly = {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200191 .name = IST("h1"),
192 .desc = "HTTP/1 multiplexer",
193 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
194 .default_cb = h1_trace,
195 .known_events = h1_trace_events,
196 .lockon_args = h1_trace_lockon_args,
197 .decoding = h1_trace_decoding,
198 .report_events = ~0, // report everything by default
199};
200
201#define TRACE_SOURCE &trace_h1
202INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
203
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100204
205/* h1 stats module */
206enum {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100207 H1_ST_OPEN_CONN,
208 H1_ST_OPEN_STREAM,
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100209 H1_ST_TOTAL_CONN,
210 H1_ST_TOTAL_STREAM,
211
Christopher Faulet41951ab2021-11-26 18:12:51 +0100212 H1_ST_BYTES_IN,
213 H1_ST_BYTES_OUT,
214#if defined(USE_LINUX_SPLICE)
215 H1_ST_SPLICED_BYTES_IN,
216 H1_ST_SPLICED_BYTES_OUT,
217#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100218 H1_STATS_COUNT /* must be the last member of the enum */
219};
220
221
222static struct name_desc h1_stats[] = {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100223 [H1_ST_OPEN_CONN] = { .name = "h1_open_connections",
224 .desc = "Count of currently open connections" },
225 [H1_ST_OPEN_STREAM] = { .name = "h1_open_streams",
226 .desc = "Count of currently open streams" },
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100227 [H1_ST_TOTAL_CONN] = { .name = "h1_total_connections",
228 .desc = "Total number of connections" },
229 [H1_ST_TOTAL_STREAM] = { .name = "h1_total_streams",
Christopher Faulet41951ab2021-11-26 18:12:51 +0100230 .desc = "Total number of streams" },
231
232 [H1_ST_BYTES_IN] = { .name = "h1_bytes_in",
233 .desc = "Total number of bytes received" },
234 [H1_ST_BYTES_OUT] = { .name = "h1_bytes_out",
235 .desc = "Total number of bytes send" },
236#if defined(USE_LINUX_SPLICE)
237 [H1_ST_SPLICED_BYTES_IN] = { .name = "h1_spliced_bytes_in",
238 .desc = "Total number of bytes received using kernel splicing" },
239 [H1_ST_SPLICED_BYTES_OUT] = { .name = "h1_spliced_bytes_out",
240 .desc = "Total number of bytes sendusing kernel splicing" },
241#endif
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100242
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100243};
244
245static struct h1_counters {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100246 long long open_conns; /* count of currently open connections */
247 long long open_streams; /* count of currently open streams */
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100248 long long total_conns; /* total number of connections */
249 long long total_streams; /* total number of streams */
250
Christopher Faulet41951ab2021-11-26 18:12:51 +0100251 long long bytes_in; /* number of bytes received */
252 long long bytes_out; /* number of bytes sent */
253#if defined(USE_LINUX_SPLICE)
254 long long spliced_bytes_in; /* number of bytes received using kernel splicing */
255 long long spliced_bytes_out; /* number of bytes sent using kernel splicing */
256#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100257} h1_counters;
258
259static void h1_fill_stats(void *data, struct field *stats)
260{
Christopher Faulet60fa0512021-11-26 18:10:39 +0100261 struct h1_counters *counters = data;
262
263 stats[H1_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
264 stats[H1_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100265 stats[H1_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
266 stats[H1_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
Christopher Faulet41951ab2021-11-26 18:12:51 +0100267
268 stats[H1_ST_BYTES_IN] = mkf_u64(FN_COUNTER, counters->bytes_in);
269 stats[H1_ST_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->bytes_out);
270#if defined(USE_LINUX_SPLICE)
271 stats[H1_ST_SPLICED_BYTES_IN] = mkf_u64(FN_COUNTER, counters->spliced_bytes_in);
272 stats[H1_ST_SPLICED_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->spliced_bytes_out);
273#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100274}
275
276static struct stats_module h1_stats_module = {
277 .name = "h1",
278 .fill_stats = h1_fill_stats,
279 .stats = h1_stats,
280 .stats_count = H1_STATS_COUNT,
281 .counters = &h1_counters,
282 .counters_size = sizeof(h1_counters),
283 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
284 .clearable = 1,
285};
286
287INITCALL1(STG_REGISTER, stats_register_module, &h1_stats_module);
288
289
Christopher Faulet51dbc942018-09-13 09:05:15 +0200290/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100291DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
292DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200293
Christopher Faulet51dbc942018-09-13 09:05:15 +0200294static int h1_recv(struct h1c *h1c);
295static int h1_send(struct h1c *h1c);
296static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100297/* h1_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100298struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state);
299struct task *h1_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Fauleta85c5222021-10-27 15:36:38 +0200300static void h1_shutw_conn(struct connection *conn);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200301static void h1_wake_stream_for_recv(struct h1s *h1s);
302static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200303
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200304/* returns the stconn associated to the H1 stream */
305static forceinline struct stconn *h1s_sc(const struct h1s *h1s)
306{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200307 return h1s->sd->sc;
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200308}
309
Christopher Faulet6b81df72019-10-01 22:08:43 +0200310/* the H1 traces always expect that arg1, if non-null, is of type connection
311 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
312 * that arg3, if non-null, is a htx for rx/tx headers.
313 */
314static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
315 const struct ist where, const struct ist func,
316 const void *a1, const void *a2, const void *a3, const void *a4)
317{
318 const struct connection *conn = a1;
319 const struct h1c *h1c = conn ? conn->ctx : NULL;
320 const struct h1s *h1s = a2;
321 const struct htx *htx = a3;
322 const size_t *val = a4;
323
324 if (!h1c)
325 h1c = (h1s ? h1s->h1c : NULL);
326
327 if (!h1c || src->verbosity < H1_VERB_CLEAN)
328 return;
329
330 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200331 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200332
333 /* Display request and response states if h1s is defined */
Christopher Faulet6580f282021-11-26 17:31:35 +0100334 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200335 chunk_appendf(&trace_buf, " [%s, %s]",
336 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
337
Christopher Faulet6580f282021-11-26 17:31:35 +0100338 if (src->verbosity > H1_VERB_SIMPLE) {
339 chunk_appendf(&trace_buf, " - req=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
340 h1s->req.flags, (unsigned long)h1s->req.curr_len, (unsigned long)h1s->req.body_len);
341 chunk_appendf(&trace_buf, " res=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
342 h1s->res.flags, (unsigned long)h1s->res.curr_len, (unsigned long)h1s->res.body_len);
343 }
344
345 }
346
Christopher Faulet6b81df72019-10-01 22:08:43 +0200347 if (src->verbosity == H1_VERB_CLEAN)
348 return;
349
350 /* Display the value to the 4th argument (level > STATE) */
351 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100352 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200353
354 /* Display status-line if possible (verbosity > MINIMAL) */
355 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
356 const struct htx_blk *blk = htx_get_head_blk(htx);
357 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
358 enum htx_blk_type type = htx_get_blk_type(blk);
359
360 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
361 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
362 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
363 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
364 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
365 }
366
367 /* Display h1c info and, if defined, h1s info (pointer + flags) */
368 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100369 if (h1c->conn)
370 chunk_appendf(&trace_buf, " conn=%p(0x%08x)", h1c->conn, h1c->conn->flags);
371 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200372 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200373 if (h1s->sd)
374 chunk_appendf(&trace_buf, " sd=%p(0x%08x)", h1s->sd, se_fl_get(h1s->sd));
375 if (h1s->sd && h1s_sc(h1s))
Willy Tarreau000d63c2022-05-27 10:39:17 +0200376 chunk_appendf(&trace_buf, " sc=%p(0x%08x)", h1s_sc(h1s), h1s_sc(h1s)->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100377 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200378
379 if (src->verbosity == H1_VERB_MINIMAL)
380 return;
381
382 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
383 if (src->level > TRACE_LEVEL_USER) {
384 if (src->verbosity == H1_VERB_COMPLETE ||
385 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
386 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
387 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
388 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
389 if (src->verbosity == H1_VERB_COMPLETE ||
390 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
391 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
392 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
393 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
394 }
395
396 /* Display htx info if defined (level > USER) */
397 if (src->level > TRACE_LEVEL_USER && htx) {
398 int full = 0;
399
400 /* Full htx info (level > STATE && verbosity > SIMPLE) */
401 if (src->level > TRACE_LEVEL_STATE) {
402 if (src->verbosity == H1_VERB_COMPLETE)
403 full = 1;
404 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
405 full = 1;
406 }
407
408 chunk_memcat(&trace_buf, "\n\t", 2);
409 htx_dump(&trace_buf, htx, full);
410 }
411}
412
413
Christopher Faulet51dbc942018-09-13 09:05:15 +0200414/*****************************************************/
415/* functions below are for dynamic buffer management */
416/*****************************************************/
417/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100418 * Indicates whether or not we may receive data. The rules are the following :
419 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200420 * must not attempt to receive
421 * - if we are waiting for the connection establishment, we must not attempt
422 * to receive
423 * - if an error was detected on the stream we must not attempt to receive
424 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100425 * - if the input buffer failed to be allocated or is full , we must not try
426 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200427 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200428 * - otherwise must may not attempt to receive
429 */
430static inline int h1_recv_allowed(const struct h1c *h1c)
431{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100432 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100433 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 +0200434 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200435 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200436
Willy Tarreau2febb842020-07-31 09:15:43 +0200437 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
438 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 +0100439 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200440 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100441
Christopher Faulet119ac872020-09-30 17:33:22 +0200442 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
443 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
444 return 0;
445 }
446
Christopher Fauletd17ad822020-09-24 15:14:29 +0200447 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200448 return 1;
449
Christopher Faulet6b81df72019-10-01 22:08:43 +0200450 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 +0200451 return 0;
452}
453
454/*
455 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
456 * flags are used to figure what buffer was requested. It returns 1 if the
457 * allocation succeeds, in which case the connection is woken up, or 0 if it's
458 * impossible to wake up and we prefer to be woken up later.
459 */
460static int h1_buf_available(void *target)
461{
462 struct h1c *h1c = target;
463
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100464 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200465 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 +0200466 h1c->flags &= ~H1C_F_IN_ALLOC;
467 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200468 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200469 return 1;
470 }
471
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100472 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200473 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 +0200474 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200475 if (h1c->h1s)
476 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200477 return 1;
478 }
479
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100480 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200481 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
482 h1c->flags &= ~H1C_F_IN_SALLOC;
483 tasklet_wakeup(h1c->wait_event.tasklet);
484 return 1;
485 }
486
Christopher Faulet51dbc942018-09-13 09:05:15 +0200487 return 0;
488}
489
490/*
491 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
492 */
493static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
494{
495 struct buffer *buf = NULL;
496
Willy Tarreau2b718102021-04-21 07:32:39 +0200497 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100498 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200499 h1c->buf_wait.target = h1c;
500 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200501 LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200502 }
503 return buf;
504}
505
506/*
507 * Release a buffer, if any, and try to wake up entities waiting in the buffer
508 * wait queue.
509 */
510static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
511{
512 if (bptr->size) {
513 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100514 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200515 }
516}
517
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100518/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100519 * 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 +0100520 * not. This flag is only set when no H1S is attached and when the previous
521 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100522 */
523static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200524{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100525 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200526
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100527 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200528}
529
Willy Tarreau00f18a32019-01-26 12:19:01 +0100530/* returns the number of streams still available on a connection */
531static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100532{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100533 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100534}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200535
Christopher Faulet7a145d62020-08-05 11:31:16 +0200536/* Refresh the h1c task timeout if necessary */
537static void h1_refresh_timeout(struct h1c *h1c)
538{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200539 int is_idle_conn = 0;
540
Christopher Faulet7a145d62020-08-05 11:31:16 +0200541 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100542 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200543 /* half-closed or dead connections : switch to clientfin/serverfin
544 * timeouts so that we don't hang too long on clients that have
545 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200546 */
547 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200548 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200549 is_idle_conn = 1;
Christopher Fauletadcd7892020-10-05 17:13:05 +0200550 }
551 else if (b_data(&h1c->obuf)) {
552 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200553 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200554 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
555 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100556 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
557 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200558 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100559 TRACE_DEVEL("refreshing connection's timeout (alive front h1c but not ready)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200560 /* A frontend connection not yet ready could be treated the same way as an idle
561 * one in case of soft-close.
562 */
563 is_idle_conn = 1;
Christopher Faulet7a145d62020-08-05 11:31:16 +0200564 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200565 else {
Willy Tarreau4596fe22022-05-17 19:07:51 +0200566 /* alive back connections of front connections with a stream connector attached */
Christopher Fauletadcd7892020-10-05 17:13:05 +0200567 h1c->task->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200568 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with an SC)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200569 }
570
Christopher Fauletdbe57792020-10-05 17:50:58 +0200571 /* Finally set the idle expiration date if shorter */
572 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200573
574 if ((h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
575 is_idle_conn && tick_isset(global.close_spread_end)) {
576 /* If a soft-stop is in progress and a close-spread-time
577 * is set, we want to spread idle connection closing roughly
578 * evenly across the defined window. This should only
579 * act on idle frontend connections.
580 * If the window end is already in the past, we wake the
581 * timeout task up immediately so that it can be closed.
582 */
583 int remaining_window = tick_remain(now_ms, global.close_spread_end);
584 if (remaining_window) {
585 /* We don't need to reset the expire if it would
586 * already happen before the close window end.
587 */
588 if (tick_is_le(global.close_spread_end, h1c->task->expire)) {
589 /* Set an expire value shorter than the current value
590 * because the close spread window end comes earlier.
591 */
592 h1c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
593 TRACE_DEVEL("connection timeout set to value before close-spread window end", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
594 }
595 }
596 else {
597 /* We are past the soft close window end, wake the timeout
598 * task up immediately.
599 */
600 task_wakeup(h1c->task, TASK_WOKEN_TIMER);
601 }
602 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200603 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
604 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200605 }
606}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200607
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200608static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200609{
610 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
611 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
612 h1c->idle_exp = TICK_ETERNITY;
613 return;
614 }
615
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100616 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200617 if (!tick_isset(h1c->idle_exp)) {
618 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
619 !b_data(&h1c->ibuf) && /* No input data */
620 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
621 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
622 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
623 }
624 else {
625 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
626 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
627 }
628 }
629 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100630 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200631 if (!tick_isset(h1c->idle_exp)) {
632 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
633 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
634 }
635 }
Willy Tarreaue68bc612022-05-27 11:23:05 +0200636 else { // ST_ATTACHED or SHUTDOWN
Christopher Fauletdbe57792020-10-05 17:50:58 +0200637 h1c->idle_exp = TICK_ETERNITY;
638 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
639 }
640}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200641/*****************************************************************/
642/* functions below are dedicated to the mux setup and management */
643/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200644
645/* returns non-zero if there are input data pending for stream h1s. */
646static inline size_t h1s_data_pending(const struct h1s *h1s)
647{
648 const struct h1m *h1m;
649
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200650 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100651 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200652}
653
Willy Tarreau4596fe22022-05-17 19:07:51 +0200654/* Creates a new stream connector and the associate stream. <input> is used as input
Christopher Faulet16df1782020-12-04 16:47:41 +0100655 * buffer for the stream. On success, it is transferred to the stream and the
656 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
657 * mux must still take care of it. However, there is nothing special to do
658 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
Willy Tarreau4596fe22022-05-17 19:07:51 +0200659 * b_free() on it is always safe. This function returns the stream connector on
Christopher Faulet16df1782020-12-04 16:47:41 +0100660 * success or NULL on error. */
Willy Tarreau000d63c2022-05-27 10:39:17 +0200661static struct stconn *h1s_new_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100662{
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100663 struct h1c *h1c = h1s->h1c;
Christopher Faulet47365272018-10-31 17:40:50 +0100664
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100665 TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100666
Christopher Fauletb669d682022-03-22 18:37:19 +0100667 if (h1s->flags & H1S_F_NOT_FIRST)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200668 se_fl_set(h1s->sd, SE_FL_NOT_FIRST);
Christopher Fauletb669d682022-03-22 18:37:19 +0100669 if (h1s->req.flags & H1_MF_UPG_WEBSOCKET)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200670 se_fl_set(h1s->sd, SE_FL_WEBSOCKET);
Christopher Fauletb669d682022-03-22 18:37:19 +0100671
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200672 if (!sc_new_from_endp(h1s->sd, h1c->conn->owner, input)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +0200673 TRACE_ERROR("SC allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100674 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200675 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200676
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100677 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
678 TRACE_LEAVE(H1_EV_STRM_NEW, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200679 return h1s_sc(h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100680
Christopher Faulet91449b02022-03-22 18:45:55 +0100681 err:
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100682 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100683 return NULL;
684}
685
Willy Tarreau000d63c2022-05-27 10:39:17 +0200686static struct stconn *h1s_upgrade_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100687{
688 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
689
Willy Tarreaudf1a2fc2022-05-27 11:11:15 +0200690 if (stream_upgrade_from_sc(h1s_sc(h1s), input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100691 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 +0100692 goto err;
693 }
694
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100695 h1s->h1c->flags |= H1C_F_ST_READY;
696 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200697 return h1s_sc(h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100698
699 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100700 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100701 return NULL;
702}
703
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200704static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200705{
706 struct h1s *h1s;
707
Christopher Faulet6b81df72019-10-01 22:08:43 +0200708 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
709
Christopher Faulet51dbc942018-09-13 09:05:15 +0200710 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100711 if (!h1s) {
712 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 +0100713 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100714 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200715 h1s->h1c = h1c;
716 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200717 h1s->sess = NULL;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200718 h1s->sd = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100719 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100720 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200721 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100722 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200723
724 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100725 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200726
Christopher Faulet129817b2018-09-20 16:14:40 +0200727 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100728 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200729
730 h1s->status = 0;
731 h1s->meth = HTTP_METH_OTHER;
732
Christopher Faulet47365272018-10-31 17:40:50 +0100733 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
734 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100735 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100736
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200737 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
738 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200739
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200740 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100741 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200742 return NULL;
743}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100744
Willy Tarreau000d63c2022-05-27 10:39:17 +0200745static struct h1s *h1c_frt_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200746{
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200747 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
Willy Tarreau000d63c2022-05-27 10:39:17 +0200755 if (sc) {
756 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200757 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200758 h1s->sd = sc->sedesc;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100759 }
760 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200761 h1s->sd = sedesc_new();
762 if (!h1s->sd)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100763 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200764 h1s->sd->se = h1s;
765 h1s->sd->conn = h1c->conn;
766 se_fl_set(h1s->sd, SE_FL_T_MUX | SE_FL_ORPHAN);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100767 }
768
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200769 h1s->sess = sess;
770
771 if (h1c->px->options2 & PR_O2_REQBUG_OK)
772 h1s->req.err_pos = -1;
773
Christopher Fauletaf5336f2022-09-12 07:46:11 +0200774 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
775 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
776
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200777 h1c->idle_exp = TICK_ETERNITY;
778 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200779 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200780 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100781
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200782 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100783 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100784 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200785 return NULL;
786}
787
Willy Tarreau000d63c2022-05-27 10:39:17 +0200788static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200789{
790 struct h1s *h1s;
791
792 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
793
794 h1s = h1s_new(h1c);
795 if (!h1s)
796 goto fail;
797
Willy Tarreau000d63c2022-05-27 10:39:17 +0200798 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200799 goto fail;
800
Christopher Faulet10a86702021-04-07 14:18:11 +0200801 h1s->flags |= H1S_F_RX_BLK;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200802 h1s->sd = sc->sedesc;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200803 h1s->sess = sess;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200804
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100805 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200806
807 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
808 h1s->res.err_pos = -1;
809
Christopher Faulet60fa0512021-11-26 18:10:39 +0100810 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100811 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100812
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200813 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
814 return h1s;
815
816 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100817 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +0200818 pool_free(pool_head_h1s, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100819 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200820}
821
822static void h1s_destroy(struct h1s *h1s)
823{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200824 if (h1s) {
825 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200826
Christopher Faulet6b81df72019-10-01 22:08:43 +0200827 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200828 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200829
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100830 if (h1s->subs)
831 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200832
Christopher Fauletd17ad822020-09-24 15:14:29 +0200833 h1_release_buf(h1c, &h1s->rxbuf);
834
Christopher Faulet10a86702021-04-07 14:18:11 +0200835 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Fauletb385b502021-01-13 18:47:57 +0100836 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200837 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
838 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200839 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100840 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +0100841 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 +0200842 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100843
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100844 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100845 !(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 +0100846 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100847 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100848 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100849 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
850 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200851 else {
Christopher Faulet26a26432021-01-27 11:27:50 +0100852 TRACE_STATE("set shudown on h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100853 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200854 }
Christopher Faulet60fa0512021-11-26 18:10:39 +0100855
856 HA_ATOMIC_DEC(&h1c->px_counters->open_streams);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200857 BUG_ON(h1s->sd && !se_fl_test(h1s->sd, SE_FL_ORPHAN));
858 sedesc_free(h1s->sd);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200859 pool_free(pool_head_h1s, h1s);
860 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200861}
862
863/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200864 * Initialize the mux once it's attached. It is expected that conn->ctx points
Willy Tarreau4596fe22022-05-17 19:07:51 +0200865 * to the existing stream connector (for outgoing connections or for incoming
866 * ones during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200867 * establishment). <input> is always used as Input buffer and may contain
868 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
869 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200870 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200871static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
872 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200873{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200874 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100875 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200876 void *conn_ctx = conn->ctx;
877
878 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200879
880 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100881 if (!h1c) {
882 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200883 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100884 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200885 h1c->conn = conn;
886 h1c->px = proxy;
887
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100888 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200889 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200890 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200891 h1c->obuf = BUF_NULL;
892 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200893 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200894
Willy Tarreau90f366b2021-02-20 11:49:49 +0100895 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200896 h1c->wait_event.tasklet = tasklet_new();
897 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200898 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200899 h1c->wait_event.tasklet->process = h1_io_cb;
900 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100901 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200902 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200903
Christopher Faulete9b70722019-04-08 10:46:02 +0200904 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200905 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100906 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
907 if (tick_isset(proxy->timeout.serverfin))
908 h1c->shut_timeout = proxy->timeout.serverfin;
Christopher Faulet563c3452021-11-26 17:37:51 +0100909
910 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be,
911 &h1_stats_module);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100912 } else {
913 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
914 if (tick_isset(proxy->timeout.clientfin))
915 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200916
Christopher Faulet563c3452021-11-26 17:37:51 +0100917 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe,
918 &h1_stats_module);
919
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200920 LIST_APPEND(&mux_stopping_data[tid].list,
921 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100922 }
923 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200924 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100925 if (!t) {
926 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 +0100927 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100928 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100929
930 h1c->task = t;
931 t->process = h1_timeout_task;
932 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200933
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100934 t->expire = tick_add(now_ms, h1c->timeout);
935 }
936
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100937 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200938
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200939 if (h1c->flags & H1C_F_IS_BACK) {
940 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200941 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
942 goto fail;
943 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100944 else if (conn_ctx) {
945 /* Upgraded frontend connection (from TCP) */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100946 if (!h1c_frt_stream_new(h1c, conn_ctx, h1c->conn->owner))
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100947 goto fail;
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100948
Willy Tarreaue68bc612022-05-27 11:23:05 +0200949 /* Attach the SC but Not ready yet */
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100950 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200951 TRACE_DEVEL("Inherit the SC from TCP connection to perform an upgrade",
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100952 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
953 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100954
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200955 if (t) {
956 h1_set_idle_expiration(h1c);
957 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100958 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200959 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100960
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200961 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100962 if (b_data(&h1c->ibuf))
963 tasklet_wakeup(h1c->wait_event.tasklet);
964 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200965 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200966
Christopher Faulet60fa0512021-11-26 18:10:39 +0100967 HA_ATOMIC_INC(&h1c->px_counters->open_conns);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100968 HA_ATOMIC_INC(&h1c->px_counters->total_conns);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100969
Christopher Faulet51dbc942018-09-13 09:05:15 +0200970 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200971 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200972 return 0;
973
974 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200975 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200976 if (h1c->wait_event.tasklet)
977 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200978 pool_free(pool_head_h1c, h1c);
979 fail_h1c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +0100980 if (!conn_is_back(conn))
981 LIST_DEL_INIT(&conn->stopping_list);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200982 conn->ctx = conn_ctx; // restore saved context
983 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200984 return -1;
985}
986
Christopher Faulet73c12072019-04-08 11:23:22 +0200987/* release function. This one should be called to free all resources allocated
988 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200989 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200990static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200991{
Christopher Faulet61840e72019-04-15 09:33:32 +0200992 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200993
Christopher Faulet6b81df72019-10-01 22:08:43 +0200994 TRACE_POINT(H1_EV_H1C_END);
995
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200996 /* The connection must be aattached to this mux to be released */
997 if (h1c->conn && h1c->conn->ctx == h1c)
998 conn = h1c->conn;
Christopher Faulet61840e72019-04-15 09:33:32 +0200999
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001000 if (conn && h1c->flags & H1C_F_UPG_H2C) {
1001 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
1002 /* Make sure we're no longer subscribed to anything */
1003 if (h1c->wait_event.events)
1004 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1005 h1c->wait_event.events, &h1c->wait_event);
1006 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
1007 /* connection successfully upgraded to H2, this
1008 * mux was already released */
1009 return;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001010 }
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001011 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
1012 sess_log(conn->owner); /* Log if the upgrade failed */
1013 }
Olivier Houchard45c44372019-06-11 14:06:23 +02001014
Christopher Faulet51dbc942018-09-13 09:05:15 +02001015
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001016 if (LIST_INLIST(&h1c->buf_wait.list))
1017 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001018
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001019 h1_release_buf(h1c, &h1c->ibuf);
1020 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001021
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001022 if (h1c->task) {
1023 h1c->task->context = NULL;
1024 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
1025 h1c->task = NULL;
1026 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001027
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001028 if (h1c->wait_event.tasklet)
1029 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet60fa0512021-11-26 18:10:39 +01001030
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001031 h1s_destroy(h1c->h1s);
1032 if (conn) {
1033 if (h1c->wait_event.events != 0)
1034 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
1035 &h1c->wait_event);
1036 h1_shutw_conn(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001037 }
1038
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001039 HA_ATOMIC_DEC(&h1c->px_counters->open_conns);
1040 pool_free(pool_head_h1c, h1c);
1041
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001042 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001043 if (!conn_is_back(conn))
1044 LIST_DEL_INIT(&conn->stopping_list);
1045
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001046 conn->mux = NULL;
1047 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001048 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001049
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001050 conn_stop_tracking(conn);
1051 conn_full_close(conn);
1052 if (conn->destroy_cb)
1053 conn->destroy_cb(conn);
1054 conn_free(conn);
1055 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001056}
1057
1058/******************************************************/
1059/* functions below are for the H1 protocol processing */
1060/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +02001061/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
1062 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +02001063 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001064static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001065{
Christopher Faulet570d1612018-11-26 11:13:57 +01001066 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001067
Christopher Faulet570d1612018-11-26 11:13:57 +01001068 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001069 (*(p + 5) > '1' ||
1070 (*(p + 5) == '1' && *(p + 7) >= '1')))
1071 h1m->flags |= H1_MF_VER_11;
1072}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001073
Christopher Faulet9768c262018-10-22 09:34:31 +02001074/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
1075 * greater or equal to 1.1
1076 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001077static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +02001078{
Christopher Faulet570d1612018-11-26 11:13:57 +01001079 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001080
Christopher Faulet570d1612018-11-26 11:13:57 +01001081 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001082 (*(p + 5) > '1' ||
1083 (*(p + 5) == '1' && *(p + 7) >= '1')))
1084 h1m->flags |= H1_MF_VER_11;
1085}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001086
Christopher Fauletf2824e62018-10-01 12:12:37 +02001087/* Deduce the connection mode of the client connection, depending on the
1088 * configuration and the H1 message flags. This function is called twice, the
1089 * first time when the request is parsed and the second time when the response
1090 * is parsed.
1091 */
1092static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
1093{
1094 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001095
1096 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001097 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001098 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001099 h1s->status == 101) {
1100 /* Either we've established an explicit tunnel, or we're
1101 * switching the protocol. In both cases, we're very unlikely to
1102 * understand the next protocols. We have to switch to tunnel
1103 * mode, so that we transfer the request and responses then let
1104 * this protocol pass unmodified. When we later implement
1105 * specific parsers for such protocols, we'll want to check the
1106 * Upgrade header which contains information about that protocol
1107 * for responses with status 101 (eg: see RFC2817 about TLS).
1108 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001109 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001110 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 +01001111 }
1112 else if (h1s->flags & H1S_F_WANT_KAL) {
1113 /* By default the client is in KAL mode. CLOSE mode mean
1114 * it is imposed by the client itself. So only change
1115 * KAL mode here. */
1116 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1117 /* no length known or explicit close => close */
1118 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001119 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 +01001120 }
1121 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1122 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001123 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001124 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001125 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 +01001126 }
1127 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001128 }
1129 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001130 /* Input direction: first pass */
1131 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1132 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001133 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001134 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 +01001135 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001136 }
1137
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001138 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001139 * unless a 'close-spread-time' option is set (either to define a
1140 * soft-close window or to disable active closing (close-spread-time
1141 * option set to 0).
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001142 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001143 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001144 int want_clo = 1;
1145 /* If a close-spread-time option is set, we want to avoid
1146 * closing all the active HTTP connections at once so we add a
1147 * random factor that will spread the closing.
1148 */
1149 if (tick_isset(global.close_spread_end)) {
1150 int remaining_window = tick_remain(now_ms, global.close_spread_end);
1151 if (remaining_window) {
1152 /* This should increase the closing rate the further along
1153 * the window we are.
1154 */
1155 want_clo = (remaining_window <= statistical_prng_range(global.close_spread_time));
1156 }
1157 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001158 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
1159 want_clo = 0;
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001160
1161 if (want_clo) {
1162 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1163 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);
1164 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001165 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001166}
1167
1168/* Deduce the connection mode of the client connection, depending on the
1169 * configuration and the H1 message flags. This function is called twice, the
1170 * first time when the request is parsed and the second time when the response
1171 * is parsed.
1172 */
1173static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1174{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001175 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001176 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001177 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001178
Christopher Fauletf2824e62018-10-01 12:12:37 +02001179 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001180 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001181 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001182 h1s->status == 101) {
1183 /* Either we've established an explicit tunnel, or we're
1184 * switching the protocol. In both cases, we're very unlikely to
1185 * understand the next protocols. We have to switch to tunnel
1186 * mode, so that we transfer the request and responses then let
1187 * this protocol pass unmodified. When we later implement
1188 * specific parsers for such protocols, we'll want to check the
1189 * Upgrade header which contains information about that protocol
1190 * for responses with status 101 (eg: see RFC2817 about TLS).
1191 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001192 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001193 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 +01001194 }
1195 else if (h1s->flags & H1S_F_WANT_KAL) {
1196 /* By default the server is in KAL mode. CLOSE mode mean
1197 * it is imposed by haproxy itself. So only change KAL
1198 * mode here. */
1199 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1200 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1201 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1202 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001203 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 +01001204 }
1205 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001206 }
Christopher Faulet70033782018-12-05 13:50:11 +01001207 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001208 /* Output direction: first pass */
1209 if (h1m->flags & H1_MF_CONN_CLO) {
1210 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001211 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001212 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 +01001213 }
1214 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1215 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1216 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1217 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1218 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1219 /* no explicit keep-alive option httpclose/server-close => close */
1220 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001221 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 +01001222 }
Christopher Faulet70033782018-12-05 13:50:11 +01001223 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001224
1225 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001226 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001227 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001228 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);
1229 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001230}
1231
Christopher Fauletb992af02019-03-28 15:42:24 +01001232static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001233{
1234 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001235
1236 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1237 * token is found
1238 */
1239 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001241
1242 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001243 if (!(h1m->flags & H1_MF_VER_11)) {
1244 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 +01001245 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001246 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001247 }
1248 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001249 if (h1m->flags & H1_MF_VER_11) {
1250 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001251 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001252 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001253 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001254}
1255
Christopher Fauletb992af02019-03-28 15:42:24 +01001256static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001257{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001258 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1259 * token is found
1260 */
1261 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001262 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001263
1264 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001265 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001266 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1267 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 +01001268 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001269 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001270 }
1271 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001272 if (h1m->flags & H1_MF_VER_11) {
1273 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001274 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001275 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001276 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001277}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001278
Christopher Fauletb992af02019-03-28 15:42:24 +01001279static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001280{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001281 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001282 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001283 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001284 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001285}
1286
Christopher Fauletb992af02019-03-28 15:42:24 +01001287static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1288{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001289 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001290 h1_set_cli_conn_mode(h1s, h1m);
1291 else
1292 h1_set_srv_conn_mode(h1s, h1m);
1293
1294 if (!(h1m->flags & H1_MF_RESP))
1295 h1_update_req_conn_value(h1s, h1m, conn_val);
1296 else
1297 h1_update_res_conn_value(h1s, h1m, conn_val);
1298}
Christopher Faulete44769b2018-11-29 23:01:45 +01001299
Christopher Faulet98fbe952019-07-22 16:18:24 +02001300/* Try to adjust the case of the message header name using the global map
1301 * <hdrs_map>.
1302 */
1303static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1304{
1305 struct ebpt_node *node;
1306 struct h1_hdr_entry *entry;
1307
1308 /* No entry in the map, do nothing */
1309 if (eb_is_empty(&hdrs_map.map))
1310 return;
1311
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001312 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001313 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1314 return;
1315
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001316 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001317 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1318 return;
1319
1320 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1321 if (!node)
1322 return;
1323 entry = container_of(node, struct h1_hdr_entry, node);
1324 name->ptr = entry->name.ptr;
1325 name->len = entry->name.len;
1326}
1327
Christopher Faulete44769b2018-11-29 23:01:45 +01001328/* Append the description of what is present in error snapshot <es> into <out>.
1329 * The description must be small enough to always fit in a buffer. The output
1330 * buffer may be the trash so the trash must not be used inside this function.
1331 */
1332static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1333{
1334 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001335 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1336 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1337 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1338 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1339 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1340 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001341}
1342/*
1343 * Capture a bad request or response and archive it in the proxy's structure.
1344 * By default it tries to report the error position as h1m->err_pos. However if
1345 * this one is not set, it will then report h1m->next, which is the last known
1346 * parsing point. The function is able to deal with wrapping buffers. It always
1347 * displays buffers as a contiguous area starting at buf->p. The direction is
1348 * determined thanks to the h1m's flags.
1349 */
1350static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1351 struct h1m *h1m, struct buffer *buf)
1352{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001353 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001354 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001355 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001356 union error_snapshot_ctx ctx;
1357
Willy Tarreauea27f482022-05-18 16:10:52 +02001358 if ((h1c->flags & H1C_F_ST_ATTACHED) && sc_strm(h1s_sc(h1s))) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001359 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02001360 sess = __sc_strm(h1s_sc(h1s))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001361 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02001362 other_end = __sc_strm(h1s_sc(h1s))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001363 else
1364 other_end = sess->fe;
1365 } else
1366 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001367
1368 /* http-specific part now */
1369 ctx.h1.state = h1m->state;
1370 ctx.h1.c_flags = h1c->flags;
1371 ctx.h1.s_flags = h1s->flags;
1372 ctx.h1.m_flags = h1m->flags;
1373 ctx.h1.m_clen = h1m->curr_len;
1374 ctx.h1.m_blen = h1m->body_len;
1375
1376 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1377 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001378 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1379 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001380}
1381
Christopher Fauletadb22202018-12-12 10:32:09 +01001382/* Emit the chunksize followed by a CRLF in front of data of the buffer
1383 * <buf>. It goes backwards and starts with the byte before the buffer's
1384 * head. The caller is responsible for ensuring there is enough room left before
1385 * the buffer's head for the string.
1386 */
1387static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1388{
1389 char *beg, *end;
1390
1391 beg = end = b_head(buf);
1392 *--beg = '\n';
1393 *--beg = '\r';
1394 do {
1395 *--beg = hextab[chksz & 0xF];
1396 } while (chksz >>= 4);
1397 buf->head -= (end - beg);
1398 b_add(buf, end - beg);
1399}
1400
1401/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1402 * ensuring there is enough room left in the buffer for the string. */
1403static void h1_emit_chunk_crlf(struct buffer *buf)
1404{
1405 *(b_peek(buf, b_data(buf))) = '\r';
1406 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1407 b_add(buf, 2);
1408}
1409
Christopher Faulet129817b2018-09-20 16:14:40 +02001410/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001411 * Switch the stream to tunnel mode. This function must only be called on 2xx
1412 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001413 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001414static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001415{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001416 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001417
Christopher Faulet5be651d2021-01-22 15:28:03 +01001418 h1s->req.state = H1_MSG_TUNNEL;
1419 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001420
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001421 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001422 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001423
Christopher Faulet5be651d2021-01-22 15:28:03 +01001424 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 +01001425
Christopher Faulet10a86702021-04-07 14:18:11 +02001426 if (h1s->flags & H1S_F_RX_BLK) {
1427 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001428 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001429 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 +02001430 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001431 if (h1s->flags & H1S_F_TX_BLK) {
1432 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001433 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001434 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 +01001435 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001436}
1437
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001438/* Search for a websocket key header. The message should have been identified
1439 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001440 *
1441 * On the request side, if found the key is stored in the session. It might be
1442 * needed to calculate response key if the server side is using http/2.
1443 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001444 * On the response side, the key might be verified if haproxy has been
1445 * responsible for the generation of a key. This happens when a h2 client is
1446 * interfaced with a h1 server.
1447 *
1448 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001449 */
1450static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1451{
1452 struct htx_blk *blk;
1453 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001454 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001455 int ws_key_found = 0, idx;
1456
1457 idx = htx_get_head(htx); // returns the SL that we skip
1458 while ((idx = htx_get_next(htx, idx)) != -1) {
1459 blk = htx_get_blk(htx, idx);
1460 type = htx_get_blk_type(blk);
1461
1462 if (type == HTX_BLK_UNUSED)
1463 continue;
1464
1465 if (type != HTX_BLK_HDR)
1466 break;
1467
1468 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001469 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001470
Amaury Denoyellec1938232020-12-11 17:53:03 +01001471 /* Websocket key is base64 encoded of 16 bytes */
1472 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001473 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001474 /* Copy the key on request side
1475 * we might need it if the server is using h2 and does
1476 * not provide the response
1477 */
1478 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001479 ws_key_found = 1;
1480 break;
1481 }
1482 else if (isteqi(n, ist("sec-websocket-accept")) &&
1483 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001484 /* Need to verify the response key if the input was
1485 * generated by haproxy
1486 */
1487 if (h1s->ws_key[0]) {
1488 char key[29];
1489 h1_calculate_ws_output_key(h1s->ws_key, key);
1490 if (!isteqi(ist(key), v))
1491 break;
1492 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001493 ws_key_found = 1;
1494 break;
1495 }
1496 }
1497
1498 /* missing websocket key, reject the message */
1499 if (!ws_key_found) {
1500 htx->flags |= HTX_FL_PARSING_ERROR;
1501 return 0;
1502 }
1503
1504 return 1;
1505}
1506
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001507/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001508 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001509 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001510 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1511 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001512 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001513static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1514 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001515{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001516 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001517 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001518
Willy Tarreau022e5e52020-09-10 09:33:15 +02001519 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 +02001520
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001521 if (h1s->meth == HTTP_METH_CONNECT)
1522 h1m->flags |= H1_MF_METH_CONNECT;
1523 if (h1s->meth == HTTP_METH_HEAD)
1524 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001525
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001526 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001527 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001528 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 +02001529 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001530 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001531 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 +02001532 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1533 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001534 else if (ret == -2) {
1535 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);
1536 h1s->flags |= H1S_F_RX_CONGESTED;
1537 }
1538 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001539 goto end;
1540 }
1541
Christopher Faulete136bd12021-09-21 18:44:55 +02001542
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001543 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload except if
1544 * accept_payload_with_any_method global option is set.
1545 *There is a payload if the c-l is not null or the the payload is
1546 * chunk-encoded. A parsing error is reported but a A
1547 * 413-Payload-Too-Large is returned instead of a 400-Bad-Request.
Christopher Faulete136bd12021-09-21 18:44:55 +02001548 */
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001549 if (!accept_payload_with_any_method &&
1550 !(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
Christopher Faulete136bd12021-09-21 18:44:55 +02001551 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1552 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1553 h1s->flags |= H1S_F_PARSING_ERROR;
1554 htx->flags |= HTX_FL_PARSING_ERROR;
1555 h1s->h1c->errcode = 413;
1556 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);
1557 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1558 ret = 0;
1559 goto end;
1560 }
1561
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001562 /* Reject any message with an unknown transfer-encoding. In fact if any
1563 * encoding other than "chunked". A 422-Unprocessable-Content is
1564 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1565 * response.
1566 */
1567 if (h1m->flags & H1_MF_TE_OTHER) {
1568 h1s->flags |= H1S_F_PARSING_ERROR;
1569 htx->flags |= HTX_FL_PARSING_ERROR;
1570 if (!(h1m->flags & H1_MF_RESP))
1571 h1s->h1c->errcode = 422;
1572 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1573 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1574 ret = 0;
1575 goto end;
1576 }
1577
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001578 /* If websocket handshake, search for the websocket key */
1579 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1580 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1581 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1582 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001583 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001584 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 +01001585 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1586
1587 ret = 0;
1588 goto end;
1589 }
1590 }
1591
Christopher Faulet486498c2019-10-11 14:22:00 +02001592 if (h1m->err_pos >= 0) {
1593 /* Maybe we found an error during the parsing while we were
1594 * configured not to block on that, so we have to capture it
1595 * now.
1596 */
1597 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1598 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1599 }
1600
Christopher Faulet5696f542020-12-02 16:08:38 +01001601 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001602 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001603 if (h1s->meth == HTTP_METH_HEAD)
1604 h1s->flags |= H1S_F_BODYLESS_RESP;
1605 }
1606 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001607 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001608 if (h1s->status == 204 || h1s->status == 304)
1609 h1s->flags |= H1S_F_BODYLESS_RESP;
1610 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001611 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001612 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001613
Christopher Faulet129817b2018-09-20 16:14:40 +02001614 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001615 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 +02001616 return ret;
1617}
1618
1619/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001620 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001621 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1622 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001623 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001624static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1625 struct buffer *buf, size_t *ofs, size_t max,
1626 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001627{
Christopher Fauletde471a42021-02-01 16:37:28 +01001628 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001629
Willy Tarreau022e5e52020-09-10 09:33:15 +02001630 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 +02001631 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001632 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001633 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 +01001634 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001635 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001636 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 +02001637 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001638 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001639 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001640 }
1641
Christopher Faulet02a02532019-11-15 09:36:28 +01001642 *ofs += ret;
1643
1644 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001645 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1646 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);
1647 h1s->flags |= H1S_F_RX_CONGESTED;
1648 }
1649
Willy Tarreau022e5e52020-09-10 09:33:15 +02001650 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 +02001651 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001652}
1653
1654/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001655 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1656 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1657 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001658 * responsible to update the parser state <h1m>. If more room is requested,
1659 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001660 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001661static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1662 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001663{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001664 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001665
Willy Tarreau022e5e52020-09-10 09:33:15 +02001666 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 +02001667 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001668 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001669 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 +02001670 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001671 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001672 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 +02001673 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1674 }
Christopher Fauletae660be2022-04-13 17:48:54 +02001675 else if (ret == -2) {
Christopher Faulet46e058d2021-09-20 07:47:27 +02001676 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);
1677 h1s->flags |= H1S_F_RX_CONGESTED;
1678 }
1679 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001680 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001681 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001682
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001683 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001684
1685 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001686 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 +02001687 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001688}
1689
1690/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001691 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001692 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1693 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001694 *
1695 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001696 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001697static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001698{
Christopher Faulet539e0292018-11-19 10:40:09 +01001699 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001700 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001701 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001702 size_t data;
1703 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001704 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001705
Christopher Faulet539e0292018-11-19 10:40:09 +01001706 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001707 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001708
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001709 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001710 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001711
Christopher Faulet2eed8002020-12-07 11:26:13 +01001712 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001713 goto end;
1714
Christopher Faulet10a86702021-04-07 14:18:11 +02001715 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001716 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001717
Christopher Faulet46e058d2021-09-20 07:47:27 +02001718 /* Always remove congestion flags and try to process more input data */
1719 h1s->flags &= ~H1S_F_RX_CONGESTED;
1720
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001721 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001722 size_t used = htx_used_space(htx);
1723
Christopher Faulet129817b2018-09-20 16:14:40 +02001724 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001725 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001726 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001727 if (!ret)
1728 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001729
1730 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1731 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1732
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001733 if ((h1m->flags & H1_MF_RESP) &&
1734 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1735 h1m_init_res(&h1s->res);
1736 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001737 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001738 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001739 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001740 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001741 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001742 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001743 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001744 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001745
1746 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1747 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001748 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001749 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001750 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001751 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001752 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001753 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001754
Christopher Faulet76014fd2019-12-10 11:47:22 +01001755 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1756 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001757 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001758 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001759 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1760 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001761
Christopher Faulet5be651d2021-01-22 15:28:03 +01001762 if ((h1m->flags & H1_MF_RESP) &&
1763 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1764 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001765 else {
1766 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1767 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001768 h1s->flags |= H1S_F_RX_BLK;
1769 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001770 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001771 if (h1s->flags & H1S_F_TX_BLK) {
1772 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001773 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001774 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 +01001775 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001776 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001777 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001778 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001779 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001780 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001781 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001782 if (!ret)
1783 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001784
1785 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1786 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001787 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001788 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001789 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001790 break;
1791 }
1792
Christopher Faulet30db3d72019-05-17 15:35:33 +02001793 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001794 } 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 +01001795
Christopher Faulet129817b2018-09-20 16:14:40 +02001796
Christopher Faulet2eed8002020-12-07 11:26:13 +01001797 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001798 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 +02001799 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001800 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001801
Christopher Faulet539e0292018-11-19 10:40:09 +01001802 b_del(&h1c->ibuf, total);
1803
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001804 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1805
Christopher Faulet30db3d72019-05-17 15:35:33 +02001806 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001807 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001808 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001809 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 +01001810 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001811 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001812
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001813 if (!b_data(&h1c->ibuf))
1814 h1_release_buf(h1c, &h1c->ibuf);
1815
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001816 if (!(h1c->flags & H1C_F_ST_READY)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001817 /* The H1 connection is not ready. Most of time, there is no SC
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001818 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1819 * cases, it is only possible on the client side.
1820 */
1821 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1822
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001823 if (h1m->state <= H1_MSG_LAST_LF) {
1824 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1825 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1826 goto end;
1827 }
1828
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001829 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001830 TRACE_DEVEL("request headers fully parsed, create and attach the SC", H1_EV_RX_DATA, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001831 BUG_ON(h1s_sc(h1s));
Willy Tarreau000d63c2022-05-27 10:39:17 +02001832 if (!h1s_new_sc(h1s, buf)) {
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001833 h1c->flags |= H1C_F_ST_ERROR;
1834 goto err;
1835 }
1836 }
1837 else {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001838 TRACE_DEVEL("request headers fully parsed, upgrade the inherited SC", H1_EV_RX_DATA, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001839 BUG_ON(h1s_sc(h1s) == NULL);
Willy Tarreau000d63c2022-05-27 10:39:17 +02001840 if (!h1s_upgrade_sc(h1s, buf)) {
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001841 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001842 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001843 goto err;
1844 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001845 }
1846 }
1847
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001848 /* Here h1s_sc(h1s) is always defined */
Christopher Fauletf5ce3202021-11-26 17:26:19 +01001849 if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) {
Christopher Fauleta583af62020-09-24 15:35:37 +02001850 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001851 se_fl_set(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001852 }
1853 else {
1854 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001855 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001856 }
1857
Willy Tarreau4596fe22022-05-17 19:07:51 +02001858 /* Set EOI on stream connector in DONE state iff:
Christopher Fauleta22782b2021-02-08 17:18:01 +01001859 * - it is a response
1860 * - it is a request but no a protocol upgrade nor a CONNECT
1861 *
1862 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001863 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001864 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001865 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1866 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001867 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauleta583af62020-09-24 15:35:37 +02001868
Christopher Fauletec4207c2021-04-08 18:34:30 +02001869 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001870 /* When Input data are pending for this message, notify upper layer that
1871 * the mux need more space in the HTX buffer to continue if :
1872 *
1873 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1874 * - Headers or trailers are pending to be copied.
1875 */
1876 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001877 se_fl_set(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001878 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1879 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001880 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001881 se_fl_clr(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001882 if (h1s->flags & H1S_F_REOS) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001883 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Faulet1e857782020-12-08 10:38:22 +01001884 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1885 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
Willy Tarreau4596fe22022-05-17 19:07:51 +02001886 * EOI on the stream connector */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001887 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001888 }
Christopher Faulet26a26432021-01-27 11:27:50 +01001889 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001890 se_fl_set(h1s->sd, SE_FL_ERROR);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001891 TRACE_ERROR("message aborted, set error on SC", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +01001892 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001893
Christopher Faulet10a86702021-04-07 14:18:11 +02001894 if (h1s->flags & H1S_F_TX_BLK) {
1895 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001896 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001897 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 +01001898 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001899 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001900 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001901
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001902 end:
Christopher Faulet5966e402022-07-08 09:02:59 +02001903 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001904 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001905 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001906
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001907 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001908 htx_to_buf(htx, buf);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001909 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001910 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001911 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001912}
1913
Christopher Faulet129817b2018-09-20 16:14:40 +02001914/*
1915 * Process outgoing data. It parses data and transfer them from the channel buffer into
1916 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1917 * 0 if it couldn't proceed.
1918 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001919static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001920{
Christopher Faulet129817b2018-09-20 16:14:40 +02001921 struct h1s *h1s = h1c->h1s;
1922 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001923 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001924 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001925 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001926 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001927 int last_data = 0;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001928 int ws_key_found = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001929
Christopher Faulet94b2c762019-05-24 15:28:57 +02001930 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001931 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1932
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001933 if (htx_is_empty(chn_htx))
1934 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001935
Christopher Faulet10a86702021-04-07 14:18:11 +02001936 if (h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01001937 goto end;
1938
Christopher Faulet51dbc942018-09-13 09:05:15 +02001939 if (!h1_get_buf(h1c, &h1c->obuf)) {
1940 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001941 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 +02001942 goto end;
1943 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001944
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001945 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001946
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001947 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001948 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001949
Willy Tarreau3815b222018-12-11 19:50:43 +01001950 /* Perform some optimizations to reduce the number of buffer copies.
1951 * First, if the mux's buffer is empty and the htx area contains
1952 * exactly one data block of the same size as the requested count,
1953 * then it's possible to simply swap the caller's buffer with the
1954 * mux's output buffer and adjust offsets and length to match the
1955 * entire DATA HTX block in the middle. In this case we perform a
1956 * true zero-copy operation from end-to-end. This is the situation
1957 * that happens all the time with large files. Second, if this is not
1958 * possible, but the mux's output buffer is empty, we still have an
1959 * opportunity to avoid the copy to the intermediary buffer, by making
1960 * the intermediary buffer's area point to the output buffer's area.
1961 * In this case we want to skip the HTX header to make sure that copies
1962 * remain aligned and that this operation remains possible all the
1963 * time. This goes for headers, data blocks and any data extracted from
1964 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001965 */
1966 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001967 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01001968 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01001969 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001970 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001971 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001972 void *old_area;
1973
Christopher Faulet6b81df72019-10-01 22:08:43 +02001974 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 +01001975 if (h1m->state == H1_MSG_DATA) {
1976 if (h1m->flags & H1_MF_CLEN) {
1977 if (count > h1m->curr_len) {
1978 TRACE_ERROR("too much payload, more than announced",
1979 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1980 goto error;
1981 }
1982 h1m->curr_len -= count;
Christopher Faulet2eb52432022-04-07 10:29:38 +02001983 if (!h1m->curr_len)
1984 last_data = 1;
Christopher Faulet140f1a52021-11-26 16:37:55 +01001985 }
1986 if (chn_htx->flags & HTX_FL_EOM) {
1987 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1988 last_data = 1;
1989 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001990 }
1991
Christopher Faulete5596bf2020-12-02 16:13:22 +01001992 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01001993 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001994 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001995 h1c->obuf.data = count;
1996
1997 buf->area = old_area;
1998 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001999
Christopher Faulet6b81df72019-10-01 22:08:43 +02002000 chn_htx = (struct htx *)buf->area;
2001 htx_reset(chn_htx);
2002
Christopher Fauletadb22202018-12-12 10:32:09 +01002003 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002004 * size and eventually the last chunk. We have at least
2005 * the size of the struct htx to write the chunk
2006 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01002007 */
2008 if (h1m->flags & H1_MF_CHNK) {
2009 h1_emit_chunk_size(&h1c->obuf, count);
2010 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002011 if (last_data) {
2012 /* Emit the last chunk too at the buffer's end */
2013 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
2014 }
Christopher Fauletadb22202018-12-12 10:32:09 +01002015 }
2016
Christopher Faulet6b81df72019-10-01 22:08:43 +02002017 if (h1m->state == H1_MSG_DATA)
2018 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002019 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002020 else
2021 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002022 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002023
Christopher Faulete5596bf2020-12-02 16:13:22 +01002024 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002025 if (last_data) {
2026 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02002027 if (h1s->flags & H1S_F_RX_BLK) {
2028 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002029 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002030 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 +01002031 }
2032
2033 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2034 H1_EV_TX_DATA, h1c->conn, h1s);
2035 }
Willy Tarreau3815b222018-12-11 19:50:43 +01002036 goto out;
2037 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002038 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002039 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002040 else
2041 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02002042
Christopher Fauletc2518a52019-06-25 21:41:02 +02002043 tmp.data = 0;
2044 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02002045 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01002046 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02002047 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01002048 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02002049 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02002050 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02002051
Christopher Fauletb2e84162018-12-06 11:39:49 +01002052 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002053 if (type != HTX_BLK_DATA && vlen > count)
2054 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002055
Christopher Faulet94b2c762019-05-24 15:28:57 +02002056 if (type == HTX_BLK_UNUSED)
2057 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02002058
Christopher Faulet94b2c762019-05-24 15:28:57 +02002059 switch (h1m->state) {
2060 case H1_MSG_RQBEFORE:
2061 if (type != HTX_BLK_REQ_SL)
2062 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002063 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 +02002064 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002065 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02002066 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002067 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002068 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002069 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002070 if (sl->flags & HTX_SL_F_BODYLESS)
2071 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02002072 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01002073 if (h1s->meth == HTTP_METH_HEAD)
2074 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02002075 if (h1s->flags & H1S_F_RX_BLK) {
2076 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002077 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002078 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 +02002079 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002080 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02002081
Christopher Faulet94b2c762019-05-24 15:28:57 +02002082 case H1_MSG_RPBEFORE:
2083 if (type != HTX_BLK_RES_SL)
2084 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002085 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 +02002086 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002087 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02002088 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002089 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002090 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01002091 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002092 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01002093 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02002094 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01002095 else if (h1s->status == 204 || h1s->status == 304)
2096 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02002097 h1m->state = H1_MSG_HDR_FIRST;
2098 break;
2099
Christopher Faulet94b2c762019-05-24 15:28:57 +02002100 case H1_MSG_HDR_FIRST:
2101 case H1_MSG_HDR_NAME:
2102 case H1_MSG_HDR_L2_LWS:
2103 if (type == HTX_BLK_EOH)
2104 goto last_lf;
2105 if (type != HTX_BLK_HDR)
2106 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002107
Christopher Faulet9768c262018-10-22 09:34:31 +02002108 h1m->state = H1_MSG_HDR_NAME;
2109 n = htx_get_blk_name(chn_htx, blk);
2110 v = htx_get_blk_value(chn_htx, blk);
2111
Christopher Faulet86d144c2019-08-14 16:32:25 +02002112 /* Skip all pseudo-headers */
2113 if (*(n.ptr) == ':')
2114 goto skip_hdr;
2115
Christopher Faulet91fcf212020-12-02 16:17:15 +01002116 if (isteq(n, ist("transfer-encoding"))) {
2117 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2118 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02002119 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01002120 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002121 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01002122 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2123 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01002124 /* Only skip C-L header with invalid value. */
2125 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01002126 goto skip_hdr;
2127 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002128 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01002129 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02002130 if (!v.len)
2131 goto skip_hdr;
2132 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01002133 else if (isteq(n, ist("upgrade"))) {
2134 h1_parse_upgrade_header(h1m, v);
2135 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002136 else if ((isteq(n, ist("sec-websocket-accept")) &&
2137 h1m->flags & H1_MF_RESP) ||
2138 (isteq(n, ist("sec-websocket-key")) &&
2139 !(h1m->flags & H1_MF_RESP))) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002140 ws_key_found = 1;
2141 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02002142 else if (isteq(n, ist("te"))) {
2143 /* "te" may only be sent with "trailers" if this value
2144 * is present, otherwise it must be deleted.
2145 */
2146 v = istist(v, ist("trailers"));
2147 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2148 goto skip_hdr;
2149 v = ist("trailers");
2150 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002151
Christopher Faulet67d58092019-10-02 10:51:38 +02002152 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002153 if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) &&
2154 isteqi(n, h1c->px->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02002155 goto skip_hdr;
2156
Christopher Faulet98fbe952019-07-22 16:18:24 +02002157 /* Try to adjust the case of the header name */
2158 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2159 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002160 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002161 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002162 skip_hdr:
2163 h1m->state = H1_MSG_HDR_L2_LWS;
2164 break;
2165
Christopher Faulet94b2c762019-05-24 15:28:57 +02002166 case H1_MSG_LAST_LF:
2167 if (type != HTX_BLK_EOH)
2168 goto error;
2169 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002170 h1m->state = H1_MSG_LAST_LF;
2171 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002172 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002173 /* If the reply comes from haproxy while the request is
2174 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002175 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2176 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2177 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002178 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2179 /* T-E + C-L: force close */
2180 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2181 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2182 }
2183 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2184 /* T-E + HTTP/1.0: force close */
2185 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2186 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2187 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002188
2189 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002190 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002191 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002192 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002193 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002194 /* Try to adjust the case of the header name */
2195 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2196 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002197 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002198 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002199 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002200 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002201 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002202
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002203 if ((h1s->meth != HTTP_METH_CONNECT &&
2204 (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 +01002205 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002206 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002207 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002208 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2209 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002210 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002211 n = ist("transfer-encoding");
2212 v = ist("chunked");
2213 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2214 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002215 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002216 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002217 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002218 h1m->flags |= H1_MF_CHNK;
2219 }
2220
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002221 /* Now add the server name to a header (if requested) */
2222 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002223 !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002224 struct server *srv = objt_server(h1c->conn->target);
2225
2226 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002227 n = h1c->px->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002228 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002229
2230 /* Try to adjust the case of the header name */
2231 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2232 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002233 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002234 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002235 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002236 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002237 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2238 }
2239
Amaury Denoyellec1938232020-12-11 17:53:03 +01002240 /* Add websocket handshake key if needed */
2241 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) &&
2242 !ws_key_found) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002243 if (!(h1m->flags & H1_MF_RESP)) {
2244 /* generate a random websocket key
2245 * stored in the session to
2246 * verify it on the response side
2247 */
2248 h1_generate_random_ws_input_key(h1s->ws_key);
2249
2250 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2251 ist(h1s->ws_key),
2252 &tmp)) {
2253 goto full;
2254 }
2255 }
2256 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002257 /* add the response header key */
2258 char key[29];
2259 h1_calculate_ws_output_key(h1s->ws_key, key);
2260 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2261 ist(key),
2262 &tmp)) {
2263 goto full;
2264 }
2265 }
2266 }
2267
Christopher Faulet6b81df72019-10-01 22:08:43 +02002268 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2269 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2270
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002271 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002272 if (!chunk_memcat(&tmp, "\r\n", 2))
2273 goto full;
2274 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002275 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002276 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002277 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002278 if (!chunk_memcat(&tmp, "\r\n", 2))
2279 goto full;
2280 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002281 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002282 else if ((h1m->flags & H1_MF_RESP) &&
2283 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002284 if (!chunk_memcat(&tmp, "\r\n", 2))
2285 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002286 h1m_init_res(&h1s->res);
2287 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002288 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002289 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002290 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002291 else {
Christopher Faulet2eb52432022-04-07 10:29:38 +02002292 /* EOM flag is set or empty payload (C-L to 0) and it is the last block */
2293 if (htx_is_unique_blk(chn_htx, blk) &&
2294 ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002295 if (h1m->flags & H1_MF_CHNK) {
2296 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2297 goto full;
2298 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002299 else if (!chunk_memcat(&tmp, "\r\n", 2))
2300 goto full;
2301 goto done;
2302 }
2303 else if (!chunk_memcat(&tmp, "\r\n", 2))
2304 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002305 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002306 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002307 break;
2308
Christopher Faulet94b2c762019-05-24 15:28:57 +02002309 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002310 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002311 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002312 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2313 goto trailers;
2314
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002315 /* If the message is not chunked, never
2316 * add the last chunk. */
2317 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002318 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002319 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 +02002320 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002321 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002322 else if (type != HTX_BLK_DATA)
2323 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002324
2325 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 +02002326
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002327 /* It is the last block of this message. After this one,
2328 * only tunneled data may be forwarded. */
2329 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2330 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2331 last_data = 1;
2332 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002333
2334 if (vlen > count) {
2335 /* Get the maximum amount of data we can xferred */
2336 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002337 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002338 }
2339
Christopher Faulet140f1a52021-11-26 16:37:55 +01002340 if (h1m->state == H1_MSG_DATA) {
2341 if (h1m->flags & H1_MF_CLEN) {
2342 if (vlen > h1m->curr_len) {
2343 TRACE_ERROR("too much payload, more than announced",
2344 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2345 goto error;
2346 }
Christopher Faulet140f1a52021-11-26 16:37:55 +01002347 }
2348 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2349 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2350 goto skip_data;
2351 }
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002352 }
2353
Christopher Fauletd9233f02019-10-14 14:01:24 +02002354 chklen = 0;
2355 if (h1m->flags & H1_MF_CHNK) {
2356 chklen = b_room(&tmp);
2357 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2358 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2359 (chklen < 1048576) ? 5 : 8);
2360 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002361
2362 /* If it is the end of the chunked message (without EOT), reserve the
2363 * last chunk size */
2364 if (last_data)
2365 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002366 }
2367
2368 if (vlen + chklen > b_room(&tmp)) {
2369 /* too large for the buffer */
2370 if (chklen >= b_room(&tmp))
2371 goto full;
2372 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002373 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002374 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002375 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002376 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002377 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002378 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002379
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002380 /* Space already reserved, so it must succeed */
2381 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2382 goto error;
2383
Christopher Faulet6b81df72019-10-01 22:08:43 +02002384 if (h1m->state == H1_MSG_DATA)
2385 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002386 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002387 else
2388 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002389 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002390
2391 skip_data:
Christopher Faulet2eb52432022-04-07 10:29:38 +02002392 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Fauletb4eca0e2022-01-10 17:27:51 +01002393 h1m->curr_len -= vlen;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002394 if (!h1m->curr_len)
2395 last_data = 1;
2396 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002397 if (last_data)
2398 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002399 break;
2400
Christopher Faulet94b2c762019-05-24 15:28:57 +02002401 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002402 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002403 goto error;
2404 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002405 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002406
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002407 if (!(h1m->flags & H1_MF_CHNK))
2408 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002409
Christopher Faulete5596bf2020-12-02 16:13:22 +01002410 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2411 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 +01002412 if (type == HTX_BLK_EOT)
2413 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002414 break;
2415 }
2416
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002417 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002418 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002419 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002420 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2421 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002422 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002423 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002424 else { // HTX_BLK_TLR
2425 n = htx_get_blk_name(chn_htx, blk);
2426 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002427
2428 /* Try to adjust the case of the header name */
2429 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2430 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002431 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002432 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002433 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002434 break;
2435
Christopher Faulet94b2c762019-05-24 15:28:57 +02002436 case H1_MSG_DONE:
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002437 /* If the message is not chunked, ignore
2438 * trailers. It may happen with H2 messages. */
2439 if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && !(h1m->flags & H1_MF_CHNK))
2440 break;
2441
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002442 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2443 goto error; /* For now return an error */
2444
Christopher Faulet94b2c762019-05-24 15:28:57 +02002445 done:
2446 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002447 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002448 h1s->flags |= H1S_F_TX_BLK;
2449 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002450 }
2451 else if ((h1m->flags & H1_MF_RESP) &&
2452 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2453 /* a successful reply to a CONNECT or a protocol switching is sent
2454 * to the client. Switch the response to tunnel mode.
2455 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002456 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002457 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 +01002458 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002459
Christopher Faulet10a86702021-04-07 14:18:11 +02002460 if (h1s->flags & H1S_F_RX_BLK) {
2461 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002462 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002463 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 +02002464 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002465
2466 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2467 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002468 break;
2469
Christopher Faulet9768c262018-10-22 09:34:31 +02002470 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002471 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002472 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002473 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002474 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002475 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002476 TRACE_ERROR("processing output error, set error on h1c/h1s",
2477 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 +01002478 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02002479 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002480
2481 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002482 total += vlen;
2483 count -= vlen;
2484 if (sz == vlen)
2485 blk = htx_remove_blk(chn_htx, blk);
2486 else {
2487 htx_cut_data_blk(chn_htx, blk, vlen);
2488 break;
2489 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002490 }
2491
Christopher Faulet9768c262018-10-22 09:34:31 +02002492 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002493 /* when the output buffer is empty, tmp shares the same area so that we
2494 * only have to update pointers and lengths.
2495 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002496 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2497 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002498 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002499 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002500
Willy Tarreau3815b222018-12-11 19:50:43 +01002501 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002502 out:
2503 if (!buf_room_for_htx_data(&h1c->obuf)) {
2504 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002505 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002506 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002507 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002508 /* Both the request and the response reached the DONE state. So set EOI
Willy Tarreau4596fe22022-05-17 19:07:51 +02002509 * flag on the stream connector. Most of time, the flag will already be set,
Christopher Fauletdea24742021-01-22 15:12:30 +01002510 * except for protocol upgrades. Report an error if data remains blocked
2511 * in the output buffer.
2512 */
2513 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2514 if (!htx_is_empty(chn_htx)) {
2515 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002516 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 +01002517 }
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02002518 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletdea24742021-01-22 15:12:30 +01002519 }
2520
Christopher Faulet6b81df72019-10-01 22:08:43 +02002521 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002522 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002523
2524 full:
2525 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2526 h1c->flags |= H1C_F_OUT_FULL;
2527 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002528}
2529
Christopher Faulet51dbc942018-09-13 09:05:15 +02002530/*********************************************************/
2531/* functions below are I/O callbacks from the connection */
2532/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002533static void h1_wake_stream_for_recv(struct h1s *h1s)
2534{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002535 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002536 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002537 tasklet_wakeup(h1s->subs->tasklet);
2538 h1s->subs->events &= ~SUB_RETRY_RECV;
2539 if (!h1s->subs->events)
2540 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002541 }
2542}
2543static void h1_wake_stream_for_send(struct h1s *h1s)
2544{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002545 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002546 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002547 tasklet_wakeup(h1s->subs->tasklet);
2548 h1s->subs->events &= ~SUB_RETRY_SEND;
2549 if (!h1s->subs->events)
2550 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002551 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002552}
2553
Christopher Fauletad4daf62021-01-21 17:49:01 +01002554/* alerts the data layer following this sequence :
2555 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2556 * - if its subscribed to send, then it's woken up for send
2557 * - if it was subscribed to neither, its ->wake() callback is called
2558 */
2559static void h1_alert(struct h1s *h1s)
2560{
2561 if (h1s->subs) {
2562 h1_wake_stream_for_recv(h1s);
2563 h1_wake_stream_for_send(h1s);
2564 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002565 else if (h1s_sc(h1s) && h1s_sc(h1s)->app_ops->wake != NULL) {
Christopher Fauletad4daf62021-01-21 17:49:01 +01002566 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002567 h1s_sc(h1s)->app_ops->wake(h1s_sc(h1s));
Christopher Fauletad4daf62021-01-21 17:49:01 +01002568 }
2569}
2570
Christopher Fauletc18fc232020-10-06 17:41:36 +02002571/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2572 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2573 * retryable errors (allocation error or buffer full). On success, the error is
2574 * copied in the output buffer.
2575*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002576static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002577{
2578 int rc = http_get_status_idx(h1c->errcode);
2579 int ret = 0;
2580
2581 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2582
2583 /* Verify if the error is mapped on /dev/null or any empty file */
2584 /// XXX: do a function !
2585 if (h1c->px->replies[rc] &&
2586 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2587 h1c->px->replies[rc]->body.errmsg &&
2588 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2589 /* Empty error, so claim a success */
2590 ret = 1;
2591 goto out;
2592 }
2593
2594 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2595 h1c->flags |= H1C_F_ERR_PENDING;
2596 goto out;
2597 }
2598
2599 if (!h1_get_buf(h1c, &h1c->obuf)) {
2600 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2601 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2602 goto out;
2603 }
Tim Duesterhus77508502022-03-15 13:11:06 +01002604 ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc]));
Christopher Fauletc18fc232020-10-06 17:41:36 +02002605 if (unlikely(ret <= 0)) {
2606 if (!ret) {
2607 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2608 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2609 goto out;
2610 }
2611 else {
2612 /* we cannot report this error, so claim a success */
2613 ret = 1;
2614 }
2615 }
2616 h1c->flags &= ~H1C_F_ERR_PENDING;
2617 out:
2618 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2619 return ret;
2620}
2621
2622/* Try to send a 500 internal error. It relies on h1_send_error to send the
2623 * error. This function takes care of incrementing stats and tracked counters.
2624 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002625static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002626{
2627 struct session *sess = h1c->conn->owner;
2628 int ret = 1;
2629
2630 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002631 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002632 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2633 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002634 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002635 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002636
2637 h1c->errcode = 500;
2638 ret = h1_send_error(h1c);
2639 sess_log(sess);
2640 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002641}
2642
Christopher Fauletb3230f72021-09-21 18:38:20 +02002643/* Try to send an error because of a parsing error. By default a 400 bad request
2644 * error is returned. But the status code may be specified by setting
2645 * h1c->errcode. It relies on h1_send_error to send the error. This function
2646 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002647 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002648static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002649{
2650 struct session *sess = h1c->conn->owner;
2651 int ret = 1;
2652
2653 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2654 goto end;
2655
2656 session_inc_http_req_ctr(sess);
2657 session_inc_http_err_ctr(sess);
2658 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002659 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2660 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002661 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002662 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002663
Christopher Fauletb3230f72021-09-21 18:38:20 +02002664 if (!h1c->errcode)
2665 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002666 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002667 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2668 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002669
2670 end:
2671 return ret;
2672}
2673
Christopher Faulet2eed8002020-12-07 11:26:13 +01002674/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2675 * the error. This function takes care of incrementing stats and tracked
2676 * counters.
2677 */
2678static int h1_handle_not_impl_err(struct h1c *h1c)
2679{
2680 struct session *sess = h1c->conn->owner;
2681 int ret = 1;
2682
2683 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2684 goto end;
2685
2686 session_inc_http_req_ctr(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002687 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002688 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2689 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002690 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002691 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002692
2693 h1c->errcode = 501;
2694 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002695 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2696 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002697
2698 end:
2699 return ret;
2700}
2701
Christopher Fauletc18fc232020-10-06 17:41:36 +02002702/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2703 * error. This function takes care of incrementing stats and tracked counters.
2704 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002705static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002706{
2707 struct session *sess = h1c->conn->owner;
2708 int ret = 1;
2709
2710 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2711 goto end;
2712
2713 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002714 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002715 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2716 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002717 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002718 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002719
2720 h1c->errcode = 408;
Christopher Faulet07e10de2021-07-26 09:42:49 +02002721 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2722 ret = h1_send_error(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002723 sess_log(sess);
2724 end:
2725 return ret;
2726}
2727
2728
Christopher Faulet51dbc942018-09-13 09:05:15 +02002729/*
2730 * Attempt to read data, and subscribe if none available
2731 */
2732static int h1_recv(struct h1c *h1c)
2733{
2734 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002735 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002736 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002737
Christopher Faulet6b81df72019-10-01 22:08:43 +02002738 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2739
2740 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2741 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002742 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002743 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002744
Christopher Fauletae635762020-09-21 11:47:16 +02002745 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2746 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002747 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002748 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002749
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002750 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2751 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002752 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002753 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002754 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002755
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002756 /*
2757 * If we only have a small amount of data, realign it,
2758 * it's probably cheaper than doing 2 recv() calls.
2759 */
2760 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002761 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002762
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002763 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002764 if (!h1c->h1s ||
2765 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2766 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002767 flags |= CO_RFL_READ_ONCE;
2768
Willy Tarreau45f2b892018-12-05 07:59:27 +01002769 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002770 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002771 if (h1c->flags & H1C_F_IN_FULL) {
2772 h1c->flags &= ~H1C_F_IN_FULL;
2773 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2774 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002775
2776 if (!b_data(&h1c->ibuf)) {
2777 /* try to pre-align the buffer like the rxbufs will be
2778 * to optimize memory copies.
2779 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002780 h1c->ibuf.head = sizeof(struct htx);
2781 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002782 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet41951ab2021-11-26 18:12:51 +01002783 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002784 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002785 if (max && !ret && h1_recv_allowed(h1c)) {
2786 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2787 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002788 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002789 else {
2790 h1_wake_stream_for_recv(h1c->h1s);
2791 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002792 }
2793
Christopher Faulet51dbc942018-09-13 09:05:15 +02002794 if (!b_data(&h1c->ibuf))
2795 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002796 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002797 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002798 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2799 }
2800
2801 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002802 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002803}
2804
2805
2806/*
2807 * Try to send data if possible
2808 */
2809static int h1_send(struct h1c *h1c)
2810{
2811 struct connection *conn = h1c->conn;
2812 unsigned int flags = 0;
2813 size_t ret;
2814 int sent = 0;
2815
Christopher Faulet6b81df72019-10-01 22:08:43 +02002816 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2817
2818 if (conn->flags & CO_FL_ERROR) {
2819 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002820 b_reset(&h1c->obuf);
2821 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002822 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002823
2824 if (!b_data(&h1c->obuf))
2825 goto end;
2826
Christopher Faulet46230362019-10-17 16:04:20 +02002827 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002828 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002829 if (h1c->flags & H1C_F_CO_STREAMER)
2830 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002831
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002832 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002833 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002834 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002835 if (h1c->flags & H1C_F_OUT_FULL) {
2836 h1c->flags &= ~H1C_F_OUT_FULL;
2837 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2838 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01002839 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002840 b_del(&h1c->obuf, ret);
2841 sent = 1;
2842 }
2843
Christopher Faulet145aa472018-12-06 10:56:20 +01002844 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002845 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002846 /* error or output closed, nothing to send, clear the buffer to release it */
2847 b_reset(&h1c->obuf);
2848 }
2849
Christopher Faulet51dbc942018-09-13 09:05:15 +02002850 end:
Christopher Fauletc17c31c2022-02-01 18:25:06 +01002851 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002852 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002853
Christopher Faulet51dbc942018-09-13 09:05:15 +02002854 /* We're done, no more to send */
2855 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002856 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002857 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002858 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002859 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002860 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002861 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002862 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002863 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2864 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002865 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002866 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002867
Christopher Faulet6b81df72019-10-01 22:08:43 +02002868 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002869 return sent;
2870}
2871
Christopher Faulet51dbc942018-09-13 09:05:15 +02002872/* callback called on any event by the connection handler.
2873 * It applies changes and returns zero, or < 0 if it wants immediate
2874 * destruction of the connection.
2875 */
2876static int h1_process(struct h1c * h1c)
2877{
2878 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002879 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002880
Christopher Faulet6b81df72019-10-01 22:08:43 +02002881 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2882
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002883 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002884 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Fauletab7389d2021-09-16 08:16:23 +02002885 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2886 !(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 +02002887 struct buffer *buf;
2888 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002889
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002890 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2891 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002892 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002893
2894 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002895 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002896 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2897 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002898 /* Try to match H2 preface before parsing the request headers. */
2899 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2900 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002901 if (h1c->flags & H1C_F_ST_ATTACHED) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02002902 /* Force the REOS here to be sure to release the SC.
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002903 Here ATTACHED implies !READY, and h1s defined
2904 */
2905 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2906 h1s->flags |= H1S_F_REOS;
2907 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002908 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002909 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002910 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002911 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002912
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002913 /* Create the H1 stream if not already there */
2914 if (!h1s) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002915 h1s = h1c_frt_stream_new(h1c, NULL, h1c->conn->owner);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002916 if (!h1s) {
2917 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002918 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002919 goto no_parsing;
2920 }
2921 }
2922
2923 if (h1s->sess->t_idle == -1)
2924 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2925
2926 /* Get the stream rxbuf */
2927 buf = h1_get_buf(h1c, &h1s->rxbuf);
2928 if (!buf) {
2929 h1c->flags |= H1C_F_IN_SALLOC;
2930 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2931 return 0;
2932 }
2933
2934 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01002935 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002936 h1_release_buf(h1c, &h1s->rxbuf);
2937 h1_set_idle_expiration(h1c);
2938
2939 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002940 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002941 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002942 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet26a26432021-01-27 11:27:50 +01002943 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002944 }
2945 else if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02002946 h1_handle_parsing_error(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002947 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002948 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002949 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002950 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2951 h1_handle_not_impl_err(h1c);
2952 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002953 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002954 }
Christopher Fauletae635762020-09-21 11:47:16 +02002955 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002956 h1_send(h1c);
2957
Christopher Faulet75308302021-11-15 14:51:37 +01002958 /* H1 connection must be released ASAP if:
2959 * - an error occurred on the connection or the H1C or
2960 * - a read0 was received or
2961 * - a silent shutdown was emitted and all outgoing data sent
2962 */
2963 if ((conn->flags & CO_FL_ERROR) ||
2964 conn_xprt_read0_pending(conn) ||
2965 (h1c->flags & H1C_F_ST_ERROR) ||
2966 ((h1c->flags & H1C_F_ST_SILENT_SHUT) && !b_data(&h1c->obuf))) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002967 if (!(h1c->flags & H1C_F_ST_READY)) {
Willy Tarreau4596fe22022-05-17 19:07:51 +02002968 /* No stream connector or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002969 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet75308302021-11-15 14:51:37 +01002970 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN))) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02002971 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002972 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002973 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002974 }
2975
2976 /* Handle pending error, if any (only possible on frontend connection) */
2977 if (h1c->flags & H1C_F_ERR_PENDING) {
2978 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2979 if (h1_send_error(h1c))
2980 h1_send(h1c);
2981 }
Christopher Fauletae635762020-09-21 11:47:16 +02002982
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002983 /* If there is some pending outgoing data or error, just wait */
2984 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2985 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002986
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002987 /* Otherwise we can release the H1 connection */
2988 goto release;
2989 }
2990 else {
Willy Tarreau4596fe22022-05-17 19:07:51 +02002991 /* Here there is still a H1 stream with a stream connector.
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002992 * Report the connection state at the stream level
2993 */
2994 if (conn_xprt_read0_pending(conn)) {
2995 h1s->flags |= H1S_F_REOS;
2996 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2997 }
Willy Tarreaucad42a72022-08-29 10:22:56 +02002998 if ((h1c->flags & H1C_F_ST_ERROR) || ((conn->flags & CO_FL_ERROR) &&
2999 (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf))))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003000 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003001 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01003002 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003003 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003004 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003005
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003006 if (!b_data(&h1c->ibuf))
3007 h1_release_buf(h1c, &h1c->ibuf);
3008
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003009 /* Check if a soft-stop is in progress.
3010 * Release idling front connection if this is the case.
3011 */
3012 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003013 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
William Dauchya9dd9012022-01-05 22:53:24 +01003014 if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) &&
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003015 h1c->flags & H1C_F_WAIT_NEXT_REQ) {
3016
3017 int send_close = 1;
3018 /* If a close-spread-time option is set, we want to avoid
3019 * closing all the active HTTP2 connections at once so we add a
3020 * random factor that will spread the closing.
3021 */
3022 if (tick_isset(global.close_spread_end)) {
3023 int remaining_window = tick_remain(now_ms, global.close_spread_end);
3024 if (remaining_window) {
3025 /* This should increase the closing rate the
3026 * further along the window we are.
3027 */
3028 send_close = (remaining_window <= statistical_prng_range(global.close_spread_time));
3029 }
3030 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02003031 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
3032 send_close = 0; /* let the client close his connection himself */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003033 if (send_close)
3034 goto release;
3035 }
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003036 }
3037 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003038
3039 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
3040 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
3041 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003042 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003043
Christopher Faulet47365272018-10-31 17:40:50 +01003044 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02003045 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003046 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003047 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01003048
3049 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003050 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003051 /* Don't release the H1 connection right now, we must destroy the
Willy Tarreaue68bc612022-05-27 11:23:05 +02003052 * attached SC first. Here, the H1C must not be READY */
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003053 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
3054
3055 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003056 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003057 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003058 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003059 h1_alert(h1s);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003060 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003061 }
3062 else {
3063 h1_release(h1c);
3064 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
3065 }
Christopher Faulet539e0292018-11-19 10:40:09 +01003066 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003067}
3068
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003069struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003070{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003071 struct connection *conn;
3072 struct tasklet *tl = (struct tasklet *)t;
3073 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003074 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003075 int ret = 0;
3076
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003077 if (state & TASK_F_USR1) {
3078 /* the tasklet was idling on an idle connection, it might have
3079 * been stolen, let's be careful!
3080 */
3081 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3082 if (tl->context == NULL) {
3083 /* The connection has been taken over by another thread,
3084 * we're no longer responsible for it, so just free the
3085 * tasklet, and do nothing.
3086 */
3087 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3088 tasklet_free(tl);
3089 return NULL;
3090 }
3091 conn = h1c->conn;
3092 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003093
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003094 /* Remove the connection from the list, to be sure nobody attempts
3095 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003096 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003097 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3098 if (conn_in_list)
3099 conn_delete_from_tree(&conn->hash_node->node);
3100
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003101 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003102 } else {
3103 /* we're certain the connection was not in an idle list */
3104 conn = h1c->conn;
3105 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
3106 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003107 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003108
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003109 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003110 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003111 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003112 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003113 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003114 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01003115
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003116 /* If we were in an idle list, we want to add it back into it,
3117 * unless h1_process() returned -1, which mean it has destroyed
3118 * the connection (testing !ret is enough, if h1_process() wasn't
3119 * called then ret will be 0 anyway.
3120 */
Willy Tarreau74163142021-03-13 11:30:19 +01003121 if (ret < 0)
3122 t = NULL;
3123
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003124 if (!ret && conn_in_list) {
3125 struct server *srv = objt_server(conn->target);
3126
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003127 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003128 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003129 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003130 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003131 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003132 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003133 }
Willy Tarreau74163142021-03-13 11:30:19 +01003134 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003135}
3136
Christopher Faulet51dbc942018-09-13 09:05:15 +02003137static int h1_wake(struct connection *conn)
3138{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003139 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01003140 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003141
Christopher Faulet6b81df72019-10-01 22:08:43 +02003142 TRACE_POINT(H1_EV_H1C_WAKE, conn);
3143
Christopher Faulet539e0292018-11-19 10:40:09 +01003144 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01003145 ret = h1_process(h1c);
3146 if (ret == 0) {
3147 struct h1s *h1s = h1c->h1s;
3148
Christopher Fauletad4daf62021-01-21 17:49:01 +01003149 if (h1c->flags & H1C_F_ST_ATTACHED)
3150 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003151 }
3152 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003153}
3154
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003155/* Connection timeout management. The principle is that if there's no receipt
3156 * nor sending for a certain amount of time, the connection is closed.
3157 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003158struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003159{
3160 struct h1c *h1c = context;
3161 int expired = tick_is_expired(t->expire, now_ms);
3162
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003163 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003164
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003165 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003166 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003167 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003168
3169 /* Somebody already stole the connection from us, so we should not
3170 * free it, we just have to free the task.
3171 */
3172 if (!t->context) {
3173 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003174 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003175 goto do_leave;
3176 }
3177
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003178 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003179 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003180 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003181 return t;
3182 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003183
Willy Tarreau4596fe22022-05-17 19:07:51 +02003184 /* If a stream connector is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003185 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003186 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003187 if (h1c->flags & H1C_F_ST_READY) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003188 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003189 t->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +02003190 TRACE_DEVEL("leaving (SC still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003191 return t;
3192 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003193
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003194 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003195 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
3196 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003197 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003198 if (h1_handle_req_tout(h1c))
3199 h1_send(h1c);
3200 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
3201 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003202 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003203 return t;
3204 }
3205 }
3206
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003207 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003208 /* Don't release the H1 connection right now, we must destroy the
Willy Tarreaue68bc612022-05-27 11:23:05 +02003209 * attached SC first. Here, the H1C must not be READY */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003210 se_fl_set(h1c->h1s->sd, SE_FL_EOS | SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003211 h1_alert(h1c->h1s);
3212 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003213 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003214 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003215 return t;
3216 }
3217
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003218 /* We're about to destroy the connection, so make sure nobody attempts
3219 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003220 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003221 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003222 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003223
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003224 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003225 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003226
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003227 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003228 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003229
3230 if (!h1c) {
3231 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003232 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003233 return NULL;
3234 }
3235
3236 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003237 h1_release(h1c);
3238 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003239 return NULL;
3240}
3241
Christopher Faulet51dbc942018-09-13 09:05:15 +02003242/*******************************************/
3243/* functions below are used by the streams */
3244/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003245
Christopher Faulet51dbc942018-09-13 09:05:15 +02003246/*
3247 * Attach a new stream to a connection
3248 * (Used for outgoing connections)
3249 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003250static int h1_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003251{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003252 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003253 struct h1s *h1s;
3254
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003255 /* this connection is no more idle (if it was at all) */
3256 h1c->flags &= ~H1C_F_ST_SILENT_SHUT;
3257
Christopher Faulet6b81df72019-10-01 22:08:43 +02003258 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003259 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003260 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3261 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003262 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003263
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003264 h1s = h1c_bck_stream_new(h1c, sd->sc, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003265 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003266 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3267 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003268 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003269
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003270 /* the connection is not idle anymore, let's mark this */
3271 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003272 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003273
Christopher Faulet6b81df72019-10-01 22:08:43 +02003274 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulete00ad352021-12-16 14:44:31 +01003275 return 0;
Christopher Faulet26a26432021-01-27 11:27:50 +01003276 err:
Christopher Faulet26a26432021-01-27 11:27:50 +01003277 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01003278 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003279}
3280
Willy Tarreau4596fe22022-05-17 19:07:51 +02003281/* Retrieves a valid stream connector from this connection, or returns NULL.
3282 * For this mux, it's easy as we can only store a single stream connector.
Christopher Faulet51dbc942018-09-13 09:05:15 +02003283 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003284static struct stconn *h1_get_first_sc(const struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003285{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003286 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003287 struct h1s *h1s = h1c->h1s;
3288
3289 if (h1s)
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02003290 return h1s_sc(h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003291
3292 return NULL;
3293}
3294
Christopher Faulet73c12072019-04-08 11:23:22 +02003295static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003296{
Christopher Faulet73c12072019-04-08 11:23:22 +02003297 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003298
Christopher Faulet6b81df72019-10-01 22:08:43 +02003299 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet7c452cc2022-04-14 11:08:26 +02003300 if (!h1c->h1s || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003301 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003302}
3303
3304/*
3305 * Detach the stream from the connection and possibly release the connection.
3306 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003307static void h1_detach(struct sedesc *sd)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003308{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003309 struct h1s *h1s = sd->se;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003310 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003311 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003312 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003313
Christopher Faulet6b81df72019-10-01 22:08:43 +02003314 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3315
Christopher Faulet6b81df72019-10-01 22:08:43 +02003316 if (!h1s) {
3317 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003318 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003319 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003320
Olivier Houchardf502aca2018-12-14 19:42:40 +01003321 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003322 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003323
Christopher Faulet42849b02020-10-05 11:35:17 +02003324 sess->accept_date = date;
3325 sess->tv_accept = now;
3326 sess->t_handshake = 0;
3327 sess->t_idle = -1;
3328
Olivier Houchard8a786902018-12-15 16:05:40 +01003329 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3330 h1s_destroy(h1s);
3331
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003332 if ((h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_IDLE)) == (H1C_F_IS_BACK|H1C_F_ST_IDLE)) {
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003333 /* this connection may be killed at any moment, we want it to
3334 * die "cleanly" (i.e. only an RST).
3335 */
3336 h1c->flags |= H1C_F_ST_SILENT_SHUT;
3337
Christopher Fauletf1204b82019-07-19 14:51:06 +02003338 /* If there are any excess server data in the input buffer,
3339 * release it and close the connection ASAP (some data may
3340 * remain in the output buffer). This happens if a server sends
3341 * invalid responses. So in such case, we don't want to reuse
3342 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003343 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003344 if (b_data(&h1c->ibuf)) {
3345 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003346 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003347 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003348 goto release;
3349 }
Christopher Faulet03627242019-07-19 11:34:08 +02003350
Christopher Faulet08016ab2020-07-01 16:10:06 +02003351 if (h1c->conn->flags & CO_FL_PRIVATE) {
3352 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003353 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3354 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003355 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003356 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003357 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003358 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003359 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003360 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003361 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3362 goto end;
3363 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003364 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003365 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003366 if (h1c->conn->owner == sess)
3367 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003368
3369 /* mark that the tasklet may lose its context to another thread and
3370 * that the handler needs to check it under the idle conns lock.
3371 */
3372 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003373 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003374 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3375
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003376 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003377 /* The server doesn't want it, let's kill the connection right away */
3378 h1c->conn->mux->destroy(h1c);
3379 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3380 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003381 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003382 /* At this point, the connection has been added to the
3383 * server idle list, so another thread may already have
3384 * hijacked it, so we can't do anything with it.
3385 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003386 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003387 }
3388 }
3389
Christopher Fauletf1204b82019-07-19 14:51:06 +02003390 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003391 /* 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 +01003392 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02003393 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003394 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003395 !h1c->conn->owner) {
3396 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003397 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003398 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003399 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003400 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003401 /* If we have a new request, process it immediately or
3402 * subscribe for reads waiting for new data
3403 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003404 if (unlikely(b_data(&h1c->ibuf))) {
3405 if (h1_process(h1c) == -1)
3406 goto end;
3407 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003408 else
3409 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3410 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003411 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003412 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003413 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003414 end:
3415 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003416}
3417
3418
Willy Tarreau000d63c2022-05-27 10:39:17 +02003419static void h1_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003420{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003421 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet7f366362019-04-08 10:51:20 +02003422 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003423
3424 if (!h1s)
3425 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003426 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003427
Christopher Faulet99293b02021-11-10 10:30:15 +01003428 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003429
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003430 if (se_fl_test(h1s->sd, SE_FL_SHR))
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003431 goto end;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003432 if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003433 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
3434 goto do_shutr;
3435 }
3436 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3437 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003438 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003439 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003440
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003441 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02003442 /* Here attached is implicit because there is SC */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003443 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3444 goto end;
3445 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003446 if (h1s->flags & H1S_F_WANT_KAL) {
3447 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003448 goto end;
3449 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003450
Christopher Faulet7f366362019-04-08 10:51:20 +02003451 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003452 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003453 if (se_fl_test(h1s->sd, SE_FL_SHR))
Christopher Faulet6b81df72019-10-01 22:08:43 +02003454 goto end;
Christopher Faulet897d6122021-12-17 17:28:35 +01003455
3456 if (conn_xprt_ready(h1c->conn) && h1c->conn->xprt->shutr)
Christopher Faulet07976562022-03-31 11:05:05 +02003457 h1c->conn->xprt->shutr(h1c->conn, h1c->conn->xprt_ctx, (mode == CO_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003458 end:
3459 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003460}
3461
Willy Tarreau000d63c2022-05-27 10:39:17 +02003462static void h1_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003463{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003464 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003465 struct h1c *h1c;
3466
3467 if (!h1s)
3468 return;
3469 h1c = h1s->h1c;
3470
Christopher Faulet99293b02021-11-10 10:30:15 +01003471 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003472
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003473 if (se_fl_test(h1s->sd, SE_FL_SHW))
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003474 goto end;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003475 if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003476 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003477 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003478 }
3479 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3480 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3481 goto do_shutw;
3482 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003483
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003484 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02003485 /* Here attached is implicit because there is SC */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003486 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3487 goto end;
3488 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003489 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3490 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003491 goto end;
3492 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003493
Christopher Faulet7f366362019-04-08 10:51:20 +02003494 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003495 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet07976562022-03-31 11:05:05 +02003496 if (mode != CO_SHW_NORMAL)
Christopher Fauleta85c5222021-10-27 15:36:38 +02003497 h1c->flags |= H1C_F_ST_SILENT_SHUT;
3498
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003499 if (!b_data(&h1c->obuf))
Christopher Faulet897d6122021-12-17 17:28:35 +01003500 h1_shutw_conn(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003501 end:
3502 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003503}
3504
Christopher Fauleta85c5222021-10-27 15:36:38 +02003505static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003506{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003507 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003508
Willy Tarreau62592ad2021-03-26 09:09:42 +01003509 if (conn->flags & CO_FL_SOCK_WR_SH)
3510 return;
3511
Christopher Fauleta85c5222021-10-27 15:36:38 +02003512 TRACE_ENTER(H1_EV_H1C_END, conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003513 conn_xprt_shutw(conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02003514 conn_sock_shutw(conn, (h1c && !(h1c->flags & H1C_F_ST_SILENT_SHUT)));
3515 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003516}
3517
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003518/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3519 * The <es> pointer is not allowed to differ from the one passed to the
3520 * subscribe() call. It always returns zero.
3521 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003522static int h1_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003523{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003524 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003525
3526 if (!h1s)
3527 return 0;
3528
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003529 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003530 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003531
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003532 es->events &= ~event_type;
3533 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003534 h1s->subs = NULL;
3535
3536 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003537 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003538
3539 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003540 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003541
Christopher Faulet51dbc942018-09-13 09:05:15 +02003542 return 0;
3543}
3544
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003545/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3546 * event subscriber <es> is not allowed to change from a previous call as long
3547 * as at least one event is still subscribed. The <event_type> must only be a
3548 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
Willy Tarreau000d63c2022-05-27 10:39:17 +02003549 * the stream connector <sc> was already detached, in which case it will return
Willy Tarreau4596fe22022-05-17 19:07:51 +02003550 * -1.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003551 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003552static int h1_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003553{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003554 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51bb1852019-09-04 10:22:34 +02003555 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003556
3557 if (!h1s)
3558 return -1;
3559
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003560 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003561 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003562
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003563 es->events |= event_type;
3564 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003565
3566 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003567 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003568
3569
Christopher Faulet6b81df72019-10-01 22:08:43 +02003570 if (event_type & SUB_RETRY_SEND) {
3571 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003572 /*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003573 * If the stconn attempts to subscribe, and the
Christopher Faulet6b81df72019-10-01 22:08:43 +02003574 * mux isn't subscribed to the connection, then it
3575 * probably means the connection wasn't established
3576 * yet, so we have to subscribe.
3577 */
3578 h1c = h1s->h1c;
3579 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3580 h1c->conn->xprt->subscribe(h1c->conn,
3581 h1c->conn->xprt_ctx,
3582 SUB_RETRY_SEND,
3583 &h1c->wait_event);
3584 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003585 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003586}
3587
Christopher Faulet564e39c2021-09-21 15:50:55 +02003588/* Called from the upper layer, to receive data.
3589 *
3590 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3591 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3592 * means the caller wants to flush input data (from the mux buffer and the
3593 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3594 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3595 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3596 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3597 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3598 * copy as much data as possible.
3599 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003600static size_t h1_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003601{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003602 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet539e0292018-11-19 10:40:09 +01003603 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003604 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003605 size_t ret = 0;
3606
Willy Tarreau022e5e52020-09-10 09:33:15 +02003607 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003608
3609 /* Do nothing for now if not READY */
3610 if (!(h1c->flags & H1C_F_ST_READY)) {
3611 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3612 goto end;
3613 }
3614
Christopher Faulet539e0292018-11-19 10:40:09 +01003615 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003616 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003617 else
3618 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003619
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003620 if ((flags & CO_RFL_BUF_FLUSH) && se_fl_test(h1s->sd, SE_FL_MAY_SPLICE)) {
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003621 h1c->flags |= H1C_F_WANT_SPLICE;
3622 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 +02003623 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003624 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003625 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 +01003626 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003627 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003628
3629 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003630 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003631 return ret;
3632}
3633
3634
3635/* Called from the upper layer, to send data */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003636static size_t h1_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003637{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003638 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003639 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003640 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003641
3642 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003643 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003644 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003645
Willy Tarreau022e5e52020-09-10 09:33:15 +02003646 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003647
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003648 /* If we're not connected yet, or we're waiting for a handshake, stop
3649 * now, as we don't want to remove everything from the channel buffer
3650 * before we're sure we can send it.
3651 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003652 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003653 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003654 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003655 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003656
Christopher Fauletdea24742021-01-22 15:12:30 +01003657 if (h1c->flags & H1C_F_ST_ERROR) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003658 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet26a26432021-01-27 11:27:50 +01003659 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 +01003660 return 0;
3661 }
3662
Christopher Faulet46230362019-10-17 16:04:20 +02003663 /* Inherit some flags from the upper layer */
3664 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3665 if (flags & CO_SFL_MSG_MORE)
3666 h1c->flags |= H1C_F_CO_MSG_MORE;
3667 if (flags & CO_SFL_STREAMER)
3668 h1c->flags |= H1C_F_CO_STREAMER;
3669
Christopher Fauletc31872f2019-06-04 22:09:36 +02003670 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003671 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003672
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003673 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003674 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003675 else
3676 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003677
Christopher Faulet372b38f2022-07-08 15:20:31 +02003678 if ((h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)))
3679 break;
3680
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003681 if ((count - ret) > 0)
3682 h1c->flags |= H1C_F_CO_MSG_MORE;
3683
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003684 if (!ret)
3685 break;
3686 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003687 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003688 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003689 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003690 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003691
Willy Tarreaucad42a72022-08-29 10:22:56 +02003692 if ((h1c->flags & H1C_F_ST_ERROR) || ((h1c->conn->flags & CO_FL_ERROR) &&
3693 (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf)))) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003694 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet26a26432021-01-27 11:27:50 +01003695 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 +01003696 }
3697
Christopher Faulet7a145d62020-08-05 11:31:16 +02003698 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003699 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003700 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003701}
3702
Willy Tarreaue5733232019-05-22 19:24:06 +02003703#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003704/* Send and get, using splicing */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003705static int h1_rcv_pipe(struct stconn *sc, struct pipe *pipe, unsigned int count)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003706{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003707 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003708 struct h1c *h1c = h1s->h1c;
3709 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003710 int ret = 0;
3711
Christopher Faulet897d6122021-12-17 17:28:35 +01003712 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003713
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003714 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003715 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003716 TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulete18777b2019-04-16 16:46:36 +02003717 goto end;
3718 }
3719
Christopher Fauletcf307562021-07-26 10:49:39 +02003720 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003721 if (h1s_data_pending(h1s)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003722 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003723 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003724 }
3725
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003726 if (!h1_recv_allowed(h1c)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003727 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet0060be92020-07-03 15:02:25 +02003728 goto end;
3729 }
3730
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003731 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003732 count = h1m->curr_len;
Christopher Faulet897d6122021-12-17 17:28:35 +01003733 ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003734 if (ret >= 0) {
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003735 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003736 if (ret > h1m->curr_len) {
3737 h1s->flags |= H1S_F_PARSING_ERROR;
3738 h1c->flags |= H1C_F_ST_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003739 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003740 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003741 H1_EV_RX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003742 goto end;
3743 }
3744 h1m->curr_len -= ret;
3745 if (!h1m->curr_len) {
3746 h1m->state = H1_MSG_DONE;
3747 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003748 TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003749 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003750 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003751 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
3752 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret);
Christopher Faulete18777b2019-04-16 16:46:36 +02003753 }
3754
Christopher Faulet1be55f92018-10-02 15:59:23 +02003755 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003756 if (conn_xprt_read0_pending(h1c->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003757 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003758 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003759 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003760 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003761
Christopher Faulet94d35102021-04-09 11:58:49 +02003762 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003763 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003764 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Faulet94d35102021-04-09 11:58:49 +02003765 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003766 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s);
3767 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet94d35102021-04-09 11:58:49 +02003768 }
Christopher Faulet27182292020-07-03 15:08:49 +02003769 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003770
Christopher Faulet897d6122021-12-17 17:28:35 +01003771 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003772 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003773}
3774
Willy Tarreau000d63c2022-05-27 10:39:17 +02003775static int h1_snd_pipe(struct stconn *sc, struct pipe *pipe)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003776{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003777 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003778 struct h1c *h1c = h1s->h1c;
3779 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003780 int ret = 0;
3781
Christopher Faulet897d6122021-12-17 17:28:35 +01003782 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003783
Christopher Faulet140f1a52021-11-26 16:37:55 +01003784 if (b_data(&h1c->obuf)) {
3785 if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003786 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s);
3787 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003788 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003789 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003790 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003791
Christopher Faulet897d6122021-12-17 17:28:35 +01003792 ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe);
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003793 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003794 if (ret > h1m->curr_len) {
3795 h1s->flags |= H1S_F_PROCESSING_ERROR;
3796 h1c->flags |= H1C_F_ST_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003797 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003798 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003799 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 +01003800 goto end;
3801 }
3802 h1m->curr_len -= ret;
3803 if (!h1m->curr_len) {
3804 h1m->state = H1_MSG_DONE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003805 TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003806 }
3807 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003808 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
3809 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret);
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003810
3811 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003812 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003813 return ret;
3814}
3815#endif
3816
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003817static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3818{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003819 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003820 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003821
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003822 switch (mux_ctl) {
3823 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003824 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003825 ret |= MUX_STATUS_READY;
3826 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003827 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003828 if (output)
3829 *((int *)output) = h1c->errcode;
3830 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3831 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3832 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3833 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003834 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003835 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003836 default:
3837 return -1;
3838 }
3839}
3840
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003841/* appends some info about connection <h1c> to buffer <msg>, or does nothing if
3842 * <h1c> is NULL. Returns non-zero if the connection is considered suspicious.
3843 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
3844 * not NULL, otherwise a single line is used.
3845 */
3846static int h1_dump_h1c_info(struct buffer *msg, struct h1c *h1c, const char *pfx)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003847{
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003848 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003849
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003850 if (!h1c)
3851 return ret;
3852
Christopher Fauletf376a312019-01-04 15:16:06 +01003853 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3854 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003855 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3856 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003857 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003858 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003859 return ret;
3860}
3861
3862/* appends some info about stream <h1s> to buffer <msg>, or does nothing if
3863 * <h1s> is NULL. Returns non-zero if the stream is considered suspicious. May
3864 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
3865 * NULL, otherwise a single line is used.
3866 */
3867static int h1_dump_h1s_info(struct buffer *msg, const struct h1s *h1s, const char *pfx)
3868{
3869 const char *method;
3870 int ret = 0;
3871
3872 if (!h1s)
3873 return ret;
3874
3875 if (h1s->meth < HTTP_METH_OTHER)
3876 method = http_known_methods[h1s->meth].ptr;
3877 else
3878 method = "UNKNOWN";
3879
3880 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .sd.flg=0x%x .req.state=%s .res.state=%s",
3881 h1s, h1s->flags, se_fl_get(h1s->sd),
3882 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003883
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003884 if (pfx)
3885 chunk_appendf(msg, "\n%s", pfx);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003886
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003887 chunk_appendf(msg, " .meth=%s status=%d",
3888 method, h1s->status);
Christopher Faulet186367f2022-05-30 08:45:15 +02003889
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003890 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(h1s->sd));
3891 if (!se_fl_test(h1s->sd, SE_FL_ORPHAN))
3892 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
3893 h1s_sc(h1s)->flags, h1s_sc(h1s)->app);
Christopher Faulet186367f2022-05-30 08:45:15 +02003894
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003895 if (pfx && h1s->subs)
3896 chunk_appendf(msg, "\n%s", pfx);
3897
3898 chunk_appendf(msg, " .subs=%p", h1s->subs);
3899 if (h1s->subs) {
3900 chunk_appendf(msg, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3901 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
3902 h1s->subs->tasklet->calls,
3903 h1s->subs->tasklet->context);
3904 if (h1s->subs->tasklet->calls >= 1000000)
3905 ret = 1;
3906 resolve_sym_name(msg, NULL, h1s->subs->tasklet->process);
3907 chunk_appendf(msg, ")");
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003908 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003909 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003910}
3911
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003912/* for debugging with CLI's "show fd" command */
3913static int h1_show_fd(struct buffer *msg, struct connection *conn)
3914{
3915 struct h1c *h1c = conn->ctx;
3916 struct h1s *h1s = h1c->h1s;
3917 int ret = 0;
3918
3919 ret |= h1_dump_h1c_info(msg, h1c, NULL);
3920
3921 if (h1s)
3922 ret |= h1_dump_h1s_info(msg, h1s, NULL);
3923
3924 return ret;
3925}
3926
Willy Tarreaue6f389d2022-09-02 16:32:31 +02003927/* for debugging with CLI's "show sess" command. May emit multiple lines, each
3928 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
3929 * line is used. Each field starts with a space so it's safe to print it after
3930 * existing fields.
3931 */
3932static int h1_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
3933{
3934 struct h1s *h1s = sd->se;
3935 int ret = 0;
3936
3937 if (!h1s)
3938 return ret;
3939
3940 ret |= h1_dump_h1s_info(msg, h1s, pfx);
3941 if (pfx)
3942 chunk_appendf(msg, "\n%s", pfx);
3943 chunk_appendf(msg, " h1c=%p", h1s->h1c);
3944 ret |= h1_dump_h1c_info(msg, h1s->h1c, pfx);
3945 return ret;
3946}
3947
Christopher Faulet98fbe952019-07-22 16:18:24 +02003948
3949/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3950static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3951{
3952 struct h1_hdr_entry *entry;
3953
3954 /* Be sure there is a non-empty <to> */
3955 if (!strlen(to)) {
3956 memprintf(err, "expect <to>");
3957 return -1;
3958 }
3959
3960 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003961 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003962 memprintf(err, "<from> and <to> must not differ execpt the case");
3963 return -1;
3964 }
3965
3966 /* Be sure <from> does not already existsin the tree */
3967 if (ebis_lookup(&hdrs_map.map, from)) {
3968 memprintf(err, "duplicate entry '%s'", from);
3969 return -1;
3970 }
3971
3972 /* Create the entry and insert it in the tree */
3973 entry = malloc(sizeof(*entry));
3974 if (!entry) {
3975 memprintf(err, "out of memory");
3976 return -1;
3977 }
3978
3979 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01003980 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003981 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003982 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003983 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003984 free(entry);
3985 memprintf(err, "out of memory");
3986 return -1;
3987 }
3988 ebis_insert(&hdrs_map.map, &entry->node);
3989 return 0;
3990}
3991
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003992/* Migrate the the connection to the current thread.
3993 * Return 0 if successful, non-zero otherwise.
3994 * Expected to be called with the old thread lock held.
3995 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003996static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003997{
3998 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003999 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004000
4001 if (fd_takeover(conn->handle.fd, conn) != 0)
4002 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004003
4004 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4005 /* We failed to takeover the xprt, even if the connection may
4006 * still be valid, flag it as error'd, as we have already
4007 * taken over the fd, and wake the tasklet, so that it will
4008 * destroy it.
4009 */
4010 conn->flags |= CO_FL_ERROR;
4011 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
4012 return -1;
4013 }
4014
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004015 if (h1c->wait_event.events)
4016 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
4017 h1c->wait_event.events, &h1c->wait_event);
4018 /* To let the tasklet know it should free itself, and do nothing else,
4019 * set its context to NULL.
4020 */
4021 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004022 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004023
4024 task = h1c->task;
4025 if (task) {
4026 task->context = NULL;
4027 h1c->task = NULL;
4028 __ha_barrier_store();
4029 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004030
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004031 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004032 if (!h1c->task) {
4033 h1_release(h1c);
4034 return -1;
4035 }
4036 h1c->task->process = h1_timeout_task;
4037 h1c->task->context = h1c;
4038 }
4039 h1c->wait_event.tasklet = tasklet_new();
4040 if (!h1c->wait_event.tasklet) {
4041 h1_release(h1c);
4042 return -1;
4043 }
4044 h1c->wait_event.tasklet->process = h1_io_cb;
4045 h1c->wait_event.tasklet->context = h1c;
4046 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
4047 SUB_RETRY_RECV, &h1c->wait_event);
4048
4049 return 0;
4050}
4051
4052
Christopher Faulet98fbe952019-07-22 16:18:24 +02004053static void h1_hdeaders_case_adjust_deinit()
4054{
4055 struct ebpt_node *node, *next;
4056 struct h1_hdr_entry *entry;
4057
4058 node = ebpt_first(&hdrs_map.map);
4059 while (node) {
4060 next = ebpt_next(node);
4061 ebpt_delete(node);
4062 entry = container_of(node, struct h1_hdr_entry, node);
4063 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004064 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004065 free(entry);
4066 node = next;
4067 }
4068 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004069}
4070
Christopher Faulet98fbe952019-07-22 16:18:24 +02004071static int cfg_h1_headers_case_adjust_postparser()
4072{
4073 FILE *file = NULL;
4074 char *c, *key_beg, *key_end, *value_beg, *value_end;
4075 char *err;
4076 int rc, line = 0, err_code = 0;
4077
4078 if (!hdrs_map.name)
4079 goto end;
4080
4081 file = fopen(hdrs_map.name, "r");
4082 if (!file) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004083 ha_alert("h1-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004084 hdrs_map.name);
4085 err_code |= ERR_ALERT | ERR_FATAL;
4086 goto end;
4087 }
4088
4089 /* now parse all lines. The file may contain only two header name per
4090 * line, separated by spaces. All heading and trailing spaces will be
4091 * ignored. Lines starting with a # are ignored.
4092 */
4093 while (fgets(trash.area, trash.size, file) != NULL) {
4094 line++;
4095 c = trash.area;
4096
4097 /* strip leading spaces and tabs */
4098 while (*c == ' ' || *c == '\t')
4099 c++;
4100
4101 /* ignore emptu lines, or lines beginning with a dash */
4102 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
4103 continue;
4104
4105 /* look for the end of the key */
4106 key_beg = c;
4107 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
4108 c++;
4109 key_end = c;
4110
4111 /* strip middle spaces and tabs */
4112 while (*c == ' ' || *c == '\t')
4113 c++;
4114
4115 /* look for the end of the value, it is the end of the line */
4116 value_beg = c;
4117 while (*c && *c != '\n' && *c != '\r')
4118 c++;
4119 value_end = c;
4120
4121 /* trim possibly trailing spaces and tabs */
4122 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
4123 value_end--;
4124
4125 /* set final \0 and check entries */
4126 *key_end = '\0';
4127 *value_end = '\0';
4128
4129 err = NULL;
4130 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
4131 if (rc < 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004132 ha_alert("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004133 hdrs_map.name, err, line);
4134 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004135 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004136 goto end;
4137 }
4138 if (rc > 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004139 ha_warning("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004140 hdrs_map.name, err, line);
4141 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004142 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004143 }
4144 }
4145
4146 end:
4147 if (file)
4148 fclose(file);
4149 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
4150 return err_code;
4151}
4152
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004153/* config parser for global "h1-accept-payload_=-with-any-method" */
4154static int cfg_parse_h1_accept_payload_with_any_method(char **args, int section_type, struct proxy *curpx,
4155 const struct proxy *defpx, const char *file, int line,
4156 char **err)
4157{
4158 if (too_many_args(0, args, err, NULL))
4159 return -1;
4160 accept_payload_with_any_method = 1;
4161 return 0;
4162}
4163
Christopher Faulet98fbe952019-07-22 16:18:24 +02004164
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004165/* config parser for global "h1-header-case-adjust" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004166static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004167 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004168 char **err)
4169{
4170 if (too_many_args(2, args, err, NULL))
4171 return -1;
4172 if (!*(args[1]) || !*(args[2])) {
4173 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
4174 return -1;
4175 }
4176 return add_hdr_case_adjust(args[1], args[2], err);
4177}
4178
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004179/* config parser for global "h1-headers-case-adjust-file" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004180static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004181 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004182 char **err)
4183{
4184 if (too_many_args(1, args, err, NULL))
4185 return -1;
4186 if (!*(args[1])) {
4187 memprintf(err, "'%s' expects <file> as argument.", args[0]);
4188 return -1;
4189 }
4190 free(hdrs_map.name);
4191 hdrs_map.name = strdup(args[1]);
4192 return 0;
4193}
4194
Christopher Faulet98fbe952019-07-22 16:18:24 +02004195/* config keyword parsers */
4196static struct cfg_kw_list cfg_kws = {{ }, {
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004197 { CFG_GLOBAL, "h1-accept-payload-with-any-method", cfg_parse_h1_accept_payload_with_any_method },
Christopher Faulet98fbe952019-07-22 16:18:24 +02004198 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
4199 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
4200 { 0, NULL, NULL },
4201 }
4202};
4203
4204INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4205REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
4206
4207
Christopher Faulet51dbc942018-09-13 09:05:15 +02004208/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05004209/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02004210/****************************************/
4211
4212/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01004213static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02004214 .init = h1_init,
4215 .wake = h1_wake,
4216 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004217 .get_first_sc = h1_get_first_sc,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004218 .detach = h1_detach,
4219 .destroy = h1_destroy,
4220 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01004221 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004222 .rcv_buf = h1_rcv_buf,
4223 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02004224#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02004225 .rcv_pipe = h1_rcv_pipe,
4226 .snd_pipe = h1_snd_pipe,
4227#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02004228 .subscribe = h1_subscribe,
4229 .unsubscribe = h1_unsubscribe,
4230 .shutr = h1_shutr,
4231 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004232 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004233 .show_sd = h1_show_sd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004234 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004235 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02004236 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02004237 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02004238};
4239
Christopher Faulet3f612f72021-02-05 16:44:21 +01004240static const struct mux_ops mux_h1_ops = {
4241 .init = h1_init,
4242 .wake = h1_wake,
4243 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004244 .get_first_sc = h1_get_first_sc,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004245 .detach = h1_detach,
4246 .destroy = h1_destroy,
4247 .avail_streams = h1_avail_streams,
4248 .used_streams = h1_used_streams,
4249 .rcv_buf = h1_rcv_buf,
4250 .snd_buf = h1_snd_buf,
4251#if defined(USE_LINUX_SPLICE)
4252 .rcv_pipe = h1_rcv_pipe,
4253 .snd_pipe = h1_snd_pipe,
4254#endif
4255 .subscribe = h1_subscribe,
4256 .unsubscribe = h1_unsubscribe,
4257 .shutr = h1_shutr,
4258 .shutw = h1_shutw,
4259 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004260 .show_sd = h1_show_sd,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004261 .ctl = h1_ctl,
4262 .takeover = h1_takeover,
4263 .flags = MX_FL_HTX|MX_FL_NO_UPG,
4264 .name = "H1",
4265};
Christopher Faulet51dbc942018-09-13 09:05:15 +02004266
Christopher Faulet3f612f72021-02-05 16:44:21 +01004267/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
4268static struct mux_proto_list mux_proto_h1 =
4269 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
4270static struct mux_proto_list mux_proto_http =
4271 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02004272
Christopher Faulet3f612f72021-02-05 16:44:21 +01004273INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
4274INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01004275
Christopher Faulet51dbc942018-09-13 09:05:15 +02004276/*
4277 * Local variables:
4278 * c-indent-level: 8
4279 * c-basic-offset: 8
4280 * End:
4281 */