blob: 47b1da78064342de3e59d42f06187dcac828b30a [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;
Christopher Faulet089cc6e2022-10-04 11:24:46 +020038 struct h1s *h1s; /* H1 stream descriptor */
39 struct task *task; /* timeout management task */
40
Christopher Faulet51dbc942018-09-13 09:05:15 +020041 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletef93be22022-10-04 17:13:32 +020042 enum h1_cs state; /* Connection state */
43
Christopher Faulet089cc6e2022-10-04 11:24:46 +020044
Christopher Faulet51dbc942018-09-13 09:05:15 +020045 struct buffer ibuf; /* Input buffer to store data before parsing */
46 struct buffer obuf; /* Output buffer to store data after reformatting */
Christopher Faulet089cc6e2022-10-04 11:24:46 +020047 struct proxy *px;
Christopher Faulet51dbc942018-09-13 09:05:15 +020048
Christopher Faulet089cc6e2022-10-04 11:24:46 +020049 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +020050
Christopher Fauletdbe57792020-10-05 17:50:58 +020051 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
52 int timeout; /* client/server timeout duration */
53 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet089cc6e2022-10-04 11:24:46 +020054
55 struct h1_counters *px_counters; /* h1 counters attached to proxy */
56 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
57 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Christopher Faulet51dbc942018-09-13 09:05:15 +020058};
59
60/* H1 stream descriptor */
61struct h1s {
62 struct h1c *h1c;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +020063 struct sedesc *sd;
Christopher Fauletfeb11742018-11-29 15:12:34 +010064 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020065
Willy Tarreau4596fe22022-05-17 19:07:51 +020066 struct wait_event *subs; /* Address of the wait_event the stream connector associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020067
Olivier Houchardf502aca2018-12-14 19:42:40 +010068 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +020069 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +020070 struct h1m req;
71 struct h1m res;
72
Ilya Shipitsin47d17182020-06-21 21:42:57 +050073 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +020074 uint16_t status; /* HTTP response status */
Amaury Denoyellec1938232020-12-11 17:53:03 +010075
76 char ws_key[25]; /* websocket handshake key */
Christopher Faulet51dbc942018-09-13 09:05:15 +020077};
78
Christopher Faulet98fbe952019-07-22 16:18:24 +020079/* Map of headers used to convert outgoing headers */
80struct h1_hdrs_map {
81 char *name;
82 struct eb_root map;
83};
84
85/* An entry in a headers map */
86struct h1_hdr_entry {
87 struct ist name;
88 struct ebpt_node node;
89};
90
91/* Declare the headers map */
92static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
Christopher Faulet0f9c0f52022-05-13 09:20:13 +020093static int accept_payload_with_any_method = 0;
Christopher Faulet98fbe952019-07-22 16:18:24 +020094
Christopher Faulet6b81df72019-10-01 22:08:43 +020095/* trace source and events */
96static void h1_trace(enum trace_level level, uint64_t mask,
97 const struct trace_source *src,
98 const struct ist where, const struct ist func,
99 const void *a1, const void *a2, const void *a3, const void *a4);
100
101/* The event representation is split like this :
102 * h1c - internal H1 connection
103 * h1s - internal H1 stream
104 * strm - application layer
105 * rx - data receipt
106 * tx - data transmission
107 *
108 */
109static const struct trace_event h1_trace_events[] = {
110#define H1_EV_H1C_NEW (1ULL << 0)
111 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
112#define H1_EV_H1C_RECV (1ULL << 1)
113 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
114#define H1_EV_H1C_SEND (1ULL << 2)
115 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
116#define H1_EV_H1C_BLK (1ULL << 3)
117 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
118#define H1_EV_H1C_WAKE (1ULL << 4)
119 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
120#define H1_EV_H1C_END (1ULL << 5)
121 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
122#define H1_EV_H1C_ERR (1ULL << 6)
123 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
124
125#define H1_EV_RX_DATA (1ULL << 7)
126 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
127#define H1_EV_RX_EOI (1ULL << 8)
128 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
129#define H1_EV_RX_HDRS (1ULL << 9)
130 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
131#define H1_EV_RX_BODY (1ULL << 10)
132 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
133#define H1_EV_RX_TLRS (1ULL << 11)
134 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
135
136#define H1_EV_TX_DATA (1ULL << 12)
137 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
138#define H1_EV_TX_EOI (1ULL << 13)
139 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
140#define H1_EV_TX_HDRS (1ULL << 14)
141 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
142#define H1_EV_TX_BODY (1ULL << 15)
143 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
144#define H1_EV_TX_TLRS (1ULL << 16)
145 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
146
147#define H1_EV_H1S_NEW (1ULL << 17)
148 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
149#define H1_EV_H1S_BLK (1ULL << 18)
150 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
151#define H1_EV_H1S_END (1ULL << 19)
152 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
153#define H1_EV_H1S_ERR (1ULL << 20)
154 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
155
156#define H1_EV_STRM_NEW (1ULL << 21)
157 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
158#define H1_EV_STRM_RECV (1ULL << 22)
159 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
160#define H1_EV_STRM_SEND (1ULL << 23)
161 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
162#define H1_EV_STRM_WAKE (1ULL << 24)
163 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
164#define H1_EV_STRM_SHUT (1ULL << 25)
165 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
166#define H1_EV_STRM_END (1ULL << 26)
167 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
168#define H1_EV_STRM_ERR (1ULL << 27)
169 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
170
171 { }
172};
173
174static const struct name_desc h1_trace_lockon_args[4] = {
175 /* arg1 */ { /* already used by the connection */ },
176 /* arg2 */ { .name="h1s", .desc="H1 stream" },
177 /* arg3 */ { },
178 /* arg4 */ { }
179};
180
181static const struct name_desc h1_trace_decoding[] = {
182#define H1_VERB_CLEAN 1
183 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
184#define H1_VERB_MINIMAL 2
185 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
186#define H1_VERB_SIMPLE 3
187 { .name="simple", .desc="add request/response status line or htx info when available" },
188#define H1_VERB_ADVANCED 4
189 { .name="advanced", .desc="add header fields or frame decoding when available" },
190#define H1_VERB_COMPLETE 5
191 { .name="complete", .desc="add full data dump when available" },
192 { /* end */ }
193};
194
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200195static struct trace_source trace_h1 __read_mostly = {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200196 .name = IST("h1"),
197 .desc = "HTTP/1 multiplexer",
198 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
199 .default_cb = h1_trace,
200 .known_events = h1_trace_events,
201 .lockon_args = h1_trace_lockon_args,
202 .decoding = h1_trace_decoding,
203 .report_events = ~0, // report everything by default
204};
205
206#define TRACE_SOURCE &trace_h1
207INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
208
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100209
210/* h1 stats module */
211enum {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100212 H1_ST_OPEN_CONN,
213 H1_ST_OPEN_STREAM,
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100214 H1_ST_TOTAL_CONN,
215 H1_ST_TOTAL_STREAM,
216
Christopher Faulet41951ab2021-11-26 18:12:51 +0100217 H1_ST_BYTES_IN,
218 H1_ST_BYTES_OUT,
219#if defined(USE_LINUX_SPLICE)
220 H1_ST_SPLICED_BYTES_IN,
221 H1_ST_SPLICED_BYTES_OUT,
222#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100223 H1_STATS_COUNT /* must be the last member of the enum */
224};
225
226
227static struct name_desc h1_stats[] = {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100228 [H1_ST_OPEN_CONN] = { .name = "h1_open_connections",
229 .desc = "Count of currently open connections" },
230 [H1_ST_OPEN_STREAM] = { .name = "h1_open_streams",
231 .desc = "Count of currently open streams" },
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100232 [H1_ST_TOTAL_CONN] = { .name = "h1_total_connections",
233 .desc = "Total number of connections" },
234 [H1_ST_TOTAL_STREAM] = { .name = "h1_total_streams",
Christopher Faulet41951ab2021-11-26 18:12:51 +0100235 .desc = "Total number of streams" },
236
237 [H1_ST_BYTES_IN] = { .name = "h1_bytes_in",
238 .desc = "Total number of bytes received" },
239 [H1_ST_BYTES_OUT] = { .name = "h1_bytes_out",
240 .desc = "Total number of bytes send" },
241#if defined(USE_LINUX_SPLICE)
242 [H1_ST_SPLICED_BYTES_IN] = { .name = "h1_spliced_bytes_in",
243 .desc = "Total number of bytes received using kernel splicing" },
244 [H1_ST_SPLICED_BYTES_OUT] = { .name = "h1_spliced_bytes_out",
245 .desc = "Total number of bytes sendusing kernel splicing" },
246#endif
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100247
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100248};
249
250static struct h1_counters {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100251 long long open_conns; /* count of currently open connections */
252 long long open_streams; /* count of currently open streams */
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100253 long long total_conns; /* total number of connections */
254 long long total_streams; /* total number of streams */
255
Christopher Faulet41951ab2021-11-26 18:12:51 +0100256 long long bytes_in; /* number of bytes received */
257 long long bytes_out; /* number of bytes sent */
258#if defined(USE_LINUX_SPLICE)
259 long long spliced_bytes_in; /* number of bytes received using kernel splicing */
260 long long spliced_bytes_out; /* number of bytes sent using kernel splicing */
261#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100262} h1_counters;
263
264static void h1_fill_stats(void *data, struct field *stats)
265{
Christopher Faulet60fa0512021-11-26 18:10:39 +0100266 struct h1_counters *counters = data;
267
268 stats[H1_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
269 stats[H1_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100270 stats[H1_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
271 stats[H1_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
Christopher Faulet41951ab2021-11-26 18:12:51 +0100272
273 stats[H1_ST_BYTES_IN] = mkf_u64(FN_COUNTER, counters->bytes_in);
274 stats[H1_ST_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->bytes_out);
275#if defined(USE_LINUX_SPLICE)
276 stats[H1_ST_SPLICED_BYTES_IN] = mkf_u64(FN_COUNTER, counters->spliced_bytes_in);
277 stats[H1_ST_SPLICED_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->spliced_bytes_out);
278#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100279}
280
281static struct stats_module h1_stats_module = {
282 .name = "h1",
283 .fill_stats = h1_fill_stats,
284 .stats = h1_stats,
285 .stats_count = H1_STATS_COUNT,
286 .counters = &h1_counters,
287 .counters_size = sizeof(h1_counters),
288 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
289 .clearable = 1,
290};
291
292INITCALL1(STG_REGISTER, stats_register_module, &h1_stats_module);
293
294
Christopher Faulet51dbc942018-09-13 09:05:15 +0200295/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100296DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
297DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200298
Christopher Faulet51dbc942018-09-13 09:05:15 +0200299static int h1_recv(struct h1c *h1c);
300static int h1_send(struct h1c *h1c);
301static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100302/* h1_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100303struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state);
304struct task *h1_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Fauleta85c5222021-10-27 15:36:38 +0200305static void h1_shutw_conn(struct connection *conn);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200306static void h1_wake_stream_for_recv(struct h1s *h1s);
307static void h1_wake_stream_for_send(struct h1s *h1s);
Willy Tarreaua220e592023-03-21 10:44:44 +0100308static void h1s_destroy(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200309
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200310/* returns the stconn associated to the H1 stream */
311static forceinline struct stconn *h1s_sc(const struct h1s *h1s)
312{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200313 return h1s->sd->sc;
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200314}
315
Christopher Faulet6b81df72019-10-01 22:08:43 +0200316/* the H1 traces always expect that arg1, if non-null, is of type connection
317 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
318 * that arg3, if non-null, is a htx for rx/tx headers.
319 */
320static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
321 const struct ist where, const struct ist func,
322 const void *a1, const void *a2, const void *a3, const void *a4)
323{
324 const struct connection *conn = a1;
325 const struct h1c *h1c = conn ? conn->ctx : NULL;
326 const struct h1s *h1s = a2;
327 const struct htx *htx = a3;
328 const size_t *val = a4;
329
330 if (!h1c)
331 h1c = (h1s ? h1s->h1c : NULL);
332
333 if (!h1c || src->verbosity < H1_VERB_CLEAN)
334 return;
335
336 /* Display frontend/backend info by default */
Christopher Fauletef93be22022-10-04 17:13:32 +0200337 chunk_appendf(&trace_buf, " : [%c,%s]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'), h1c_st_to_str(h1c->state));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200338
339 /* Display request and response states if h1s is defined */
Christopher Faulet6580f282021-11-26 17:31:35 +0100340 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200341 chunk_appendf(&trace_buf, " [%s, %s]",
342 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
343
Christopher Faulet6580f282021-11-26 17:31:35 +0100344 if (src->verbosity > H1_VERB_SIMPLE) {
345 chunk_appendf(&trace_buf, " - req=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
346 h1s->req.flags, (unsigned long)h1s->req.curr_len, (unsigned long)h1s->req.body_len);
347 chunk_appendf(&trace_buf, " res=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
348 h1s->res.flags, (unsigned long)h1s->res.curr_len, (unsigned long)h1s->res.body_len);
349 }
350
351 }
352
Christopher Faulet6b81df72019-10-01 22:08:43 +0200353 if (src->verbosity == H1_VERB_CLEAN)
354 return;
355
356 /* Display the value to the 4th argument (level > STATE) */
357 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100358 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200359
360 /* Display status-line if possible (verbosity > MINIMAL) */
361 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
362 const struct htx_blk *blk = htx_get_head_blk(htx);
363 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
364 enum htx_blk_type type = htx_get_blk_type(blk);
365
366 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
367 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
368 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
369 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
370 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
371 }
372
373 /* Display h1c info and, if defined, h1s info (pointer + flags) */
374 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100375 if (h1c->conn)
376 chunk_appendf(&trace_buf, " conn=%p(0x%08x)", h1c->conn, h1c->conn->flags);
377 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200378 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200379 if (h1s->sd)
380 chunk_appendf(&trace_buf, " sd=%p(0x%08x)", h1s->sd, se_fl_get(h1s->sd));
381 if (h1s->sd && h1s_sc(h1s))
Willy Tarreau000d63c2022-05-27 10:39:17 +0200382 chunk_appendf(&trace_buf, " sc=%p(0x%08x)", h1s_sc(h1s), h1s_sc(h1s)->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100383 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200384
385 if (src->verbosity == H1_VERB_MINIMAL)
386 return;
387
388 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
389 if (src->level > TRACE_LEVEL_USER) {
390 if (src->verbosity == H1_VERB_COMPLETE ||
391 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
392 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
393 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
394 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
395 if (src->verbosity == H1_VERB_COMPLETE ||
396 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
397 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
398 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
399 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
400 }
401
402 /* Display htx info if defined (level > USER) */
403 if (src->level > TRACE_LEVEL_USER && htx) {
404 int full = 0;
405
406 /* Full htx info (level > STATE && verbosity > SIMPLE) */
407 if (src->level > TRACE_LEVEL_STATE) {
408 if (src->verbosity == H1_VERB_COMPLETE)
409 full = 1;
410 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
411 full = 1;
412 }
413
414 chunk_memcat(&trace_buf, "\n\t", 2);
415 htx_dump(&trace_buf, htx, full);
416 }
417}
418
419
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420/*****************************************************/
421/* functions below are for dynamic buffer management */
422/*****************************************************/
423/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100424 * Indicates whether or not we may receive data. The rules are the following :
Christopher Fauletfc473a62022-10-05 08:22:33 +0200425 * - if an error or a shutdown for reads was detected on the H1 connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200426 * must not attempt to receive
427 * - if we are waiting for the connection establishment, we must not attempt
428 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200429 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100430 * - if the input buffer failed to be allocated or is full , we must not try
431 * to receive
Christopher Fauletfc473a62022-10-05 08:22:33 +0200432 * - if the mux is blocked on an input condition, we must may not attempt to
433 * receive
434 * - otherwise we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200435 */
436static inline int h1_recv_allowed(const struct h1c *h1c)
437{
Christopher Fauletfc473a62022-10-05 08:22:33 +0200438 if (h1c->flags & (H1C_F_EOS|H1C_F_ERROR)) {
439 TRACE_DEVEL("recv not allowed because of (eos|error) on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200440 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200441 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200442
Christopher Fauletfc473a62022-10-05 08:22:33 +0200443 if (h1c->conn->flags & (CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
444 TRACE_DEVEL("recv not allowed because of (waitl4|waitl6) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletcf56b992018-12-11 16:12:31 +0100445 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200446 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100447
Christopher Fauletfc473a62022-10-05 08:22:33 +0200448 if ((h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC))) {
449 TRACE_DEVEL("recv not allowed because input is blocked", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet119ac872020-09-30 17:33:22 +0200450 return 0;
451 }
452
Christopher Fauletfc473a62022-10-05 08:22:33 +0200453 return 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200454}
455
456/*
457 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
458 * flags are used to figure what buffer was requested. It returns 1 if the
459 * allocation succeeds, in which case the connection is woken up, or 0 if it's
460 * impossible to wake up and we prefer to be woken up later.
461 */
462static int h1_buf_available(void *target)
463{
464 struct h1c *h1c = target;
465
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100466 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200467 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 +0200468 h1c->flags &= ~H1C_F_IN_ALLOC;
469 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200470 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200471 return 1;
472 }
473
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100474 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200475 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 +0200476 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200477 if (h1c->h1s)
478 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200479 return 1;
480 }
481
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100482 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200483 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
484 h1c->flags &= ~H1C_F_IN_SALLOC;
485 tasklet_wakeup(h1c->wait_event.tasklet);
486 return 1;
487 }
488
Christopher Faulet51dbc942018-09-13 09:05:15 +0200489 return 0;
490}
491
492/*
493 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
494 */
495static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
496{
497 struct buffer *buf = NULL;
498
Willy Tarreau2b718102021-04-21 07:32:39 +0200499 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100500 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200501 h1c->buf_wait.target = h1c;
502 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200503 LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200504 }
505 return buf;
506}
507
508/*
509 * Release a buffer, if any, and try to wake up entities waiting in the buffer
510 * wait queue.
511 */
512static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
513{
514 if (bptr->size) {
515 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100516 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200517 }
518}
519
Christopher Fauletef93be22022-10-04 17:13:32 +0200520/* Returns 1 if the H1 connection is alive (IDLE, EMBRYONIC, RUNNING or
521 * RUNNING). Ortherwise 0 is returned.
522 */
523static inline int h1_is_alive(const struct h1c *h1c)
524{
525 return (h1c->state <= H1_CS_RUNNING);
526}
527
528/* Switch the H1 connection to CLOSING or CLOSED mode, depending on the output
529 * buffer state and if there is still a H1 stream or not. If there are sill
530 * pending outgoing data or if there is still a H1 stream, it is set to CLOSING
531 * state. Otherwise it is set to CLOSED mode. */
532static inline void h1_close(struct h1c *h1c)
533{
534 h1c->state = ((h1c->h1s || b_data(&h1c->obuf)) ? H1_CS_CLOSING : H1_CS_CLOSED);
535}
536
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100537/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet4e72b172022-10-04 17:35:19 +0200538 * or not. We rely on H1C state to know if the connection is in-use or not. It
539 * is IDLE only when no H1 stream is attached and when the previous stream, if
540 * any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100541 */
542static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200543{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100544 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200545
Christopher Faulet4e72b172022-10-04 17:35:19 +0200546 return ((h1c->state == H1_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200547}
548
Willy Tarreau00f18a32019-01-26 12:19:01 +0100549/* returns the number of streams still available on a connection */
550static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100551{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100552 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100553}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200554
Christopher Faulet7a145d62020-08-05 11:31:16 +0200555/* Refresh the h1c task timeout if necessary */
556static void h1_refresh_timeout(struct h1c *h1c)
557{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200558 int is_idle_conn = 0;
559
Christopher Faulet7a145d62020-08-05 11:31:16 +0200560 if (h1c->task) {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200561 if (!h1_is_alive(h1c)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200562 /* half-closed or dead connections : switch to clientfin/serverfin
563 * timeouts so that we don't hang too long on clients that have
564 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200565 */
566 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200567 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 +0200568 is_idle_conn = 1;
Christopher Fauletadcd7892020-10-05 17:13:05 +0200569 }
570 else if (b_data(&h1c->obuf)) {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200571 /* alive connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200572 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200573 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
574 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200575 else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->state != H1_CS_RUNNING)) {
576 /* alive front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200577 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100578 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 +0200579 /* A frontend connection not yet ready could be treated the same way as an idle
580 * one in case of soft-close.
581 */
582 is_idle_conn = 1;
Christopher Faulet7a145d62020-08-05 11:31:16 +0200583 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200584 else {
Willy Tarreau4596fe22022-05-17 19:07:51 +0200585 /* alive back connections of front connections with a stream connector attached */
Christopher Fauletadcd7892020-10-05 17:13:05 +0200586 h1c->task->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200587 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 +0200588 }
589
Christopher Fauletdbe57792020-10-05 17:50:58 +0200590 /* Finally set the idle expiration date if shorter */
591 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200592
593 if ((h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
594 is_idle_conn && tick_isset(global.close_spread_end)) {
595 /* If a soft-stop is in progress and a close-spread-time
596 * is set, we want to spread idle connection closing roughly
597 * evenly across the defined window. This should only
598 * act on idle frontend connections.
599 * If the window end is already in the past, we wake the
600 * timeout task up immediately so that it can be closed.
601 */
602 int remaining_window = tick_remain(now_ms, global.close_spread_end);
603 if (remaining_window) {
604 /* We don't need to reset the expire if it would
605 * already happen before the close window end.
606 */
607 if (tick_is_le(global.close_spread_end, h1c->task->expire)) {
608 /* Set an expire value shorter than the current value
609 * because the close spread window end comes earlier.
610 */
611 h1c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
612 TRACE_DEVEL("connection timeout set to value before close-spread window end", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
613 }
614 }
615 else {
616 /* We are past the soft close window end, wake the timeout
617 * task up immediately.
618 */
619 task_wakeup(h1c->task, TASK_WOKEN_TIMER);
620 }
621 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200622 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
623 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200624 }
625}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200626
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200627static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200628{
629 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
630 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
631 h1c->idle_exp = TICK_ETERNITY;
632 return;
633 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200634 if (h1c->state == H1_CS_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200635 if (!tick_isset(h1c->idle_exp)) {
636 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
637 !b_data(&h1c->ibuf) && /* No input data */
638 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
639 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
640 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
641 }
642 else {
643 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
644 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
645 }
646 }
647 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200648 else if (h1c->state < H1_CS_RUNNING) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200649 if (!tick_isset(h1c->idle_exp)) {
650 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
651 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
652 }
653 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200654 else {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200655 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200656 TRACE_DEVEL("unset idle expiration (running or closing)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletdbe57792020-10-05 17:50:58 +0200657 }
658}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200659/*****************************************************************/
660/* functions below are dedicated to the mux setup and management */
661/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200662
663/* returns non-zero if there are input data pending for stream h1s. */
664static inline size_t h1s_data_pending(const struct h1s *h1s)
665{
666 const struct h1m *h1m;
667
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200668 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100669 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200670}
671
Willy Tarreau4596fe22022-05-17 19:07:51 +0200672/* Creates a new stream connector and the associate stream. <input> is used as input
Christopher Faulet16df1782020-12-04 16:47:41 +0100673 * buffer for the stream. On success, it is transferred to the stream and the
674 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
675 * mux must still take care of it. However, there is nothing special to do
676 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
Willy Tarreau4596fe22022-05-17 19:07:51 +0200677 * b_free() on it is always safe. This function returns the stream connector on
Christopher Faulet16df1782020-12-04 16:47:41 +0100678 * success or NULL on error. */
Willy Tarreau000d63c2022-05-27 10:39:17 +0200679static struct stconn *h1s_new_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100680{
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100681 struct h1c *h1c = h1s->h1c;
Christopher Faulet47365272018-10-31 17:40:50 +0100682
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100683 TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100684
Christopher Fauletb669d682022-03-22 18:37:19 +0100685 if (h1s->flags & H1S_F_NOT_FIRST)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200686 se_fl_set(h1s->sd, SE_FL_NOT_FIRST);
Christopher Fauletb669d682022-03-22 18:37:19 +0100687 if (h1s->req.flags & H1_MF_UPG_WEBSOCKET)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200688 se_fl_set(h1s->sd, SE_FL_WEBSOCKET);
Christopher Fauletb669d682022-03-22 18:37:19 +0100689
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200690 if (!sc_new_from_endp(h1s->sd, h1c->conn->owner, input)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +0200691 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 +0100692 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200693 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200694
Christopher Faulet4e72b172022-10-04 17:35:19 +0200695 h1c->state = H1_CS_RUNNING;
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100696 TRACE_LEAVE(H1_EV_STRM_NEW, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200697 return h1s_sc(h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100698
Christopher Faulet91449b02022-03-22 18:45:55 +0100699 err:
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100700 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100701 return NULL;
702}
703
Willy Tarreau000d63c2022-05-27 10:39:17 +0200704static struct stconn *h1s_upgrade_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100705{
706 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
707
Willy Tarreaudf1a2fc2022-05-27 11:11:15 +0200708 if (stream_upgrade_from_sc(h1s_sc(h1s), input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100709 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 +0100710 goto err;
711 }
712
Christopher Faulet4e72b172022-10-04 17:35:19 +0200713 h1s->h1c->state = H1_CS_RUNNING;
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100714 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200715 return h1s_sc(h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100716
717 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100718 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100719 return NULL;
720}
721
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200722static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200723{
724 struct h1s *h1s;
725
Christopher Faulet6b81df72019-10-01 22:08:43 +0200726 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
727
Christopher Faulet51dbc942018-09-13 09:05:15 +0200728 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100729 if (!h1s) {
730 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 +0100731 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100732 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200733 h1s->h1c = h1c;
734 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200735 h1s->sess = NULL;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200736 h1s->sd = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100737 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100738 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200739 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100740 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200741
742 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100743 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200744
Christopher Faulet129817b2018-09-20 16:14:40 +0200745 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100746 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200747
748 h1s->status = 0;
749 h1s->meth = HTTP_METH_OTHER;
750
Christopher Faulet47365272018-10-31 17:40:50 +0100751 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
752 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200753 h1s->h1c->state = H1_CS_EMBRYONIC;
Christopher Faulet4427ea72022-11-23 15:58:59 +0100754 h1s->h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200755 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
756 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200757
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200758 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100759 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200760 return NULL;
761}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100762
Willy Tarreau000d63c2022-05-27 10:39:17 +0200763static struct h1s *h1c_frt_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200764{
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200765 struct h1s *h1s;
766
767 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
768
769 h1s = h1s_new(h1c);
770 if (!h1s)
771 goto fail;
772
Willy Tarreau000d63c2022-05-27 10:39:17 +0200773 if (sc) {
774 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200775 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200776 h1s->sd = sc->sedesc;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100777 }
778 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200779 h1s->sd = sedesc_new();
780 if (!h1s->sd)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100781 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200782 h1s->sd->se = h1s;
783 h1s->sd->conn = h1c->conn;
784 se_fl_set(h1s->sd, SE_FL_T_MUX | SE_FL_ORPHAN);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100785 }
Christopher Fauletf4b89f12023-02-23 13:58:13 +0100786 /* When a request starts, the H1S does not expect data while the request
787 * is not finished. It does not mean the response must not be received,
788 * especially if headers were already forwarded. But it is not
789 * mandatory.
790 */
791 se_expect_no_data(h1s->sd);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200792 h1s->sess = sess;
793
794 if (h1c->px->options2 & PR_O2_REQBUG_OK)
795 h1s->req.err_pos = -1;
796
Christopher Fauletaf5336f2022-09-12 07:46:11 +0200797 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
798 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
799
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200800 h1c->idle_exp = TICK_ETERNITY;
801 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200802 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200803 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100804
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200805 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100806 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Willy Tarreaua220e592023-03-21 10:44:44 +0100807 h1s_destroy(h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200808 return NULL;
809}
810
Willy Tarreau000d63c2022-05-27 10:39:17 +0200811static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200812{
813 struct h1s *h1s;
814
815 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
816
817 h1s = h1s_new(h1c);
818 if (!h1s)
819 goto fail;
820
Willy Tarreau000d63c2022-05-27 10:39:17 +0200821 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200822 goto fail;
823
Christopher Faulet10a86702021-04-07 14:18:11 +0200824 h1s->flags |= H1S_F_RX_BLK;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200825 h1s->sd = sc->sedesc;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200826 h1s->sess = sess;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200827
Christopher Faulet4e72b172022-10-04 17:35:19 +0200828 h1c->state = H1_CS_RUNNING;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200829
830 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
831 h1s->res.err_pos = -1;
832
Christopher Faulet60fa0512021-11-26 18:10:39 +0100833 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100834 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100835
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200836 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
837 return h1s;
838
839 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100840 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Willy Tarreaua220e592023-03-21 10:44:44 +0100841 h1s_destroy(h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100842 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200843}
844
845static void h1s_destroy(struct h1s *h1s)
846{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200847 if (h1s) {
848 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200849
Christopher Faulet6b81df72019-10-01 22:08:43 +0200850 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200851 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200852
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100853 if (h1s->subs)
854 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200855
Christopher Fauletd17ad822020-09-24 15:14:29 +0200856 h1_release_buf(h1c, &h1s->rxbuf);
857
Christopher Faulet10a86702021-04-07 14:18:11 +0200858 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200859 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
860 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100861
Christopher Faulet31da34d2022-10-10 16:36:10 +0200862 if (!(h1c->flags & (H1C_F_EOS|H1C_F_ERR_PENDING|H1C_F_ERROR|H1C_F_ABRT_PENDING|H1C_F_ABRTED)) && /* No error/read0/abort */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200863 h1_is_alive(h1c) && /* still alive */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100864 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100865 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200866 h1c->state = H1_CS_IDLE;
867 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100868 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
869 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200870 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200871 h1_close(h1c);
872 TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200873 }
Christopher Faulet60fa0512021-11-26 18:10:39 +0100874
875 HA_ATOMIC_DEC(&h1c->px_counters->open_streams);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200876 BUG_ON(h1s->sd && !se_fl_test(h1s->sd, SE_FL_ORPHAN));
877 sedesc_free(h1s->sd);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878 pool_free(pool_head_h1s, h1s);
879 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200880}
881
882/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200883 * Initialize the mux once it's attached. It is expected that conn->ctx points
Willy Tarreau4596fe22022-05-17 19:07:51 +0200884 * to the existing stream connector (for outgoing connections or for incoming
885 * ones during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200886 * establishment). <input> is always used as Input buffer and may contain
887 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
888 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200889 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200890static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
891 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200892{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200893 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100894 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200895 void *conn_ctx = conn->ctx;
896
897 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200898
899 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100900 if (!h1c) {
901 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200902 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100903 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200904 h1c->conn = conn;
905 h1c->px = proxy;
906
Christopher Fauletef93be22022-10-04 17:13:32 +0200907 h1c->state = H1_CS_IDLE;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200908 h1c->flags = H1C_F_NONE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200909 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200910 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200911 h1c->obuf = BUF_NULL;
912 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200913 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200914
Willy Tarreau90f366b2021-02-20 11:49:49 +0100915 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200916 h1c->wait_event.tasklet = tasklet_new();
917 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200918 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200919 h1c->wait_event.tasklet->process = h1_io_cb;
920 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100921 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200922 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200923
Christopher Faulete9b70722019-04-08 10:46:02 +0200924 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200925 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100926 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
927 if (tick_isset(proxy->timeout.serverfin))
928 h1c->shut_timeout = proxy->timeout.serverfin;
Christopher Faulet563c3452021-11-26 17:37:51 +0100929
930 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be,
931 &h1_stats_module);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100932 } else {
933 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
934 if (tick_isset(proxy->timeout.clientfin))
935 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200936
Christopher Faulet563c3452021-11-26 17:37:51 +0100937 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe,
938 &h1_stats_module);
939
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200940 LIST_APPEND(&mux_stopping_data[tid].list,
941 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100942 }
943 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200944 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100945 if (!t) {
946 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 +0100947 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100948 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100949
950 h1c->task = t;
951 t->process = h1_timeout_task;
952 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200953
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100954 t->expire = tick_add(now_ms, h1c->timeout);
955 }
956
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100957 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200958
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200959 if (h1c->flags & H1C_F_IS_BACK) {
960 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200961 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
962 goto fail;
963 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100964 else if (conn_ctx) {
965 /* Upgraded frontend connection (from TCP) */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100966 if (!h1c_frt_stream_new(h1c, conn_ctx, h1c->conn->owner))
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100967 goto fail;
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100968
Willy Tarreaue68bc612022-05-27 11:23:05 +0200969 /* Attach the SC but Not ready yet */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200970 h1c->state = H1_CS_UPGRADING;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200971 TRACE_DEVEL("Inherit the SC from TCP connection to perform an upgrade",
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100972 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
973 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100974
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200975 if (t) {
976 h1_set_idle_expiration(h1c);
977 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100978 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200979 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100980
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200981 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100982 if (b_data(&h1c->ibuf))
983 tasklet_wakeup(h1c->wait_event.tasklet);
984 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200985 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200986
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100987 if (!conn_is_back(conn))
988 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, proxy, 1);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100989 HA_ATOMIC_INC(&h1c->px_counters->open_conns);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100990 HA_ATOMIC_INC(&h1c->px_counters->total_conns);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100991
Christopher Faulet51dbc942018-09-13 09:05:15 +0200992 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200993 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200994 return 0;
995
996 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200997 task_destroy(t);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200998 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200999 pool_free(pool_head_h1c, h1c);
1000 fail_h1c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +01001001 if (!conn_is_back(conn))
1002 LIST_DEL_INIT(&conn->stopping_list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001003 conn->ctx = conn_ctx; // restore saved context
1004 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001005 return -1;
1006}
1007
Christopher Faulet73c12072019-04-08 11:23:22 +02001008/* release function. This one should be called to free all resources allocated
1009 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +02001010 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001011static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001012{
Christopher Faulet61840e72019-04-15 09:33:32 +02001013 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001014
Christopher Faulet6b81df72019-10-01 22:08:43 +02001015 TRACE_POINT(H1_EV_H1C_END);
1016
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001017 /* The connection must be aattached to this mux to be released */
1018 if (h1c->conn && h1c->conn->ctx == h1c)
1019 conn = h1c->conn;
Christopher Faulet61840e72019-04-15 09:33:32 +02001020
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001021 if (conn && h1c->flags & H1C_F_UPG_H2C) {
1022 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
1023 /* Make sure we're no longer subscribed to anything */
1024 if (h1c->wait_event.events)
1025 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1026 h1c->wait_event.events, &h1c->wait_event);
1027 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
1028 /* connection successfully upgraded to H2, this
1029 * mux was already released */
1030 return;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001031 }
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001032 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
1033 sess_log(conn->owner); /* Log if the upgrade failed */
1034 }
Olivier Houchard45c44372019-06-11 14:06:23 +02001035
Christopher Faulet51dbc942018-09-13 09:05:15 +02001036
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001037 if (LIST_INLIST(&h1c->buf_wait.list))
1038 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001039
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001040 h1_release_buf(h1c, &h1c->ibuf);
1041 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001042
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001043 if (h1c->task) {
1044 h1c->task->context = NULL;
1045 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
1046 h1c->task = NULL;
1047 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001048
Christopher Faulet551b8962023-03-24 09:26:16 +01001049 if (h1c->wait_event.tasklet) {
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001050 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet551b8962023-03-24 09:26:16 +01001051 h1c->wait_event.tasklet = NULL;
1052 }
Christopher Faulet60fa0512021-11-26 18:10:39 +01001053
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001054 h1s_destroy(h1c->h1s);
1055 if (conn) {
1056 if (h1c->wait_event.events != 0)
1057 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
1058 &h1c->wait_event);
1059 h1_shutw_conn(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001060 }
1061
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001062 HA_ATOMIC_DEC(&h1c->px_counters->open_conns);
1063 pool_free(pool_head_h1c, h1c);
1064
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001065 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001066 if (!conn_is_back(conn))
1067 LIST_DEL_INIT(&conn->stopping_list);
1068
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001069 conn->mux = NULL;
1070 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001071 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001072
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001073 conn_stop_tracking(conn);
1074 conn_full_close(conn);
1075 if (conn->destroy_cb)
1076 conn->destroy_cb(conn);
1077 conn_free(conn);
1078 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001079}
1080
1081/******************************************************/
1082/* functions below are for the H1 protocol processing */
1083/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +02001084/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
1085 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +02001086 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001087static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001088{
Christopher Faulet570d1612018-11-26 11:13:57 +01001089 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001090
Christopher Faulet570d1612018-11-26 11:13:57 +01001091 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 (*(p + 5) > '1' ||
1093 (*(p + 5) == '1' && *(p + 7) >= '1')))
1094 h1m->flags |= H1_MF_VER_11;
1095}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001096
Christopher Faulet9768c262018-10-22 09:34:31 +02001097/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
1098 * greater or equal to 1.1
1099 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001100static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +02001101{
Christopher Faulet570d1612018-11-26 11:13:57 +01001102 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001103
Christopher Faulet570d1612018-11-26 11:13:57 +01001104 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001105 (*(p + 5) > '1' ||
1106 (*(p + 5) == '1' && *(p + 7) >= '1')))
1107 h1m->flags |= H1_MF_VER_11;
1108}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001109
Christopher Fauletf2824e62018-10-01 12:12:37 +02001110/* Deduce the connection mode of the client connection, depending on the
1111 * configuration and the H1 message flags. This function is called twice, the
1112 * first time when the request is parsed and the second time when the response
1113 * is parsed.
1114 */
1115static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
1116{
1117 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001118
1119 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001120 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001121 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001122 h1s->status == 101) {
1123 /* Either we've established an explicit tunnel, or we're
1124 * switching the protocol. In both cases, we're very unlikely to
1125 * understand the next protocols. We have to switch to tunnel
1126 * mode, so that we transfer the request and responses then let
1127 * this protocol pass unmodified. When we later implement
1128 * specific parsers for such protocols, we'll want to check the
1129 * Upgrade header which contains information about that protocol
1130 * for responses with status 101 (eg: see RFC2817 about TLS).
1131 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001132 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001133 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 +01001134 }
1135 else if (h1s->flags & H1S_F_WANT_KAL) {
1136 /* By default the client is in KAL mode. CLOSE mode mean
1137 * it is imposed by the client itself. So only change
1138 * KAL mode here. */
1139 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1140 /* no length known or explicit close => close */
1141 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001142 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 +01001143 }
1144 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1145 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001146 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001147 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001148 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 +01001149 }
1150 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001151 }
1152 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001153 /* Input direction: first pass */
1154 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1155 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001156 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001157 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 +01001158 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001159 }
1160
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001161 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001162 * unless a 'close-spread-time' option is set (either to define a
1163 * soft-close window or to disable active closing (close-spread-time
1164 * option set to 0).
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001165 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001166 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001167 int want_clo = 1;
1168 /* If a close-spread-time option is set, we want to avoid
1169 * closing all the active HTTP connections at once so we add a
1170 * random factor that will spread the closing.
1171 */
1172 if (tick_isset(global.close_spread_end)) {
1173 int remaining_window = tick_remain(now_ms, global.close_spread_end);
1174 if (remaining_window) {
1175 /* This should increase the closing rate the further along
1176 * the window we are.
1177 */
1178 want_clo = (remaining_window <= statistical_prng_range(global.close_spread_time));
1179 }
1180 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001181 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
1182 want_clo = 0;
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001183
1184 if (want_clo) {
1185 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1186 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);
1187 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001188 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001189}
1190
1191/* Deduce the connection mode of the client connection, depending on the
1192 * configuration and the H1 message flags. This function is called twice, the
1193 * first time when the request is parsed and the second time when the response
1194 * is parsed.
1195 */
1196static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1197{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001198 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001199 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001200 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001201
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001203 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001204 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001205 h1s->status == 101) {
1206 /* Either we've established an explicit tunnel, or we're
1207 * switching the protocol. In both cases, we're very unlikely to
1208 * understand the next protocols. We have to switch to tunnel
1209 * mode, so that we transfer the request and responses then let
1210 * this protocol pass unmodified. When we later implement
1211 * specific parsers for such protocols, we'll want to check the
1212 * Upgrade header which contains information about that protocol
1213 * for responses with status 101 (eg: see RFC2817 about TLS).
1214 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001215 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001216 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 +01001217 }
1218 else if (h1s->flags & H1S_F_WANT_KAL) {
1219 /* By default the server is in KAL mode. CLOSE mode mean
1220 * it is imposed by haproxy itself. So only change KAL
1221 * mode here. */
1222 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1223 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1224 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1225 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001226 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 +01001227 }
1228 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001229 }
Christopher Faulet70033782018-12-05 13:50:11 +01001230 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001231 /* Output direction: first pass */
1232 if (h1m->flags & H1_MF_CONN_CLO) {
1233 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001234 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001235 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 +01001236 }
1237 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1238 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1239 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001240 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1241 /* no explicit keep-alive option httpclose/server-close => close */
1242 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001243 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 +01001244 }
Christopher Faulet70033782018-12-05 13:50:11 +01001245 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001246
1247 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001248 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001249 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001250 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);
1251 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001252}
1253
Christopher Fauletb992af02019-03-28 15:42:24 +01001254static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001255{
1256 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001257
1258 /* 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 || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001265 if (!(h1m->flags & H1_MF_VER_11)) {
1266 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 +01001267 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001268 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001269 }
1270 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001271 if (h1m->flags & H1_MF_VER_11) {
1272 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001273 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001274 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001275 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001276}
1277
Christopher Fauletb992af02019-03-28 15:42:24 +01001278static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001279{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001280 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1281 * token is found
1282 */
1283 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001284 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001285
1286 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001287 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001288 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1289 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 +01001290 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001291 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001292 }
1293 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001294 if (h1m->flags & H1_MF_VER_11) {
1295 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001296 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001297 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001298 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001299}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001300
Christopher Fauletb992af02019-03-28 15:42:24 +01001301static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001302{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001303 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001305 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001306 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001307}
1308
Christopher Fauletb992af02019-03-28 15:42:24 +01001309static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1310{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001311 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001312 h1_set_cli_conn_mode(h1s, h1m);
1313 else
1314 h1_set_srv_conn_mode(h1s, h1m);
1315
1316 if (!(h1m->flags & H1_MF_RESP))
1317 h1_update_req_conn_value(h1s, h1m, conn_val);
1318 else
1319 h1_update_res_conn_value(h1s, h1m, conn_val);
1320}
Christopher Faulete44769b2018-11-29 23:01:45 +01001321
Christopher Faulet98fbe952019-07-22 16:18:24 +02001322/* Try to adjust the case of the message header name using the global map
1323 * <hdrs_map>.
1324 */
1325static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1326{
1327 struct ebpt_node *node;
1328 struct h1_hdr_entry *entry;
1329
1330 /* No entry in the map, do nothing */
1331 if (eb_is_empty(&hdrs_map.map))
1332 return;
1333
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001334 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001335 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1336 return;
1337
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001338 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001339 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1340 return;
1341
1342 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1343 if (!node)
1344 return;
1345 entry = container_of(node, struct h1_hdr_entry, node);
1346 name->ptr = entry->name.ptr;
1347 name->len = entry->name.len;
1348}
1349
Christopher Faulete44769b2018-11-29 23:01:45 +01001350/* Append the description of what is present in error snapshot <es> into <out>.
1351 * The description must be small enough to always fit in a buffer. The output
1352 * buffer may be the trash so the trash must not be used inside this function.
1353 */
1354static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1355{
1356 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001357 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1358 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1359 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1360 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1361 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1362 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001363}
1364/*
1365 * Capture a bad request or response and archive it in the proxy's structure.
1366 * By default it tries to report the error position as h1m->err_pos. However if
1367 * this one is not set, it will then report h1m->next, which is the last known
1368 * parsing point. The function is able to deal with wrapping buffers. It always
1369 * displays buffers as a contiguous area starting at buf->p. The direction is
1370 * determined thanks to the h1m's flags.
1371 */
1372static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1373 struct h1m *h1m, struct buffer *buf)
1374{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001375 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001376 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001377 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001378 union error_snapshot_ctx ctx;
1379
Christopher Faulet4e72b172022-10-04 17:35:19 +02001380 if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001381 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02001382 sess = __sc_strm(h1s_sc(h1s))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001383 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02001384 other_end = __sc_strm(h1s_sc(h1s))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001385 else
1386 other_end = sess->fe;
1387 } else
1388 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001389
1390 /* http-specific part now */
1391 ctx.h1.state = h1m->state;
1392 ctx.h1.c_flags = h1c->flags;
1393 ctx.h1.s_flags = h1s->flags;
1394 ctx.h1.m_flags = h1m->flags;
1395 ctx.h1.m_clen = h1m->curr_len;
1396 ctx.h1.m_blen = h1m->body_len;
1397
1398 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1399 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001400 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1401 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001402}
1403
Christopher Fauletadb22202018-12-12 10:32:09 +01001404/* Emit the chunksize followed by a CRLF in front of data of the buffer
1405 * <buf>. It goes backwards and starts with the byte before the buffer's
1406 * head. The caller is responsible for ensuring there is enough room left before
1407 * the buffer's head for the string.
1408 */
1409static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1410{
1411 char *beg, *end;
1412
1413 beg = end = b_head(buf);
1414 *--beg = '\n';
1415 *--beg = '\r';
1416 do {
1417 *--beg = hextab[chksz & 0xF];
1418 } while (chksz >>= 4);
1419 buf->head -= (end - beg);
1420 b_add(buf, end - beg);
1421}
1422
1423/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1424 * ensuring there is enough room left in the buffer for the string. */
1425static void h1_emit_chunk_crlf(struct buffer *buf)
1426{
1427 *(b_peek(buf, b_data(buf))) = '\r';
1428 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1429 b_add(buf, 2);
1430}
1431
Christopher Faulet129817b2018-09-20 16:14:40 +02001432/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001433 * Switch the stream to tunnel mode. This function must only be called on 2xx
1434 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001435 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001436static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001437{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001438 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001439
Christopher Faulet5be651d2021-01-22 15:28:03 +01001440 h1s->req.state = H1_MSG_TUNNEL;
1441 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001442
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001443 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001444 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001445
Christopher Faulet5be651d2021-01-22 15:28:03 +01001446 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 +01001447
Christopher Faulet10a86702021-04-07 14:18:11 +02001448 if (h1s->flags & H1S_F_RX_BLK) {
1449 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001450 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001451 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 +02001452 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001453 if (h1s->flags & H1S_F_TX_BLK) {
1454 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001455 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001456 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 +01001457 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001458}
1459
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001460/* Search for a websocket key header. The message should have been identified
1461 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001462 *
1463 * On the request side, if found the key is stored in the session. It might be
1464 * needed to calculate response key if the server side is using http/2.
1465 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001466 * On the response side, the key might be verified if haproxy has been
1467 * responsible for the generation of a key. This happens when a h2 client is
1468 * interfaced with a h1 server.
1469 *
1470 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001471 */
1472static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1473{
1474 struct htx_blk *blk;
1475 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001476 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001477 int ws_key_found = 0, idx;
1478
1479 idx = htx_get_head(htx); // returns the SL that we skip
1480 while ((idx = htx_get_next(htx, idx)) != -1) {
1481 blk = htx_get_blk(htx, idx);
1482 type = htx_get_blk_type(blk);
1483
1484 if (type == HTX_BLK_UNUSED)
1485 continue;
1486
1487 if (type != HTX_BLK_HDR)
1488 break;
1489
1490 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001491 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001492
Amaury Denoyellec1938232020-12-11 17:53:03 +01001493 /* Websocket key is base64 encoded of 16 bytes */
1494 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001495 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001496 /* Copy the key on request side
1497 * we might need it if the server is using h2 and does
1498 * not provide the response
1499 */
1500 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001501 ws_key_found = 1;
1502 break;
1503 }
1504 else if (isteqi(n, ist("sec-websocket-accept")) &&
1505 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001506 /* Need to verify the response key if the input was
1507 * generated by haproxy
1508 */
1509 if (h1s->ws_key[0]) {
1510 char key[29];
1511 h1_calculate_ws_output_key(h1s->ws_key, key);
1512 if (!isteqi(ist(key), v))
1513 break;
1514 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001515 ws_key_found = 1;
1516 break;
1517 }
1518 }
1519
1520 /* missing websocket key, reject the message */
1521 if (!ws_key_found) {
1522 htx->flags |= HTX_FL_PARSING_ERROR;
1523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001529/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001530 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001531 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001532 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1533 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001534 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001535static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1536 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001537{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001538 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001539 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001540
Willy Tarreau022e5e52020-09-10 09:33:15 +02001541 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 +02001542
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001543 if (h1s->meth == HTTP_METH_CONNECT)
1544 h1m->flags |= H1_MF_METH_CONNECT;
1545 if (h1s->meth == HTTP_METH_HEAD)
1546 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001547
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001548 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001549 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001550 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 +02001551 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001552 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001553 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 +02001554 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1555 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001556 else if (ret == -2) {
1557 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);
1558 h1s->flags |= H1S_F_RX_CONGESTED;
1559 }
1560 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001561 goto end;
1562 }
1563
Christopher Faulete136bd12021-09-21 18:44:55 +02001564
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001565 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload except if
1566 * accept_payload_with_any_method global option is set.
1567 *There is a payload if the c-l is not null or the the payload is
1568 * chunk-encoded. A parsing error is reported but a A
1569 * 413-Payload-Too-Large is returned instead of a 400-Bad-Request.
Christopher Faulete136bd12021-09-21 18:44:55 +02001570 */
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001571 if (!accept_payload_with_any_method &&
1572 !(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
Christopher Faulete136bd12021-09-21 18:44:55 +02001573 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1574 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1575 h1s->flags |= H1S_F_PARSING_ERROR;
1576 htx->flags |= HTX_FL_PARSING_ERROR;
1577 h1s->h1c->errcode = 413;
1578 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);
1579 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1580 ret = 0;
1581 goto end;
1582 }
1583
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001584 /* Reject any message with an unknown transfer-encoding. In fact if any
1585 * encoding other than "chunked". A 422-Unprocessable-Content is
1586 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1587 * response.
1588 */
1589 if (h1m->flags & H1_MF_TE_OTHER) {
1590 h1s->flags |= H1S_F_PARSING_ERROR;
1591 htx->flags |= HTX_FL_PARSING_ERROR;
1592 if (!(h1m->flags & H1_MF_RESP))
1593 h1s->h1c->errcode = 422;
1594 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1595 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1596 ret = 0;
1597 goto end;
1598 }
1599
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001600 /* If websocket handshake, search for the websocket key */
1601 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1602 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1603 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1604 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001605 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001606 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 +01001607 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1608
1609 ret = 0;
1610 goto end;
1611 }
1612 }
1613
Christopher Faulet486498c2019-10-11 14:22:00 +02001614 if (h1m->err_pos >= 0) {
1615 /* Maybe we found an error during the parsing while we were
1616 * configured not to block on that, so we have to capture it
1617 * now.
1618 */
1619 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1620 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1621 }
1622
Christopher Faulet5696f542020-12-02 16:08:38 +01001623 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001624 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001625 if (h1s->meth == HTTP_METH_HEAD)
1626 h1s->flags |= H1S_F_BODYLESS_RESP;
1627 }
1628 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001629 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001630 if (h1s->status == 204 || h1s->status == 304)
1631 h1s->flags |= H1S_F_BODYLESS_RESP;
1632 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001633 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001634 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001635
Christopher Faulet129817b2018-09-20 16:14:40 +02001636 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001637 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 +02001638 return ret;
1639}
1640
1641/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001642 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001643 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1644 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001645 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001646static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1647 struct buffer *buf, size_t *ofs, size_t max,
1648 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001649{
Christopher Fauletde471a42021-02-01 16:37:28 +01001650 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001651
Willy Tarreau022e5e52020-09-10 09:33:15 +02001652 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 +02001653 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001654 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001655 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 +01001656 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001657 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001658 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 +02001659 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001661 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001662 }
1663
Christopher Faulet02a02532019-11-15 09:36:28 +01001664 *ofs += ret;
1665
1666 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001667 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1668 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);
1669 h1s->flags |= H1S_F_RX_CONGESTED;
1670 }
1671
Willy Tarreau022e5e52020-09-10 09:33:15 +02001672 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 +02001673 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001674}
1675
1676/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001677 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1678 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1679 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001680 * responsible to update the parser state <h1m>. If more room is requested,
1681 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001682 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001683static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1684 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001685{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001686 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001687
Willy Tarreau022e5e52020-09-10 09:33:15 +02001688 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 +02001689 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001690 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001691 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 +02001692 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001693 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001694 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 +02001695 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1696 }
Christopher Fauletae660be2022-04-13 17:48:54 +02001697 else if (ret == -2) {
Christopher Faulet46e058d2021-09-20 07:47:27 +02001698 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);
1699 h1s->flags |= H1S_F_RX_CONGESTED;
1700 }
1701 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001702 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001703 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001704
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001705 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001706
1707 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001708 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 +02001709 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001710}
1711
1712/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001713 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001714 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1715 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001716 *
1717 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001718 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001719static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001720{
Christopher Faulet539e0292018-11-19 10:40:09 +01001721 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001722 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001723 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001724 size_t data;
1725 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001726 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001727
Christopher Faulet539e0292018-11-19 10:40:09 +01001728 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001729 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001730
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001731 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001732 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001733
Christopher Fauletbef89002022-10-05 07:50:19 +02001734 if (h1s->flags & (H1S_F_INTERNAL_ERROR|H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001735 goto end;
1736
Christopher Faulet10a86702021-04-07 14:18:11 +02001737 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001738 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001739
Christopher Faulet46e058d2021-09-20 07:47:27 +02001740 /* Always remove congestion flags and try to process more input data */
1741 h1s->flags &= ~H1S_F_RX_CONGESTED;
1742
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001743 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001744 size_t used = htx_used_space(htx);
1745
Christopher Faulet129817b2018-09-20 16:14:40 +02001746 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001747 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001748 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001749 if (!ret)
1750 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001751
1752 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1753 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1754
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001755 if ((h1m->flags & H1_MF_RESP) &&
1756 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1757 h1m_init_res(&h1s->res);
1758 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001759 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001760 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001761 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001762 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001763 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001764 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001765 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001766 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001767
1768 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1769 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001770 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001771 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001772 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001773 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001774 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001775 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001776
Christopher Faulet76014fd2019-12-10 11:47:22 +01001777 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1778 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001779 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001780 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001781 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1782 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001783
Christopher Fauletf4b89f12023-02-23 13:58:13 +01001784 if (!(h1c->flags & H1C_F_IS_BACK)) {
1785 /* The request was fully received. It means the H1S now
1786 * expect data from the opposite side
1787 */
1788 se_expect_data(h1s->sd);
1789 }
1790
Christopher Faulet5be651d2021-01-22 15:28:03 +01001791 if ((h1m->flags & H1_MF_RESP) &&
1792 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1793 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001794 else {
1795 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1796 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001797 h1s->flags |= H1S_F_RX_BLK;
1798 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001799 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001800 if (h1s->flags & H1S_F_TX_BLK) {
1801 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001802 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001803 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 +01001804 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001805 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001806 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001807 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001808 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001809 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001810 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001811 if (!ret)
1812 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001813
1814 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1815 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001816 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001817 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001818 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001819 break;
1820 }
1821
Christopher Faulet30db3d72019-05-17 15:35:33 +02001822 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001823 } 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 +01001824
Christopher Faulet129817b2018-09-20 16:14:40 +02001825
Christopher Faulet2eed8002020-12-07 11:26:13 +01001826 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001827 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 +02001828 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001829 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001830
Christopher Faulet539e0292018-11-19 10:40:09 +01001831 b_del(&h1c->ibuf, total);
1832
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001833 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1834
Christopher Faulet30db3d72019-05-17 15:35:33 +02001835 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001836 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001837 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001838 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 +01001839 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001840 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001841
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001842 if (!b_data(&h1c->ibuf))
1843 h1_release_buf(h1c, &h1c->ibuf);
1844
Christopher Faulet2177d962022-10-05 08:39:14 +02001845 if (h1m->state <= H1_MSG_LAST_LF)
1846 goto out;
1847
Christopher Faulet4e72b172022-10-04 17:35:19 +02001848 if (h1c->state < H1_CS_RUNNING) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001849 /* The H1 connection is not ready. Most of time, there is no SC
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001850 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1851 * cases, it is only possible on the client side.
1852 */
1853 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1854
Christopher Faulet4e72b172022-10-04 17:35:19 +02001855 if (h1c->state == H1_CS_EMBRYONIC) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001856 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 +02001857 BUG_ON(h1s_sc(h1s));
Willy Tarreau000d63c2022-05-27 10:39:17 +02001858 if (!h1s_new_sc(h1s, buf)) {
Christopher Fauletbef89002022-10-05 07:50:19 +02001859 h1s->flags |= H1S_F_INTERNAL_ERROR;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001860 goto err;
1861 }
1862 }
1863 else {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001864 TRACE_DEVEL("request headers fully parsed, upgrade the inherited SC", H1_EV_RX_DATA, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001865 BUG_ON(h1s_sc(h1s) == NULL);
Willy Tarreau000d63c2022-05-27 10:39:17 +02001866 if (!h1s_upgrade_sc(h1s, buf)) {
Christopher Fauletbef89002022-10-05 07:50:19 +02001867 h1s->flags |= H1S_F_INTERNAL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001868 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001869 goto err;
1870 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001871 }
1872 }
1873
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001874 /* Here h1s_sc(h1s) is always defined */
Christopher Fauletf5ce3202021-11-26 17:26:19 +01001875 if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) {
Christopher Fauleta583af62020-09-24 15:35:37 +02001876 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 +02001877 se_fl_set(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001878 }
1879 else {
1880 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 +02001881 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001882 }
1883
Willy Tarreau4596fe22022-05-17 19:07:51 +02001884 /* Set EOI on stream connector in DONE state iff:
Christopher Fauleta22782b2021-02-08 17:18:01 +01001885 * - it is a response
1886 * - it is a request but no a protocol upgrade nor a CONNECT
1887 *
1888 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001889 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001890 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001891 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1892 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001893 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauleta583af62020-09-24 15:35:37 +02001894
Christopher Fauletec4207c2021-04-08 18:34:30 +02001895 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001896 /* When Input data are pending for this message, notify upper layer that
1897 * the mux need more space in the HTX buffer to continue if :
1898 *
1899 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1900 * - Headers or trailers are pending to be copied.
1901 */
1902 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001903 se_fl_set(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001904 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1905 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001906 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001907 se_fl_clr(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001908 if (h1c->flags & H1C_F_EOS) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001909 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001910 TRACE_STATE("report EOS to SE", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet10498562022-10-10 18:05:25 +02001911 if (h1m->state >= H1_MSG_DONE || (h1m->state > H1_MSG_LAST_LF && !(h1m->flags & H1_MF_XFER_LEN))) {
Christopher Faulet1e857782020-12-08 10:38:22 +01001912 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
Willy Tarreau4596fe22022-05-17 19:07:51 +02001913 * EOI on the stream connector */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001914 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001915 TRACE_STATE("report EOI to SE", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001916 }
Christopher Faulet91ff7092023-03-01 16:04:23 +01001917 else if (h1m->state < H1_MSG_DONE) {
Christopher Faulete9bacf62023-03-29 10:23:21 +02001918 if (h1m->state <= H1_MSG_LAST_LF && b_data(&h1c->ibuf))
Christopher Faulet91ff7092023-03-01 16:04:23 +01001919 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete9bacf62023-03-29 10:23:21 +02001920 se_fl_set(h1s->sd, SE_FL_ERROR);
1921 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 +01001922 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001923
Christopher Faulet10a86702021-04-07 14:18:11 +02001924 if (h1s->flags & H1S_F_TX_BLK) {
1925 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001926 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001927 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 +01001928 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001929 }
Christopher Fauleta4bd7602023-03-15 19:10:55 +01001930 if (h1c->flags & H1C_F_ERROR) {
1931 /* Report a terminal error to the SE if a previous read error was detected */
1932 se_fl_set(h1s->sd, SE_FL_ERROR);
1933 TRACE_STATE("report ERROR to SE", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
1934 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001935 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001936
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001937 end:
Christopher Faulet5966e402022-07-08 09:02:59 +02001938 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001939 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001940 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001941
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001942 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001943 htx_to_buf(htx, buf);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001944 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet75028f82022-12-16 10:43:11 +01001945 if (h1c->state < H1_CS_RUNNING) {
1946 h1c->flags |= H1C_F_EOS;
1947 se_fl_set(h1s->sd, SE_FL_EOS);
1948 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001949 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001950 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001951}
1952
Christopher Faulet129817b2018-09-20 16:14:40 +02001953/*
1954 * Process outgoing data. It parses data and transfer them from the channel buffer into
1955 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1956 * 0 if it couldn't proceed.
1957 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001958static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001959{
Christopher Faulet129817b2018-09-20 16:14:40 +02001960 struct h1s *h1s = h1c->h1s;
1961 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001962 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001963 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001964 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001965 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001966 int last_data = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001967
Christopher Faulet94b2c762019-05-24 15:28:57 +02001968 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001969 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1970
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001971 if (htx_is_empty(chn_htx))
1972 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001973
Christopher Fauletbef89002022-10-05 07:50:19 +02001974 if (h1s->flags & (H1S_F_INTERNAL_ERROR|H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01001975 goto end;
1976
Christopher Faulet51dbc942018-09-13 09:05:15 +02001977 if (!h1_get_buf(h1c, &h1c->obuf)) {
1978 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001979 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 +02001980 goto end;
1981 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001982
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001983 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001984
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001985 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001986 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001987
Willy Tarreau3815b222018-12-11 19:50:43 +01001988 /* Perform some optimizations to reduce the number of buffer copies.
1989 * First, if the mux's buffer is empty and the htx area contains
1990 * exactly one data block of the same size as the requested count,
1991 * then it's possible to simply swap the caller's buffer with the
1992 * mux's output buffer and adjust offsets and length to match the
1993 * entire DATA HTX block in the middle. In this case we perform a
1994 * true zero-copy operation from end-to-end. This is the situation
1995 * that happens all the time with large files. Second, if this is not
1996 * possible, but the mux's output buffer is empty, we still have an
1997 * opportunity to avoid the copy to the intermediary buffer, by making
1998 * the intermediary buffer's area point to the output buffer's area.
1999 * In this case we want to skip the HTX header to make sure that copies
2000 * remain aligned and that this operation remains possible all the
2001 * time. This goes for headers, data blocks and any data extracted from
2002 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01002003 */
2004 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01002005 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002006 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01002007 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01002008 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01002009 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01002010 void *old_area;
2011
Christopher Faulet6b81df72019-10-01 22:08:43 +02002012 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 +01002013 if (h1m->state == H1_MSG_DATA) {
2014 if (h1m->flags & H1_MF_CLEN) {
2015 if (count > h1m->curr_len) {
2016 TRACE_ERROR("too much payload, more than announced",
2017 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2018 goto error;
2019 }
2020 h1m->curr_len -= count;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002021 if (!h1m->curr_len)
2022 last_data = 1;
Christopher Faulet140f1a52021-11-26 16:37:55 +01002023 }
2024 if (chn_htx->flags & HTX_FL_EOM) {
2025 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2026 last_data = 1;
2027 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002028 }
2029
Christopher Faulete5596bf2020-12-02 16:13:22 +01002030 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01002031 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02002032 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01002033 h1c->obuf.data = count;
2034
2035 buf->area = old_area;
2036 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01002037
Christopher Faulet6b81df72019-10-01 22:08:43 +02002038 chn_htx = (struct htx *)buf->area;
2039 htx_reset(chn_htx);
2040
Christopher Fauletadb22202018-12-12 10:32:09 +01002041 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002042 * size and eventually the last chunk. We have at least
2043 * the size of the struct htx to write the chunk
2044 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01002045 */
2046 if (h1m->flags & H1_MF_CHNK) {
2047 h1_emit_chunk_size(&h1c->obuf, count);
2048 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002049 if (last_data) {
2050 /* Emit the last chunk too at the buffer's end */
2051 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
2052 }
Christopher Fauletadb22202018-12-12 10:32:09 +01002053 }
2054
Christopher Faulet6b81df72019-10-01 22:08:43 +02002055 if (h1m->state == H1_MSG_DATA)
2056 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002057 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002058 else
2059 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002060 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002061
Christopher Faulete5596bf2020-12-02 16:13:22 +01002062 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002063 if (last_data) {
2064 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02002065 if (h1s->flags & H1S_F_RX_BLK) {
2066 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002067 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002068 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 +01002069 }
2070
2071 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2072 H1_EV_TX_DATA, h1c->conn, h1s);
2073 }
Willy Tarreau3815b222018-12-11 19:50:43 +01002074 goto out;
2075 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002076 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002077 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002078 else
2079 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02002080
Christopher Fauletc2518a52019-06-25 21:41:02 +02002081 tmp.data = 0;
2082 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02002083 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01002084 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02002085 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01002086 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02002087 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02002088 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02002089
Christopher Fauletb2e84162018-12-06 11:39:49 +01002090 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002091 if (type != HTX_BLK_DATA && vlen > count)
2092 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002093
Christopher Faulet94b2c762019-05-24 15:28:57 +02002094 if (type == HTX_BLK_UNUSED)
2095 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02002096
Christopher Faulet94b2c762019-05-24 15:28:57 +02002097 switch (h1m->state) {
2098 case H1_MSG_RQBEFORE:
2099 if (type != HTX_BLK_REQ_SL)
2100 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002101 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 +02002102 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002103 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02002104 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002105 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002106 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002107 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002108 if (sl->flags & HTX_SL_F_BODYLESS)
2109 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02002110 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01002111 if (h1s->meth == HTTP_METH_HEAD)
2112 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02002113 if (h1s->flags & H1S_F_RX_BLK) {
2114 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002115 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002116 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 +02002117 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002118 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02002119
Christopher Faulet94b2c762019-05-24 15:28:57 +02002120 case H1_MSG_RPBEFORE:
2121 if (type != HTX_BLK_RES_SL)
2122 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002123 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 +02002124 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002125 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02002126 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002127 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002128 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01002129 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002130 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01002131 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02002132 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01002133 else if (h1s->status == 204 || h1s->status == 304)
2134 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02002135 h1m->state = H1_MSG_HDR_FIRST;
2136 break;
2137
Christopher Faulet94b2c762019-05-24 15:28:57 +02002138 case H1_MSG_HDR_FIRST:
2139 case H1_MSG_HDR_NAME:
2140 case H1_MSG_HDR_L2_LWS:
2141 if (type == HTX_BLK_EOH)
2142 goto last_lf;
2143 if (type != HTX_BLK_HDR)
2144 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002145
Christopher Faulet9768c262018-10-22 09:34:31 +02002146 h1m->state = H1_MSG_HDR_NAME;
2147 n = htx_get_blk_name(chn_htx, blk);
2148 v = htx_get_blk_value(chn_htx, blk);
2149
Christopher Faulet86d144c2019-08-14 16:32:25 +02002150 /* Skip all pseudo-headers */
2151 if (*(n.ptr) == ':')
2152 goto skip_hdr;
2153
Christopher Faulet91fcf212020-12-02 16:17:15 +01002154 if (isteq(n, ist("transfer-encoding"))) {
2155 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2156 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02002157 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01002158 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002159 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01002160 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2161 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01002162 /* Only skip C-L header with invalid value. */
2163 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01002164 goto skip_hdr;
2165 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002166 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01002167 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02002168 if (!v.len)
2169 goto skip_hdr;
2170 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01002171 else if (isteq(n, ist("upgrade"))) {
2172 h1_parse_upgrade_header(h1m, v);
2173 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002174 else if ((isteq(n, ist("sec-websocket-accept")) &&
2175 h1m->flags & H1_MF_RESP) ||
2176 (isteq(n, ist("sec-websocket-key")) &&
2177 !(h1m->flags & H1_MF_RESP))) {
Christopher Faulet62138aa2022-11-02 08:42:08 +01002178 h1s->flags |= H1S_F_HAVE_WS_KEY;
Amaury Denoyellec1938232020-12-11 17:53:03 +01002179 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02002180 else if (isteq(n, ist("te"))) {
2181 /* "te" may only be sent with "trailers" if this value
2182 * is present, otherwise it must be deleted.
2183 */
2184 v = istist(v, ist("trailers"));
2185 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2186 goto skip_hdr;
2187 v = ist("trailers");
2188 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002189
Christopher Faulet67d58092019-10-02 10:51:38 +02002190 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002191 if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) &&
2192 isteqi(n, h1c->px->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02002193 goto skip_hdr;
2194
Christopher Faulet98fbe952019-07-22 16:18:24 +02002195 /* Try to adjust the case of the header name */
2196 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2197 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002198 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002199 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002200 skip_hdr:
2201 h1m->state = H1_MSG_HDR_L2_LWS;
2202 break;
2203
Christopher Faulet94b2c762019-05-24 15:28:57 +02002204 case H1_MSG_LAST_LF:
2205 if (type != HTX_BLK_EOH)
2206 goto error;
2207 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002208 h1m->state = H1_MSG_LAST_LF;
2209 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002210 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002211 /* If the reply comes from haproxy while the request is
2212 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002213 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2214 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2215 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002216 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2217 /* T-E + C-L: force close */
2218 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet8087fbb2023-09-27 15:05:03 +02002219 h1m->flags &= ~H1_MF_CLEN;
Christopher Faulet631c7e82021-09-27 09:47:03 +02002220 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2221 }
2222 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2223 /* T-E + HTTP/1.0: force close */
2224 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2225 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2226 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002227
2228 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002229 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002230 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002231 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002232 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002233 /* Try to adjust the case of the header name */
2234 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2235 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002236 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002237 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002238 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002239 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002240 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002241
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002242 if ((h1s->meth != HTTP_METH_CONNECT &&
2243 (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 +01002244 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002245 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002246 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002247 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2248 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002249 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002250 n = ist("transfer-encoding");
2251 v = ist("chunked");
2252 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2253 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002254 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002255 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002256 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002257 h1m->flags |= H1_MF_CHNK;
2258 }
2259
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002260 /* Now add the server name to a header (if requested) */
2261 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002262 !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002263 struct server *srv = objt_server(h1c->conn->target);
2264
2265 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002266 n = h1c->px->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002267 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002268
2269 /* Try to adjust the case of the header name */
2270 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2271 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002272 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002273 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002274 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002275 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002276 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2277 }
2278
Amaury Denoyellec1938232020-12-11 17:53:03 +01002279 /* Add websocket handshake key if needed */
Christopher Faulet62138aa2022-11-02 08:42:08 +01002280 if (!(h1s->flags & H1S_F_HAVE_WS_KEY) &&
2281 (h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002282 if (!(h1m->flags & H1_MF_RESP)) {
2283 /* generate a random websocket key
2284 * stored in the session to
2285 * verify it on the response side
2286 */
2287 h1_generate_random_ws_input_key(h1s->ws_key);
2288
2289 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2290 ist(h1s->ws_key),
2291 &tmp)) {
2292 goto full;
2293 }
2294 }
2295 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002296 /* add the response header key */
2297 char key[29];
2298 h1_calculate_ws_output_key(h1s->ws_key, key);
2299 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2300 ist(key),
2301 &tmp)) {
2302 goto full;
2303 }
2304 }
Christopher Faulet62138aa2022-11-02 08:42:08 +01002305 h1s->flags |= H1S_F_HAVE_WS_KEY;
Amaury Denoyellec1938232020-12-11 17:53:03 +01002306 }
2307
Christopher Faulet6b81df72019-10-01 22:08:43 +02002308 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2309 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2310
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002311 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002312 if (!chunk_memcat(&tmp, "\r\n", 2))
2313 goto full;
2314 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002315 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002316 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002317 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002318 if (!chunk_memcat(&tmp, "\r\n", 2))
2319 goto full;
2320 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002321 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002322 else if ((h1m->flags & H1_MF_RESP) &&
2323 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002324 if (!chunk_memcat(&tmp, "\r\n", 2))
2325 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002326 h1m_init_res(&h1s->res);
2327 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002328 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002329 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002330 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002331 else {
Christopher Faulet2eb52432022-04-07 10:29:38 +02002332 /* EOM flag is set or empty payload (C-L to 0) and it is the last block */
2333 if (htx_is_unique_blk(chn_htx, blk) &&
2334 ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) {
Mickael Torres226082d2022-11-16 14:29:37 +01002335 if ((h1m->flags & H1_MF_CHNK) && !(h1s->flags & H1S_F_BODYLESS_RESP)) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002336 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2337 goto full;
2338 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002339 else if (!chunk_memcat(&tmp, "\r\n", 2))
2340 goto full;
2341 goto done;
2342 }
2343 else if (!chunk_memcat(&tmp, "\r\n", 2))
2344 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002345 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002346 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002347 break;
2348
Christopher Faulet94b2c762019-05-24 15:28:57 +02002349 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002350 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002351 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002352 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2353 goto trailers;
2354
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002355 /* If the message is not chunked, never
2356 * add the last chunk. */
2357 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002358 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002359 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 +02002360 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002361 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002362 else if (type != HTX_BLK_DATA)
2363 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002364
2365 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 +02002366
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002367 /* It is the last block of this message. After this one,
2368 * only tunneled data may be forwarded. */
2369 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2370 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2371 last_data = 1;
2372 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002373
2374 if (vlen > count) {
2375 /* Get the maximum amount of data we can xferred */
2376 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002377 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002378 }
2379
Christopher Faulet140f1a52021-11-26 16:37:55 +01002380 if (h1m->state == H1_MSG_DATA) {
2381 if (h1m->flags & H1_MF_CLEN) {
2382 if (vlen > h1m->curr_len) {
2383 TRACE_ERROR("too much payload, more than announced",
2384 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2385 goto error;
2386 }
Christopher Faulet140f1a52021-11-26 16:37:55 +01002387 }
2388 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2389 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2390 goto skip_data;
2391 }
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002392 }
2393
Christopher Fauletd9233f02019-10-14 14:01:24 +02002394 chklen = 0;
2395 if (h1m->flags & H1_MF_CHNK) {
2396 chklen = b_room(&tmp);
2397 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2398 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2399 (chklen < 1048576) ? 5 : 8);
2400 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002401
2402 /* If it is the end of the chunked message (without EOT), reserve the
2403 * last chunk size */
2404 if (last_data)
2405 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002406 }
2407
2408 if (vlen + chklen > b_room(&tmp)) {
2409 /* too large for the buffer */
2410 if (chklen >= b_room(&tmp))
2411 goto full;
2412 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002413 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002414 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002415 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002416 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002417 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002418 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002419
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002420 /* Space already reserved, so it must succeed */
2421 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2422 goto error;
2423
Christopher Faulet6b81df72019-10-01 22:08:43 +02002424 if (h1m->state == H1_MSG_DATA)
2425 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002426 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002427 else
2428 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002429 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002430
2431 skip_data:
Christopher Faulet2eb52432022-04-07 10:29:38 +02002432 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Fauletb4eca0e2022-01-10 17:27:51 +01002433 h1m->curr_len -= vlen;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002434 if (!h1m->curr_len)
2435 last_data = 1;
2436 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002437 if (last_data)
2438 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002439 break;
2440
Christopher Faulet94b2c762019-05-24 15:28:57 +02002441 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002442 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002443 goto error;
2444 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002445 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002446
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002447 if (!(h1m->flags & H1_MF_CHNK))
2448 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002449
Christopher Faulete5596bf2020-12-02 16:13:22 +01002450 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2451 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 +01002452 if (type == HTX_BLK_EOT)
2453 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002454 break;
2455 }
2456
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002457 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002458 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002459 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002460 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2461 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002462 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002463 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002464 else { // HTX_BLK_TLR
2465 n = htx_get_blk_name(chn_htx, blk);
2466 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002467
2468 /* Try to adjust the case of the header name */
2469 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2470 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002471 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002472 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002473 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002474 break;
2475
Christopher Faulet94b2c762019-05-24 15:28:57 +02002476 case H1_MSG_DONE:
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002477 /* If the message is not chunked, ignore
2478 * trailers. It may happen with H2 messages. */
2479 if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && !(h1m->flags & H1_MF_CHNK))
2480 break;
2481
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002482 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2483 goto error; /* For now return an error */
2484
Christopher Faulet94b2c762019-05-24 15:28:57 +02002485 done:
2486 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002487 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002488 h1s->flags |= H1S_F_TX_BLK;
2489 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002490 }
2491 else if ((h1m->flags & H1_MF_RESP) &&
2492 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2493 /* a successful reply to a CONNECT or a protocol switching is sent
2494 * to the client. Switch the response to tunnel mode.
2495 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002496 h1_set_tunnel_mode(h1s);
Christopher Fauletf3158e92019-11-15 11:14:23 +01002497 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002498
Christopher Faulet10a86702021-04-07 14:18:11 +02002499 if (h1s->flags & H1S_F_RX_BLK) {
2500 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002501 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002502 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 +02002503 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002504
2505 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2506 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002507 break;
2508
Christopher Faulet9768c262018-10-22 09:34:31 +02002509 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002510 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002511 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002512 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002513 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletfc473a62022-10-05 08:22:33 +02002514 se_fl_set(h1s->sd, SE_FL_ERROR);
2515 TRACE_ERROR("processing output error, set error on h1s",
2516 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01002517 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02002518 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002519
2520 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002521 total += vlen;
2522 count -= vlen;
2523 if (sz == vlen)
2524 blk = htx_remove_blk(chn_htx, blk);
2525 else {
2526 htx_cut_data_blk(chn_htx, blk, vlen);
2527 break;
2528 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002529 }
2530
Christopher Faulet9768c262018-10-22 09:34:31 +02002531 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002532 /* when the output buffer is empty, tmp shares the same area so that we
2533 * only have to update pointers and lengths.
2534 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002535 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2536 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002537 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002538 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002539
Willy Tarreau3815b222018-12-11 19:50:43 +01002540 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002541 out:
2542 if (!buf_room_for_htx_data(&h1c->obuf)) {
2543 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002544 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002545 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002546 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002547 /* Both the request and the response reached the DONE state. So set EOI
Willy Tarreau4596fe22022-05-17 19:07:51 +02002548 * flag on the stream connector. Most of time, the flag will already be set,
Christopher Fauletdea24742021-01-22 15:12:30 +01002549 * except for protocol upgrades. Report an error if data remains blocked
2550 * in the output buffer.
2551 */
2552 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002553 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletdea24742021-01-22 15:12:30 +01002554 if (!htx_is_empty(chn_htx)) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002555 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
2556 h1s->flags |= H1S_F_PROCESSING_ERROR;
2557 se_fl_set(h1s->sd, SE_FL_ERROR);
2558 TRACE_ERROR("txn done but data waiting to be sent, set error on h1s",
2559 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002560 }
Christopher Fauletdea24742021-01-22 15:12:30 +01002561 }
2562
Christopher Faulet6b81df72019-10-01 22:08:43 +02002563 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002564 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002565
2566 full:
2567 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2568 h1c->flags |= H1C_F_OUT_FULL;
2569 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002570}
2571
Christopher Faulet51dbc942018-09-13 09:05:15 +02002572/*********************************************************/
2573/* functions below are I/O callbacks from the connection */
2574/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002575static void h1_wake_stream_for_recv(struct h1s *h1s)
2576{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002577 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002578 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002579 tasklet_wakeup(h1s->subs->tasklet);
2580 h1s->subs->events &= ~SUB_RETRY_RECV;
2581 if (!h1s->subs->events)
2582 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002583 }
2584}
2585static void h1_wake_stream_for_send(struct h1s *h1s)
2586{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002587 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002588 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002589 tasklet_wakeup(h1s->subs->tasklet);
2590 h1s->subs->events &= ~SUB_RETRY_SEND;
2591 if (!h1s->subs->events)
2592 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002593 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002594}
2595
Christopher Fauletad4daf62021-01-21 17:49:01 +01002596/* alerts the data layer following this sequence :
2597 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2598 * - if its subscribed to send, then it's woken up for send
2599 * - if it was subscribed to neither, its ->wake() callback is called
2600 */
2601static void h1_alert(struct h1s *h1s)
2602{
2603 if (h1s->subs) {
2604 h1_wake_stream_for_recv(h1s);
2605 h1_wake_stream_for_send(h1s);
2606 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002607 else if (h1s_sc(h1s) && h1s_sc(h1s)->app_ops->wake != NULL) {
Christopher Fauletad4daf62021-01-21 17:49:01 +01002608 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002609 h1s_sc(h1s)->app_ops->wake(h1s_sc(h1s));
Christopher Fauletad4daf62021-01-21 17:49:01 +01002610 }
2611}
2612
Christopher Fauletc18fc232020-10-06 17:41:36 +02002613/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
Christopher Faulet56a49942022-10-04 17:45:24 +02002614 * and 0 on error. The flag H1C_F_ABRT_PENDING is set on the H1 connection for
Christopher Fauletc18fc232020-10-06 17:41:36 +02002615 * retryable errors (allocation error or buffer full). On success, the error is
2616 * copied in the output buffer.
2617*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002618static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002619{
2620 int rc = http_get_status_idx(h1c->errcode);
2621 int ret = 0;
2622
2623 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2624
2625 /* Verify if the error is mapped on /dev/null or any empty file */
2626 /// XXX: do a function !
2627 if (h1c->px->replies[rc] &&
2628 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2629 h1c->px->replies[rc]->body.errmsg &&
2630 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2631 /* Empty error, so claim a success */
2632 ret = 1;
2633 goto out;
2634 }
2635
2636 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002637 h1c->flags |= H1C_F_ABRT_PENDING;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002638 goto out;
2639 }
2640
2641 if (!h1_get_buf(h1c, &h1c->obuf)) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002642 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ABRT_PENDING);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002643 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2644 goto out;
2645 }
Tim Duesterhus77508502022-03-15 13:11:06 +01002646 ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc]));
Christopher Fauletc18fc232020-10-06 17:41:36 +02002647 if (unlikely(ret <= 0)) {
2648 if (!ret) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002649 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ABRT_PENDING);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002650 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2651 goto out;
2652 }
2653 else {
2654 /* we cannot report this error, so claim a success */
2655 ret = 1;
2656 }
2657 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02002658
Christopher Fauletda938022022-12-15 09:22:35 +01002659 if (h1c->state == H1_CS_EMBRYONIC) {
Christopher Faulet8f1f1b02022-12-15 09:59:50 +01002660 BUG_ON(h1c->h1s == NULL || h1s_sc(h1c->h1s) != NULL);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002661 TRACE_DEVEL("Abort embryonic H1S", H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2662 h1s_destroy(h1c->h1s);
2663 }
2664
Christopher Fauleta1a76ce2022-11-23 17:07:48 +01002665 h1c->flags = (h1c->flags & ~(H1C_F_WAIT_NEXT_REQ|H1C_F_ABRT_PENDING)) | H1C_F_ABRTED;
Christopher Faulet061a0982022-11-29 17:16:30 +01002666 h1_close(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002667 out:
2668 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2669 return ret;
2670}
2671
2672/* Try to send a 500 internal error. It relies on h1_send_error to send the
2673 * error. This function takes care of incrementing stats and tracked counters.
2674 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002675static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002676{
2677 struct session *sess = h1c->conn->owner;
Christopher Fauletd1b57302022-11-23 17:13:12 +01002678 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002679
2680 session_inc_http_req_ctr(sess);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002681 proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1);
Willy Tarreau4781b152021-04-06 13:53:36 +02002682 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2683 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002684 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002685 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002686
2687 h1c->errcode = 500;
2688 ret = h1_send_error(h1c);
2689 sess_log(sess);
2690 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002691}
2692
Christopher Fauletb3230f72021-09-21 18:38:20 +02002693/* Try to send an error because of a parsing error. By default a 400 bad request
2694 * error is returned. But the status code may be specified by setting
2695 * h1c->errcode. It relies on h1_send_error to send the error. This function
2696 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002697 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002698static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002699{
2700 struct session *sess = h1c->conn->owner;
Christopher Fauletd1b57302022-11-23 17:13:12 +01002701 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002702
Christopher Faulet4e72b172022-10-04 17:35:19 +02002703 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
Christopher Fauleta1a76ce2022-11-23 17:07:48 +01002704 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED;
Christopher Faulet061a0982022-11-29 17:16:30 +01002705 h1_close(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002706 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002707 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002708
2709 session_inc_http_req_ctr(sess);
2710 session_inc_http_err_ctr(sess);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002711 proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1);
Willy Tarreau4781b152021-04-06 13:53:36 +02002712 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2713 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002714 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002715 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002716
Christopher Fauletb3230f72021-09-21 18:38:20 +02002717 if (!h1c->errcode)
2718 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002719 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002720 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2721 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002722
2723 end:
2724 return ret;
2725}
2726
Christopher Faulet2eed8002020-12-07 11:26:13 +01002727/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2728 * the error. This function takes care of incrementing stats and tracked
2729 * counters.
2730 */
2731static int h1_handle_not_impl_err(struct h1c *h1c)
2732{
2733 struct session *sess = h1c->conn->owner;
Christopher Fauletd1b57302022-11-23 17:13:12 +01002734 int ret = 0;
Christopher Faulet2eed8002020-12-07 11:26:13 +01002735
Christopher Faulet4e72b172022-10-04 17:35:19 +02002736 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
Christopher Fauleta1a76ce2022-11-23 17:07:48 +01002737 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED;
Christopher Faulet061a0982022-11-29 17:16:30 +01002738 h1_close(h1c);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002739 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002740 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002741
2742 session_inc_http_req_ctr(sess);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002743 proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1);
Willy Tarreau4781b152021-04-06 13:53:36 +02002744 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2745 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002746 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002747 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002748
2749 h1c->errcode = 501;
2750 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002751 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2752 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002753
2754 end:
2755 return ret;
2756}
2757
Christopher Fauletc18fc232020-10-06 17:41:36 +02002758/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2759 * error. This function takes care of incrementing stats and tracked counters.
2760 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002761static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002762{
2763 struct session *sess = h1c->conn->owner;
Christopher Fauletd1b57302022-11-23 17:13:12 +01002764 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002765
Christopher Faulet4e72b172022-10-04 17:35:19 +02002766 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
Christopher Fauleta1a76ce2022-11-23 17:07:48 +01002767 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED;
Christopher Faulet061a0982022-11-29 17:16:30 +01002768 h1_close(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002769 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002770 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002771
2772 session_inc_http_req_ctr(sess);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002773 proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1);
Willy Tarreau4781b152021-04-06 13:53:36 +02002774 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2775 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002776 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002777 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002778
2779 h1c->errcode = 408;
Christopher Faulet22742442022-11-23 16:58:22 +01002780 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002781 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
Christopher Faulet22742442022-11-23 16:58:22 +01002782 sess_log(sess);
2783
Christopher Fauletc18fc232020-10-06 17:41:36 +02002784 end:
2785 return ret;
2786}
2787
2788
Christopher Faulet51dbc942018-09-13 09:05:15 +02002789/*
2790 * Attempt to read data, and subscribe if none available
2791 */
2792static int h1_recv(struct h1c *h1c)
2793{
2794 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002795 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002796 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002797
Christopher Faulet6b81df72019-10-01 22:08:43 +02002798 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2799
2800 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2801 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002802 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002803 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002804
Christopher Fauletae635762020-09-21 11:47:16 +02002805 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2806 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002807 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002808 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002809
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002810 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2811 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002812 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002813 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002814 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002815
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002816 /*
2817 * If we only have a small amount of data, realign it,
2818 * it's probably cheaper than doing 2 recv() calls.
2819 */
2820 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002821 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002822
Willy Tarreau88718952023-03-17 12:30:38 +01002823 max = buf_room_for_htx_data(&h1c->ibuf);
2824
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002825 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002826 if (!h1c->h1s ||
2827 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
Willy Tarreau88718952023-03-17 12:30:38 +01002828 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE)) {
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002829 flags |= CO_RFL_READ_ONCE;
2830
Willy Tarreau88718952023-03-17 12:30:38 +01002831 /* we know that the first read will be constrained to a smaller
2832 * read by the stream layer in order to respect the reserve.
2833 * Reading too much will result in global.tune.maxrewrite being
2834 * left at the end of the buffer, and in a very small read
2835 * being performed again to complete them (typically 16 bytes
2836 * freed in the index after headers were consumed) before
2837 * another larger read. Instead, given that we know we're
2838 * waiting for a header and we'll be limited, let's perform a
2839 * shorter first read that the upper layer can retrieve by just
2840 * a pointer swap and the next read will be doable at once in
2841 * an empty buffer.
2842 */
2843 if (max > global.tune.bufsize - global.tune.maxrewrite)
2844 max = global.tune.bufsize - global.tune.maxrewrite;
2845 }
2846
Christopher Faulet51dbc942018-09-13 09:05:15 +02002847 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002848 if (h1c->flags & H1C_F_IN_FULL) {
2849 h1c->flags &= ~H1C_F_IN_FULL;
2850 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2851 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002852
2853 if (!b_data(&h1c->ibuf)) {
2854 /* try to pre-align the buffer like the rxbufs will be
2855 * to optimize memory copies.
2856 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002857 h1c->ibuf.head = sizeof(struct htx);
2858 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002859 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet41951ab2021-11-26 18:12:51 +01002860 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002861 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02002862
2863 if (conn_xprt_read0_pending(conn)) {
2864 TRACE_DEVEL("read0 on connection", H1_EV_H1C_RECV, h1c->conn);
2865 h1c->flags |= H1C_F_EOS;
2866 }
2867 if (h1c->conn->flags & CO_FL_ERROR) {
2868 TRACE_DEVEL("connection error", H1_EV_H1C_RECV, h1c->conn);
2869 h1c->flags |= H1C_F_ERROR;
2870 }
2871
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002872 if (max && !ret && h1_recv_allowed(h1c)) {
2873 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2874 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002875 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002876 else {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002877 TRACE_DATA("data received or pending or connection error", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002878 h1_wake_stream_for_recv(h1c->h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002879 }
2880
Christopher Faulet51dbc942018-09-13 09:05:15 +02002881 if (!b_data(&h1c->ibuf))
2882 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002883 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002884 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002885 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2886 }
2887
2888 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002889 return !!ret || (h1c->flags & (H1C_F_EOS|H1C_F_ERROR));
Christopher Faulet51dbc942018-09-13 09:05:15 +02002890}
2891
2892
2893/*
2894 * Try to send data if possible
2895 */
2896static int h1_send(struct h1c *h1c)
2897{
2898 struct connection *conn = h1c->conn;
2899 unsigned int flags = 0;
2900 size_t ret;
2901 int sent = 0;
2902
Christopher Faulet6b81df72019-10-01 22:08:43 +02002903 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2904
Christopher Fauletfc473a62022-10-05 08:22:33 +02002905 if (h1c->flags & (H1C_F_ERROR|H1C_F_ERR_PENDING)) {
2906 TRACE_DEVEL("leaving on H1C error|err_pending", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002907 b_reset(&h1c->obuf);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002908 if (h1c->flags & H1C_F_EOS)
2909 h1c->flags |= H1C_F_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002910 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002911 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002912
2913 if (!b_data(&h1c->obuf))
2914 goto end;
2915
Christopher Faulet46230362019-10-17 16:04:20 +02002916 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002917 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002918 if (h1c->flags & H1C_F_CO_STREAMER)
2919 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002920
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002921 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002922 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002923 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002924 if (h1c->flags & H1C_F_OUT_FULL) {
2925 h1c->flags &= ~H1C_F_OUT_FULL;
2926 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2927 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01002928 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002929 b_del(&h1c->obuf, ret);
2930 sent = 1;
2931 }
2932
Christopher Fauletfc473a62022-10-05 08:22:33 +02002933 if (conn->flags & CO_FL_ERROR) {
2934 /* connection error, nothing to send, clear the buffer to release it */
2935 TRACE_DEVEL("connection error", H1_EV_H1C_SEND, h1c->conn);
2936 h1c->flags |= H1C_F_ERR_PENDING;
2937 if (h1c->flags & H1C_F_EOS)
2938 h1c->flags |= H1C_F_ERROR;
Christopher Fauleta462ee02022-11-22 17:16:22 +01002939 else if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
2940 /* EOS not seen, so subscribe for reads to be able to
2941 * catch the error on the reading path. It is especially
2942 * important if EOI was reached.
2943 */
2944 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2945 }
Christopher Faulet145aa472018-12-06 10:56:20 +01002946 b_reset(&h1c->obuf);
2947 }
2948
Christopher Faulet51dbc942018-09-13 09:05:15 +02002949 end:
Christopher Fauletc17c31c2022-02-01 18:25:06 +01002950 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002951 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002952
Christopher Faulet51dbc942018-09-13 09:05:15 +02002953 /* We're done, no more to send */
2954 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002955 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002956 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002957 if (h1c->state == H1_CS_CLOSING) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002958 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002959 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002960 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002961 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002962 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2963 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002964 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002965 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002966
Christopher Faulet6b81df72019-10-01 22:08:43 +02002967 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002968 return sent || (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) || (h1c->state == H1_CS_CLOSED);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002969}
2970
Christopher Faulet51dbc942018-09-13 09:05:15 +02002971/* callback called on any event by the connection handler.
2972 * It applies changes and returns zero, or < 0 if it wants immediate
2973 * destruction of the connection.
2974 */
2975static int h1_process(struct h1c * h1c)
2976{
2977 struct connection *conn = h1c->conn;
2978
Christopher Faulet6b81df72019-10-01 22:08:43 +02002979 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2980
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002981 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002982 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Faulet4e72b172022-10-04 17:35:19 +02002983 (h1c->state < H1_CS_RUNNING) && /* IDLE, EMBRYONIC or UPGRADING */
Christopher Fauletfc473a62022-10-05 08:22:33 +02002984 !(h1c->flags & (H1C_F_IN_SALLOC|H1C_F_ABRT_PENDING))) { /* No allocation failure on the stream rxbuf and no ERROR on the H1C */
Christopher Faulet4e72b172022-10-04 17:35:19 +02002985 struct h1s *h1s = h1c->h1s;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002986 struct buffer *buf;
2987 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002988
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002989 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2990 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002991 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002992
2993 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002994 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002995 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2996 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002997 /* Try to match H2 preface before parsing the request headers. */
2998 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2999 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet4e72b172022-10-04 17:35:19 +02003000 if (h1c->state == H1_CS_UPGRADING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003001 BUG_ON(!h1s);
Christopher Fauletc393c9e2023-04-11 08:32:13 +02003002 se_fl_set(h1s->sd, SE_FL_EOI|SE_FL_EOS); /* Set EOS here to release the SC */
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003003 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003004 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01003005 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003006 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003007 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003008
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003009 /* Create the H1 stream if not already there */
3010 if (!h1s) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01003011 h1s = h1c_frt_stream_new(h1c, NULL, h1c->conn->owner);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003012 if (!h1s) {
3013 b_reset(&h1c->ibuf);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003014 h1_handle_internal_err(h1c);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003015 TRACE_ERROR("alloc error", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003016 goto no_parsing;
3017 }
3018 }
3019
3020 if (h1s->sess->t_idle == -1)
Willy Tarreau69530f52023-04-28 09:16:15 +02003021 h1s->sess->t_idle = ns_to_ms(now_ns - h1s->sess->accept_ts) - h1s->sess->t_handshake;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003022
3023 /* Get the stream rxbuf */
3024 buf = h1_get_buf(h1c, &h1s->rxbuf);
3025 if (!buf) {
3026 h1c->flags |= H1C_F_IN_SALLOC;
3027 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
3028 return 0;
3029 }
3030
3031 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003032 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003033 h1_release_buf(h1c, &h1s->rxbuf);
3034 h1_set_idle_expiration(h1c);
Christopher Fauletad4ed002022-12-16 11:13:00 +01003035 if (h1c->state < H1_CS_RUNNING) {
3036 if (h1s->flags & H1S_F_INTERNAL_ERROR) {
3037 h1_handle_internal_err(h1c);
3038 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
3039 }
3040 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
3041 h1_handle_not_impl_err(h1c);
3042 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
3043 }
3044 else if (h1s->flags & H1S_F_PARSING_ERROR || se_fl_test(h1s->sd, SE_FL_ERROR)) {
3045 h1_handle_parsing_error(h1c);
3046 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
3047 }
3048 else {
3049 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
3050 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3051 }
Christopher Faulet2177d962022-10-05 08:39:14 +02003052 }
Christopher Fauletae635762020-09-21 11:47:16 +02003053 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003054
Christopher Faulete6ef4cd2022-11-17 15:54:12 +01003055 no_parsing:
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003056 h1_send(h1c);
3057
Christopher Faulet75308302021-11-15 14:51:37 +01003058 /* H1 connection must be released ASAP if:
Christopher Fauletfc473a62022-10-05 08:22:33 +02003059 * - an error occurred on the H1C or
Christopher Faulet75308302021-11-15 14:51:37 +01003060 * - a read0 was received or
3061 * - a silent shutdown was emitted and all outgoing data sent
3062 */
Christopher Faulet31da34d2022-10-10 16:36:10 +02003063 if ((h1c->flags & (H1C_F_EOS|H1C_F_ERROR|H1C_F_ABRT_PENDING|H1C_F_ABRTED)) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003064 (h1c->state >= H1_CS_CLOSING && (h1c->flags & H1C_F_SILENT_SHUT) && !b_data(&h1c->obuf))) {
3065 if (h1c->state != H1_CS_RUNNING) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003066 /* No stream connector or upgrading */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003067 if (h1c->state < H1_CS_RUNNING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ERROR))) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003068 /* shutdown for reads and no error on the frontend connection: Send an error */
Christopher Fauletb3230f72021-09-21 18:38:20 +02003069 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003070 h1_send(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003071 }
Christopher Faulet56a49942022-10-04 17:45:24 +02003072 else if (h1c->flags & H1C_F_ABRT_PENDING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003073 /* Handle pending error, if any (only possible on frontend connection) */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003074 BUG_ON(h1c->flags & H1C_F_IS_BACK);
3075 if (h1_send_error(h1c))
3076 h1_send(h1c);
3077 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02003078 else {
3079 h1_close(h1c);
3080 TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn);
3081 }
Christopher Fauletae635762020-09-21 11:47:16 +02003082
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003083 /* If there is some pending outgoing data or error, just wait */
Christopher Faulet56a49942022-10-04 17:45:24 +02003084 if (h1c->state == H1_CS_CLOSING || (h1c->flags & H1C_F_ABRT_PENDING))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003085 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01003086
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003087 /* Otherwise we can release the H1 connection */
3088 goto release;
3089 }
3090 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003091 struct h1s *h1s = h1c->h1s;
3092
Willy Tarreau4596fe22022-05-17 19:07:51 +02003093 /* Here there is still a H1 stream with a stream connector.
Christopher Fauletfc473a62022-10-05 08:22:33 +02003094 * Report an error at the stream level and wake up the stream
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003095 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003096 BUG_ON(!h1s);
3097
Christopher Fauletfc473a62022-10-05 08:22:33 +02003098 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3099 se_fl_set_error(h1s->sd);
3100 TRACE_STATE("report (ERR_PENDING|ERROR) to SE", H1_EV_H1C_RECV, conn, h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003101 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003102 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01003103 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003104 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003105 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003106
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003107 if (!b_data(&h1c->ibuf))
3108 h1_release_buf(h1c, &h1c->ibuf);
3109
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003110 /* Check if a soft-stop is in progress.
3111 * Release idling front connection if this is the case.
3112 */
3113 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003114 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
William Dauchya9dd9012022-01-05 22:53:24 +01003115 if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) &&
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003116 h1c->flags & H1C_F_WAIT_NEXT_REQ) {
3117
3118 int send_close = 1;
3119 /* If a close-spread-time option is set, we want to avoid
3120 * closing all the active HTTP2 connections at once so we add a
3121 * random factor that will spread the closing.
3122 */
3123 if (tick_isset(global.close_spread_end)) {
3124 int remaining_window = tick_remain(now_ms, global.close_spread_end);
3125 if (remaining_window) {
3126 /* This should increase the closing rate the
3127 * further along the window we are.
3128 */
3129 send_close = (remaining_window <= statistical_prng_range(global.close_spread_time));
3130 }
3131 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02003132 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
3133 send_close = 0; /* let the client close his connection himself */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003134 if (send_close)
3135 goto release;
3136 }
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003137 }
3138 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003139
Christopher Faulet4e72b172022-10-04 17:35:19 +02003140 if (h1c->state == H1_CS_RUNNING && (h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1c->h1s)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003141 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet4e72b172022-10-04 17:35:19 +02003142 h1_wake_stream_for_recv(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003143 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003144
Christopher Faulet47365272018-10-31 17:40:50 +01003145 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02003146 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003147 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003148 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01003149
3150 release:
Christopher Faulet4e72b172022-10-04 17:35:19 +02003151 if (h1c->state == H1_CS_UPGRADING) {
3152 struct h1s *h1s = h1c->h1s;
3153
3154 /* Don't release the H1 connection right now, we must destroy
3155 * the attached SC first */
3156 BUG_ON(!h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003157
Christopher Fauletfc473a62022-10-05 08:22:33 +02003158 if (h1c->flags & H1C_F_EOS) {
Christopher Fauletc393c9e2023-04-11 08:32:13 +02003159 se_fl_set(h1s->sd, SE_FL_EOI|SE_FL_EOS);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003160 TRACE_STATE("report EOS to SE", H1_EV_H1C_RECV, conn, h1s);
3161 }
3162 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3163 se_fl_set_error(h1s->sd);
3164 TRACE_STATE("report (ERR_PENDING|ERROR) to SE", H1_EV_H1C_RECV, conn, h1s);
3165 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003166 h1_alert(h1s);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003167 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003168 }
3169 else {
3170 h1_release(h1c);
3171 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
3172 }
Christopher Faulet539e0292018-11-19 10:40:09 +01003173 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003174}
3175
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003176struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003177{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003178 struct connection *conn;
3179 struct tasklet *tl = (struct tasklet *)t;
3180 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003181 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003182 int ret = 0;
3183
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003184 if (state & TASK_F_USR1) {
3185 /* the tasklet was idling on an idle connection, it might have
3186 * been stolen, let's be careful!
3187 */
3188 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3189 if (tl->context == NULL) {
3190 /* The connection has been taken over by another thread,
3191 * we're no longer responsible for it, so just free the
3192 * tasklet, and do nothing.
3193 */
3194 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3195 tasklet_free(tl);
3196 return NULL;
3197 }
3198 conn = h1c->conn;
3199 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003200
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003201 /* Remove the connection from the list, to be sure nobody attempts
3202 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003203 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01003204 conn_in_list = conn_get_idle_flag(conn);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003205 if (conn_in_list)
3206 conn_delete_from_tree(&conn->hash_node->node);
3207
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003208 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003209 } else {
3210 /* we're certain the connection was not in an idle list */
3211 conn = h1c->conn;
3212 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
3213 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003214 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003215
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003216 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003217 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003218 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003219 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003220 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003221 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01003222
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003223 /* If we were in an idle list, we want to add it back into it,
3224 * unless h1_process() returned -1, which mean it has destroyed
3225 * the connection (testing !ret is enough, if h1_process() wasn't
3226 * called then ret will be 0 anyway.
3227 */
Willy Tarreau74163142021-03-13 11:30:19 +01003228 if (ret < 0)
3229 t = NULL;
3230
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003231 if (!ret && conn_in_list) {
3232 struct server *srv = objt_server(conn->target);
3233
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003234 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003235 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02003236 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003237 else
Willy Tarreau85223482022-09-29 20:32:43 +02003238 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003239 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003240 }
Willy Tarreau74163142021-03-13 11:30:19 +01003241 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003242}
3243
Christopher Faulet51dbc942018-09-13 09:05:15 +02003244static int h1_wake(struct connection *conn)
3245{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003246 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01003247 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003248
Christopher Faulet6b81df72019-10-01 22:08:43 +02003249 TRACE_POINT(H1_EV_H1C_WAKE, conn);
3250
Christopher Faulet539e0292018-11-19 10:40:09 +01003251 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01003252 ret = h1_process(h1c);
3253 if (ret == 0) {
3254 struct h1s *h1s = h1c->h1s;
3255
Christopher Faulet4e72b172022-10-04 17:35:19 +02003256 if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING)
Christopher Fauletad4daf62021-01-21 17:49:01 +01003257 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003258 }
3259 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003260}
3261
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003262/* Connection timeout management. The principle is that if there's no receipt
3263 * nor sending for a certain amount of time, the connection is closed.
3264 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003265struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003266{
3267 struct h1c *h1c = context;
3268 int expired = tick_is_expired(t->expire, now_ms);
3269
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003270 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003271
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003272 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003273 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003274 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003275
3276 /* Somebody already stole the connection from us, so we should not
3277 * free it, we just have to free the task.
3278 */
3279 if (!t->context) {
3280 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003281 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003282 goto do_leave;
3283 }
3284
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003285 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003286 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003287 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003288 return t;
3289 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003290
Willy Tarreau4596fe22022-05-17 19:07:51 +02003291 /* If a stream connector is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003292 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003293 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003294 if (h1c->state == H1_CS_RUNNING) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003295 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003296 t->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +02003297 TRACE_DEVEL("leaving (SC still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003298 return t;
3299 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003300
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003301 /* Try to send an error to the client */
Christopher Faulet56a49942022-10-04 17:45:24 +02003302 if (h1c->state != H1_CS_CLOSING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ERROR|H1C_F_ABRT_PENDING))) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003303 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003304 if (h1_handle_req_tout(h1c))
3305 h1_send(h1c);
Christopher Faulet56a49942022-10-04 17:45:24 +02003306 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ABRT_PENDING)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003307 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003308 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003309 return t;
3310 }
3311 }
3312
Christopher Fauletf75cc542022-11-22 17:06:13 +01003313 if (h1c->h1s && !se_fl_test(h1c->h1s->sd, SE_FL_ORPHAN)) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003314 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet4e72b172022-10-04 17:35:19 +02003315 * attached SC first. */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003316 se_fl_set(h1c->h1s->sd, SE_FL_EOS | SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003317 h1_alert(h1c->h1s);
3318 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003319 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003320 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003321 return t;
3322 }
3323
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003324 /* We're about to destroy the connection, so make sure nobody attempts
3325 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003326 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01003327 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003328 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003329
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003330 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003331 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003332
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003333 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003334 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003335
3336 if (!h1c) {
3337 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003338 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003339 return NULL;
3340 }
3341
3342 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003343 h1_release(h1c);
3344 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003345 return NULL;
3346}
3347
Christopher Faulet51dbc942018-09-13 09:05:15 +02003348/*******************************************/
3349/* functions below are used by the streams */
3350/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003351
Christopher Faulet51dbc942018-09-13 09:05:15 +02003352/*
3353 * Attach a new stream to a connection
3354 * (Used for outgoing connections)
3355 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003356static int h1_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003357{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003358 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003359 struct h1s *h1s;
3360
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003361 /* this connection is no more idle (if it was at all) */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003362 h1c->flags &= ~H1C_F_SILENT_SHUT;
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003363
Christopher Faulet6b81df72019-10-01 22:08:43 +02003364 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003365 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003366 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3367 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003368 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003369
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003370 h1s = h1c_bck_stream_new(h1c, sd->sc, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003371 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003372 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3373 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003374 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003375
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003376 /* the connection is not idle anymore, let's mark this */
3377 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003378 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003379
Christopher Faulet6b81df72019-10-01 22:08:43 +02003380 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulete00ad352021-12-16 14:44:31 +01003381 return 0;
Christopher Faulet26a26432021-01-27 11:27:50 +01003382 err:
Christopher Faulet26a26432021-01-27 11:27:50 +01003383 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 +01003384 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003385}
3386
Willy Tarreau4596fe22022-05-17 19:07:51 +02003387/* Retrieves a valid stream connector from this connection, or returns NULL.
3388 * For this mux, it's easy as we can only store a single stream connector.
Christopher Faulet51dbc942018-09-13 09:05:15 +02003389 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003390static struct stconn *h1_get_first_sc(const struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003391{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003392 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003393 struct h1s *h1s = h1c->h1s;
3394
3395 if (h1s)
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02003396 return h1s_sc(h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003397
3398 return NULL;
3399}
3400
Christopher Faulet73c12072019-04-08 11:23:22 +02003401static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003402{
Christopher Faulet73c12072019-04-08 11:23:22 +02003403 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003404
Christopher Faulet6b81df72019-10-01 22:08:43 +02003405 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet7c452cc2022-04-14 11:08:26 +02003406 if (!h1c->h1s || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003407 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003408}
3409
3410/*
3411 * Detach the stream from the connection and possibly release the connection.
3412 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003413static void h1_detach(struct sedesc *sd)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003414{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003415 struct h1s *h1s = sd->se;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003416 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003417 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003418 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003419
Christopher Faulet6b81df72019-10-01 22:08:43 +02003420 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3421
Christopher Faulet6b81df72019-10-01 22:08:43 +02003422 if (!h1s) {
3423 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003424 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003425 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003426
Olivier Houchardf502aca2018-12-14 19:42:40 +01003427 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003428 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003429
Christopher Faulet42849b02020-10-05 11:35:17 +02003430 sess->accept_date = date;
Willy Tarreau69530f52023-04-28 09:16:15 +02003431 sess->accept_ts = now_ns;
Christopher Faulet42849b02020-10-05 11:35:17 +02003432 sess->t_handshake = 0;
3433 sess->t_idle = -1;
3434
Olivier Houchard8a786902018-12-15 16:05:40 +01003435 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3436 h1s_destroy(h1s);
3437
Christopher Faulet4e72b172022-10-04 17:35:19 +02003438 if (h1c->state == H1_CS_IDLE && (h1c->flags & H1C_F_IS_BACK)) {
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003439 /* this connection may be killed at any moment, we want it to
3440 * die "cleanly" (i.e. only an RST).
3441 */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003442 h1c->flags |= H1C_F_SILENT_SHUT;
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003443
Christopher Fauletf1204b82019-07-19 14:51:06 +02003444 /* If there are any excess server data in the input buffer,
3445 * release it and close the connection ASAP (some data may
3446 * remain in the output buffer). This happens if a server sends
3447 * invalid responses. So in such case, we don't want to reuse
3448 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003449 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003450 if (b_data(&h1c->ibuf)) {
3451 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet061a0982022-11-29 17:16:30 +01003452 h1_close(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003453 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003454 goto release;
3455 }
Christopher Faulet03627242019-07-19 11:34:08 +02003456
Christopher Faulet08016ab2020-07-01 16:10:06 +02003457 if (h1c->conn->flags & CO_FL_PRIVATE) {
3458 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003459 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3460 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003461 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003462 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003463 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003464 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003465 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003466 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003467 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3468 goto end;
3469 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003470 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003471 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003472 if (h1c->conn->owner == sess)
3473 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003474
3475 /* mark that the tasklet may lose its context to another thread and
3476 * that the handler needs to check it under the idle conns lock.
3477 */
3478 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003479 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003480 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3481
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003482 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003483 /* The server doesn't want it, let's kill the connection right away */
3484 h1c->conn->mux->destroy(h1c);
3485 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3486 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003487 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003488 /* At this point, the connection has been added to the
3489 * server idle list, so another thread may already have
3490 * hijacked it, so we can't do anything with it.
3491 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003492 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003493 }
3494 }
3495
Christopher Fauletf1204b82019-07-19 14:51:06 +02003496 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003497 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003498 if ((h1c->flags & H1C_F_ERROR) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003499 (h1c->state == H1_CS_CLOSED) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003500 (h1c->state == H1_CS_CLOSING && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003501 !h1c->conn->owner) {
3502 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003503 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003504 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003505 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003506 if (h1c->state == H1_CS_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003507 /* If we have a new request, process it immediately or
3508 * subscribe for reads waiting for new data
3509 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003510 if (unlikely(b_data(&h1c->ibuf))) {
3511 if (h1_process(h1c) == -1)
3512 goto end;
3513 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003514 else
3515 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3516 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003517 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003518 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003519 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003520 end:
3521 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003522}
3523
3524
Willy Tarreau000d63c2022-05-27 10:39:17 +02003525static void h1_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003526{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003527 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet7f366362019-04-08 10:51:20 +02003528 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003529
3530 if (!h1s)
3531 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003532 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003533
Christopher Fauletc3fe6f32022-10-05 10:24:11 +02003534 TRACE_POINT(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003535}
3536
Willy Tarreau000d63c2022-05-27 10:39:17 +02003537static void h1_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003538{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003539 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003540 struct h1c *h1c;
3541
3542 if (!h1s)
3543 return;
3544 h1c = h1s->h1c;
3545
Christopher Faulet99293b02021-11-10 10:30:15 +01003546 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003547
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003548 if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003549 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003550 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003551 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003552 if (h1c->state == H1_CS_CLOSING || (h1c->flags & (H1C_F_EOS|H1C_F_ERR_PENDING|H1C_F_ERROR))) {
3553 TRACE_STATE("shutdown on connection (EOS || CLOSING || ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003554 goto do_shutw;
3555 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003556
Christopher Fauletfc473a62022-10-05 08:22:33 +02003557 if (h1c->state == H1_CS_UPGRADING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003558 TRACE_STATE("keep connection alive (UPGRADING)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003559 goto end;
3560 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003561 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3562 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003563 goto end;
3564 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003565
Christopher Faulet7f366362019-04-08 10:51:20 +02003566 do_shutw:
Christopher Faulet061a0982022-11-29 17:16:30 +01003567 h1_close(h1c);
Christopher Faulet07976562022-03-31 11:05:05 +02003568 if (mode != CO_SHW_NORMAL)
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003569 h1c->flags |= H1C_F_SILENT_SHUT;
Christopher Fauleta85c5222021-10-27 15:36:38 +02003570
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003571 if (!b_data(&h1c->obuf))
Christopher Faulet897d6122021-12-17 17:28:35 +01003572 h1_shutw_conn(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003573 end:
3574 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003575}
3576
Christopher Fauleta85c5222021-10-27 15:36:38 +02003577static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003578{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003579 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003580
Christopher Faulet4e72b172022-10-04 17:35:19 +02003581 TRACE_ENTER(H1_EV_H1C_END, conn);
3582 h1_close(h1c);
Willy Tarreau62592ad2021-03-26 09:09:42 +01003583 if (conn->flags & CO_FL_SOCK_WR_SH)
3584 return;
3585
Christopher Faulet666a0c42019-01-08 11:12:04 +01003586 conn_xprt_shutw(conn);
Christopher Fauletce7928d2022-11-18 08:44:44 +01003587 conn_sock_shutw(conn, !(h1c->flags & H1C_F_SILENT_SHUT));
Christopher Faulet551b8962023-03-24 09:26:16 +01003588
3589 if (h1c->wait_event.tasklet && !h1c->wait_event.events)
3590 tasklet_wakeup(h1c->wait_event.tasklet);
3591
Christopher Fauleta85c5222021-10-27 15:36:38 +02003592 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003593}
3594
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003595/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3596 * The <es> pointer is not allowed to differ from the one passed to the
3597 * subscribe() call. It always returns zero.
3598 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003599static int h1_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003600{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003601 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003602
3603 if (!h1s)
3604 return 0;
3605
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003606 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003607 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003608
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003609 es->events &= ~event_type;
3610 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003611 h1s->subs = NULL;
3612
3613 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003614 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003615
3616 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003617 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003618
Christopher Faulet51dbc942018-09-13 09:05:15 +02003619 return 0;
3620}
3621
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003622/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3623 * event subscriber <es> is not allowed to change from a previous call as long
3624 * as at least one event is still subscribed. The <event_type> must only be a
3625 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
Willy Tarreau000d63c2022-05-27 10:39:17 +02003626 * the stream connector <sc> was already detached, in which case it will return
Willy Tarreau4596fe22022-05-17 19:07:51 +02003627 * -1.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003628 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003629static int h1_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003630{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003631 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51bb1852019-09-04 10:22:34 +02003632 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003633
3634 if (!h1s)
3635 return -1;
3636
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003637 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003638 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003639
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003640 es->events |= event_type;
3641 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003642
3643 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003644 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003645
3646
Christopher Faulet6b81df72019-10-01 22:08:43 +02003647 if (event_type & SUB_RETRY_SEND) {
3648 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003649 /*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003650 * If the stconn attempts to subscribe, and the
Christopher Faulet6b81df72019-10-01 22:08:43 +02003651 * mux isn't subscribed to the connection, then it
3652 * probably means the connection wasn't established
3653 * yet, so we have to subscribe.
3654 */
3655 h1c = h1s->h1c;
3656 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3657 h1c->conn->xprt->subscribe(h1c->conn,
3658 h1c->conn->xprt_ctx,
3659 SUB_RETRY_SEND,
3660 &h1c->wait_event);
3661 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003662 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003663}
3664
Christopher Faulet564e39c2021-09-21 15:50:55 +02003665/* Called from the upper layer, to receive data.
3666 *
3667 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3668 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3669 * means the caller wants to flush input data (from the mux buffer and the
3670 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3671 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3672 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3673 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3674 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3675 * copy as much data as possible.
3676 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003677static size_t h1_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003678{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003679 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet539e0292018-11-19 10:40:09 +01003680 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003681 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003682 size_t ret = 0;
3683
Willy Tarreau022e5e52020-09-10 09:33:15 +02003684 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003685
Christopher Faulet4e72b172022-10-04 17:35:19 +02003686 /* Do nothing for now if not RUNNING (implies UPGRADING) */
3687 if (h1c->state < H1_CS_RUNNING) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003688 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3689 goto end;
3690 }
3691
Christopher Faulet539e0292018-11-19 10:40:09 +01003692 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003693 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003694 else
3695 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003696
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003697 if ((flags & CO_RFL_BUF_FLUSH) && se_fl_test(h1s->sd, SE_FL_MAY_SPLICE)) {
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003698 h1c->flags |= H1C_F_WANT_SPLICE;
3699 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 +02003700 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003701 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003702 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 +01003703 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003704 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003705
3706 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003707 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003708 return ret;
3709}
3710
3711
3712/* Called from the upper layer, to send data */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003713static size_t h1_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003714{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003715 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003716 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003717 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003718
3719 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003720 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003721 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003722
Willy Tarreau022e5e52020-09-10 09:33:15 +02003723 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003724
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003725 /* If we're not connected yet, or we're waiting for a handshake, stop
3726 * now, as we don't want to remove everything from the channel buffer
3727 * before we're sure we can send it.
3728 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003729 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003730 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003731 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003732 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003733
Christopher Fauletfc473a62022-10-05 08:22:33 +02003734 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3735 se_fl_set_error(h1s->sd);
Christopher Faulet26a26432021-01-27 11:27:50 +01003736 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 +01003737 return 0;
3738 }
3739
Christopher Faulet46230362019-10-17 16:04:20 +02003740 /* Inherit some flags from the upper layer */
3741 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3742 if (flags & CO_SFL_MSG_MORE)
3743 h1c->flags |= H1C_F_CO_MSG_MORE;
3744 if (flags & CO_SFL_STREAMER)
3745 h1c->flags |= H1C_F_CO_STREAMER;
3746
Christopher Fauletc31872f2019-06-04 22:09:36 +02003747 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003748 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003749
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003750 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003751 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003752 else
3753 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003754
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003755 if (!ret)
Christopher Faulet372b38f2022-07-08 15:20:31 +02003756 break;
3757
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003758 if ((count - ret) > 0)
3759 h1c->flags |= H1C_F_CO_MSG_MORE;
3760
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003761 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003762 count -= ret;
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003763
Olivier Houchard68787ef2020-01-15 19:13:32 +01003764 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003765 break;
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003766
3767 if ((h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)))
3768 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003769 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003770
Christopher Fauletfc473a62022-10-05 08:22:33 +02003771 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3772 // FIXME: following test was removed :
3773 // ((h1c->conn->flags & CO_FL_ERROR) && (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf)))) {
3774 se_fl_set_error(h1s->sd);
Christopher Faulet26a26432021-01-27 11:27:50 +01003775 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 +01003776 }
3777
Christopher Faulet7a145d62020-08-05 11:31:16 +02003778 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003779 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003780 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003781}
3782
Willy Tarreaue5733232019-05-22 19:24:06 +02003783#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003784/* Send and get, using splicing */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003785static int h1_rcv_pipe(struct stconn *sc, struct pipe *pipe, unsigned int count)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003786{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003787 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003788 struct h1c *h1c = h1s->h1c;
3789 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003790 int ret = 0;
3791
Christopher Faulet897d6122021-12-17 17:28:35 +01003792 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003793
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003794 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003795 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003796 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 +02003797 goto end;
3798 }
3799
Christopher Fauletcf307562021-07-26 10:49:39 +02003800 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003801 if (h1s_data_pending(h1s)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003802 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003803 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003804 }
3805
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003806 if (!h1_recv_allowed(h1c)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003807 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet0060be92020-07-03 15:02:25 +02003808 goto end;
3809 }
3810
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003811 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003812 count = h1m->curr_len;
Christopher Faulet897d6122021-12-17 17:28:35 +01003813 ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003814 if (ret >= 0) {
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003815 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003816 if (ret > h1m->curr_len) {
3817 h1s->flags |= H1S_F_PARSING_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003818 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003819 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003820 H1_EV_RX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet9ff1bc82023-09-22 09:18:40 +02003821 goto out;
Christopher Faulet140f1a52021-11-26 16:37:55 +01003822 }
3823 h1m->curr_len -= ret;
3824 if (!h1m->curr_len) {
3825 h1m->state = H1_MSG_DONE;
3826 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletf4b89f12023-02-23 13:58:13 +01003827
3828 if (!(h1c->flags & H1C_F_IS_BACK)) {
3829 /* The request was fully received. It means the H1S now
3830 * expect data from the opposite side
3831 */
3832 se_expect_data(h1s->sd);
3833 }
3834
Christopher Faulet897d6122021-12-17 17:28:35 +01003835 TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003836 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003837 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003838 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
3839 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret);
Christopher Faulete18777b2019-04-16 16:46:36 +02003840 }
3841
Christopher Faulet9ff1bc82023-09-22 09:18:40 +02003842 out:
Christopher Faulet897d6122021-12-17 17:28:35 +01003843 if (conn_xprt_read0_pending(h1c->conn)) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003844 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Faulet147e18f2023-03-23 17:29:47 +01003845 TRACE_STATE("report EOS to SE", H1_EV_STRM_RECV, h1c->conn, h1s);
3846 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
3847 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
3848 * EOI on the stream connector */
3849 se_fl_set(h1s->sd, SE_FL_EOI);
3850 TRACE_STATE("report EOI to SE", H1_EV_STRM_RECV, h1c->conn, h1s);
3851 }
3852 else {
3853 se_fl_set(h1s->sd, SE_FL_ERROR);
3854 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERROR;
3855 TRACE_ERROR("message aborted, set error on SC", H1_EV_STRM_RECV|H1_EV_H1S_ERR, h1c->conn, h1s);
3856 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003857 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_EOS;
Christopher Faulet897d6122021-12-17 17:28:35 +01003858 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003859 }
Christopher Faulet9ff1bc82023-09-22 09:18:40 +02003860 end:
Christopher Faulet9009c972022-10-05 12:04:56 +02003861 if (h1c->conn->flags & CO_FL_ERROR) {
3862 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003863 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERROR;
Christopher Faulet9009c972022-10-05 12:04:56 +02003864 TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
3865 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003866
Christopher Faulet94d35102021-04-09 11:58:49 +02003867 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003868 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003869 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Faulet94d35102021-04-09 11:58:49 +02003870 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003871 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s);
3872 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet94d35102021-04-09 11:58:49 +02003873 }
Christopher Faulet27182292020-07-03 15:08:49 +02003874 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003875
Christopher Faulet897d6122021-12-17 17:28:35 +01003876 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003877 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003878}
3879
Willy Tarreau000d63c2022-05-27 10:39:17 +02003880static int h1_snd_pipe(struct stconn *sc, struct pipe *pipe)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003881{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003882 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003883 struct h1c *h1c = h1s->h1c;
3884 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003885 int ret = 0;
3886
Christopher Faulet897d6122021-12-17 17:28:35 +01003887 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003888
Christopher Faulet140f1a52021-11-26 16:37:55 +01003889 if (b_data(&h1c->obuf)) {
3890 if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003891 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s);
3892 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003893 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003894 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003895 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003896
Christopher Faulet897d6122021-12-17 17:28:35 +01003897 ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe);
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003898 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003899 if (ret > h1m->curr_len) {
3900 h1s->flags |= H1S_F_PROCESSING_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003901 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003902 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003903 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 +01003904 goto end;
3905 }
3906 h1m->curr_len -= ret;
3907 if (!h1m->curr_len) {
3908 h1m->state = H1_MSG_DONE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003909 TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003910 }
3911 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003912 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
3913 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret);
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003914
3915 end:
Christopher Faulet9009c972022-10-05 12:04:56 +02003916 if (h1c->conn->flags & CO_FL_ERROR) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003917 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERR_PENDING;
3918 if (h1c->flags & H1C_F_EOS)
3919 h1c->flags |= H1C_F_ERROR;
Christopher Fauleta462ee02022-11-22 17:16:22 +01003920 else if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
3921 /* EOS not seen, so subscribe for reads to be able to
3922 * catch the error on the reading path. It is especially
3923 * important if EOI was reached.
3924 */
3925 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3926 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003927 se_fl_set_error(h1s->sd);
Christopher Faulet9009c972022-10-05 12:04:56 +02003928 TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
3929 }
3930
Christopher Faulet897d6122021-12-17 17:28:35 +01003931 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003932 return ret;
3933}
3934#endif
3935
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003936static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3937{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003938 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003939 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003940
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003941 switch (mux_ctl) {
3942 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003943 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003944 ret |= MUX_STATUS_READY;
3945 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003946 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003947 if (output)
3948 *((int *)output) = h1c->errcode;
3949 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3950 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3951 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3952 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003953 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003954 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003955 default:
3956 return -1;
3957 }
3958}
3959
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003960/* appends some info about connection <h1c> to buffer <msg>, or does nothing if
3961 * <h1c> is NULL. Returns non-zero if the connection is considered suspicious.
3962 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
3963 * not NULL, otherwise a single line is used.
3964 */
3965static int h1_dump_h1c_info(struct buffer *msg, struct h1c *h1c, const char *pfx)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003966{
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003967 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003968
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003969 if (!h1c)
3970 return ret;
3971
Christopher Fauletf376a312019-01-04 15:16:06 +01003972 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3973 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003974 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3975 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003976 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003977 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
Christopher Faulet38f61352022-11-30 14:49:56 +01003978
3979 chunk_appendf(msg, " .task=%p", h1c->task);
3980 if (h1c->task) {
3981 chunk_appendf(msg, " .exp=%s",
3982 h1c->task->expire ? tick_is_expired(h1c->task->expire, now_ms) ? "<PAST>" :
3983 human_time(TICKS_TO_MS(h1c->task->expire - now_ms), TICKS_TO_MS(1000)) : "<NEVER>");
3984 }
3985
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003986 return ret;
3987}
3988
3989/* appends some info about stream <h1s> to buffer <msg>, or does nothing if
3990 * <h1s> is NULL. Returns non-zero if the stream is considered suspicious. May
3991 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
3992 * NULL, otherwise a single line is used.
3993 */
3994static int h1_dump_h1s_info(struct buffer *msg, const struct h1s *h1s, const char *pfx)
3995{
3996 const char *method;
3997 int ret = 0;
3998
3999 if (!h1s)
4000 return ret;
4001
4002 if (h1s->meth < HTTP_METH_OTHER)
4003 method = http_known_methods[h1s->meth].ptr;
4004 else
4005 method = "UNKNOWN";
4006
4007 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .sd.flg=0x%x .req.state=%s .res.state=%s",
4008 h1s, h1s->flags, se_fl_get(h1s->sd),
4009 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004010
Willy Tarreau7079c0f2022-09-02 16:11:28 +02004011 if (pfx)
4012 chunk_appendf(msg, "\n%s", pfx);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004013
Willy Tarreau7079c0f2022-09-02 16:11:28 +02004014 chunk_appendf(msg, " .meth=%s status=%d",
4015 method, h1s->status);
Christopher Faulet186367f2022-05-30 08:45:15 +02004016
Willy Tarreau7079c0f2022-09-02 16:11:28 +02004017 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(h1s->sd));
4018 if (!se_fl_test(h1s->sd, SE_FL_ORPHAN))
4019 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
4020 h1s_sc(h1s)->flags, h1s_sc(h1s)->app);
Christopher Faulet186367f2022-05-30 08:45:15 +02004021
Willy Tarreau7079c0f2022-09-02 16:11:28 +02004022 if (pfx && h1s->subs)
4023 chunk_appendf(msg, "\n%s", pfx);
4024
4025 chunk_appendf(msg, " .subs=%p", h1s->subs);
4026 if (h1s->subs) {
4027 chunk_appendf(msg, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
4028 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
4029 h1s->subs->tasklet->calls,
4030 h1s->subs->tasklet->context);
4031 if (h1s->subs->tasklet->calls >= 1000000)
4032 ret = 1;
4033 resolve_sym_name(msg, NULL, h1s->subs->tasklet->process);
4034 chunk_appendf(msg, ")");
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004035 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01004036 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02004037}
4038
Willy Tarreau7079c0f2022-09-02 16:11:28 +02004039/* for debugging with CLI's "show fd" command */
4040static int h1_show_fd(struct buffer *msg, struct connection *conn)
4041{
4042 struct h1c *h1c = conn->ctx;
4043 struct h1s *h1s = h1c->h1s;
4044 int ret = 0;
4045
4046 ret |= h1_dump_h1c_info(msg, h1c, NULL);
4047
4048 if (h1s)
4049 ret |= h1_dump_h1s_info(msg, h1s, NULL);
4050
4051 return ret;
4052}
4053
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004054/* for debugging with CLI's "show sess" command. May emit multiple lines, each
4055 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
4056 * line is used. Each field starts with a space so it's safe to print it after
4057 * existing fields.
4058 */
4059static int h1_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
4060{
4061 struct h1s *h1s = sd->se;
4062 int ret = 0;
4063
4064 if (!h1s)
4065 return ret;
4066
4067 ret |= h1_dump_h1s_info(msg, h1s, pfx);
4068 if (pfx)
4069 chunk_appendf(msg, "\n%s", pfx);
4070 chunk_appendf(msg, " h1c=%p", h1s->h1c);
4071 ret |= h1_dump_h1c_info(msg, h1s->h1c, pfx);
4072 return ret;
4073}
4074
Christopher Faulet98fbe952019-07-22 16:18:24 +02004075
4076/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
4077static int add_hdr_case_adjust(const char *from, const char *to, char **err)
4078{
4079 struct h1_hdr_entry *entry;
4080
4081 /* Be sure there is a non-empty <to> */
4082 if (!strlen(to)) {
4083 memprintf(err, "expect <to>");
4084 return -1;
4085 }
4086
4087 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004088 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02004089 memprintf(err, "<from> and <to> must not differ execpt the case");
4090 return -1;
4091 }
4092
4093 /* Be sure <from> does not already existsin the tree */
4094 if (ebis_lookup(&hdrs_map.map, from)) {
4095 memprintf(err, "duplicate entry '%s'", from);
4096 return -1;
4097 }
4098
4099 /* Create the entry and insert it in the tree */
4100 entry = malloc(sizeof(*entry));
4101 if (!entry) {
4102 memprintf(err, "out of memory");
4103 return -1;
4104 }
4105
4106 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004107 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01004108 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02004109 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004110 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004111 free(entry);
4112 memprintf(err, "out of memory");
4113 return -1;
4114 }
4115 ebis_insert(&hdrs_map.map, &entry->node);
4116 return 0;
4117}
4118
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004119/* Migrate the the connection to the current thread.
4120 * Return 0 if successful, non-zero otherwise.
4121 * Expected to be called with the old thread lock held.
4122 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004123static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004124{
4125 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004126 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004127
4128 if (fd_takeover(conn->handle.fd, conn) != 0)
4129 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004130
4131 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4132 /* We failed to takeover the xprt, even if the connection may
4133 * still be valid, flag it as error'd, as we have already
4134 * taken over the fd, and wake the tasklet, so that it will
4135 * destroy it.
4136 */
4137 conn->flags |= CO_FL_ERROR;
4138 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
4139 return -1;
4140 }
4141
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004142 if (h1c->wait_event.events)
4143 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
4144 h1c->wait_event.events, &h1c->wait_event);
4145 /* To let the tasklet know it should free itself, and do nothing else,
4146 * set its context to NULL.
4147 */
4148 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004149 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004150
4151 task = h1c->task;
4152 if (task) {
4153 task->context = NULL;
4154 h1c->task = NULL;
4155 __ha_barrier_store();
4156 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004157
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004158 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004159 if (!h1c->task) {
4160 h1_release(h1c);
4161 return -1;
4162 }
4163 h1c->task->process = h1_timeout_task;
4164 h1c->task->context = h1c;
4165 }
4166 h1c->wait_event.tasklet = tasklet_new();
4167 if (!h1c->wait_event.tasklet) {
4168 h1_release(h1c);
4169 return -1;
4170 }
4171 h1c->wait_event.tasklet->process = h1_io_cb;
4172 h1c->wait_event.tasklet->context = h1c;
4173 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
4174 SUB_RETRY_RECV, &h1c->wait_event);
4175
4176 return 0;
4177}
4178
4179
Christopher Faulet98fbe952019-07-22 16:18:24 +02004180static void h1_hdeaders_case_adjust_deinit()
4181{
4182 struct ebpt_node *node, *next;
4183 struct h1_hdr_entry *entry;
4184
4185 node = ebpt_first(&hdrs_map.map);
4186 while (node) {
4187 next = ebpt_next(node);
4188 ebpt_delete(node);
4189 entry = container_of(node, struct h1_hdr_entry, node);
4190 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004191 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004192 free(entry);
4193 node = next;
4194 }
4195 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004196}
4197
Christopher Faulet98fbe952019-07-22 16:18:24 +02004198static int cfg_h1_headers_case_adjust_postparser()
4199{
4200 FILE *file = NULL;
4201 char *c, *key_beg, *key_end, *value_beg, *value_end;
4202 char *err;
4203 int rc, line = 0, err_code = 0;
4204
4205 if (!hdrs_map.name)
4206 goto end;
4207
4208 file = fopen(hdrs_map.name, "r");
4209 if (!file) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004210 ha_alert("h1-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004211 hdrs_map.name);
4212 err_code |= ERR_ALERT | ERR_FATAL;
4213 goto end;
4214 }
4215
4216 /* now parse all lines. The file may contain only two header name per
4217 * line, separated by spaces. All heading and trailing spaces will be
4218 * ignored. Lines starting with a # are ignored.
4219 */
4220 while (fgets(trash.area, trash.size, file) != NULL) {
4221 line++;
4222 c = trash.area;
4223
4224 /* strip leading spaces and tabs */
4225 while (*c == ' ' || *c == '\t')
4226 c++;
4227
4228 /* ignore emptu lines, or lines beginning with a dash */
4229 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
4230 continue;
4231
4232 /* look for the end of the key */
4233 key_beg = c;
4234 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
4235 c++;
4236 key_end = c;
4237
4238 /* strip middle spaces and tabs */
4239 while (*c == ' ' || *c == '\t')
4240 c++;
4241
4242 /* look for the end of the value, it is the end of the line */
4243 value_beg = c;
4244 while (*c && *c != '\n' && *c != '\r')
4245 c++;
4246 value_end = c;
4247
4248 /* trim possibly trailing spaces and tabs */
4249 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
4250 value_end--;
4251
4252 /* set final \0 and check entries */
4253 *key_end = '\0';
4254 *value_end = '\0';
4255
4256 err = NULL;
4257 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
4258 if (rc < 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004259 ha_alert("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004260 hdrs_map.name, err, line);
4261 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004262 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004263 goto end;
4264 }
4265 if (rc > 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004266 ha_warning("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004267 hdrs_map.name, err, line);
4268 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004269 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004270 }
4271 }
4272
4273 end:
4274 if (file)
4275 fclose(file);
4276 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
4277 return err_code;
4278}
4279
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004280/* config parser for global "h1-accept-payload_=-with-any-method" */
4281static int cfg_parse_h1_accept_payload_with_any_method(char **args, int section_type, struct proxy *curpx,
4282 const struct proxy *defpx, const char *file, int line,
4283 char **err)
4284{
4285 if (too_many_args(0, args, err, NULL))
4286 return -1;
4287 accept_payload_with_any_method = 1;
4288 return 0;
4289}
4290
Christopher Faulet98fbe952019-07-22 16:18:24 +02004291
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004292/* config parser for global "h1-header-case-adjust" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004293static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004294 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004295 char **err)
4296{
4297 if (too_many_args(2, args, err, NULL))
4298 return -1;
4299 if (!*(args[1]) || !*(args[2])) {
4300 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
4301 return -1;
4302 }
4303 return add_hdr_case_adjust(args[1], args[2], err);
4304}
4305
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004306/* config parser for global "h1-headers-case-adjust-file" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004307static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004308 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004309 char **err)
4310{
4311 if (too_many_args(1, args, err, NULL))
4312 return -1;
4313 if (!*(args[1])) {
4314 memprintf(err, "'%s' expects <file> as argument.", args[0]);
4315 return -1;
4316 }
4317 free(hdrs_map.name);
4318 hdrs_map.name = strdup(args[1]);
4319 return 0;
4320}
4321
Christopher Faulet98fbe952019-07-22 16:18:24 +02004322/* config keyword parsers */
4323static struct cfg_kw_list cfg_kws = {{ }, {
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004324 { CFG_GLOBAL, "h1-accept-payload-with-any-method", cfg_parse_h1_accept_payload_with_any_method },
Christopher Faulet98fbe952019-07-22 16:18:24 +02004325 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
4326 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
4327 { 0, NULL, NULL },
4328 }
4329};
4330
4331INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4332REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
4333
4334
Christopher Faulet51dbc942018-09-13 09:05:15 +02004335/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05004336/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02004337/****************************************/
4338
4339/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01004340static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02004341 .init = h1_init,
4342 .wake = h1_wake,
4343 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004344 .get_first_sc = h1_get_first_sc,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004345 .detach = h1_detach,
4346 .destroy = h1_destroy,
4347 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01004348 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004349 .rcv_buf = h1_rcv_buf,
4350 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02004351#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02004352 .rcv_pipe = h1_rcv_pipe,
4353 .snd_pipe = h1_snd_pipe,
4354#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02004355 .subscribe = h1_subscribe,
4356 .unsubscribe = h1_unsubscribe,
4357 .shutr = h1_shutr,
4358 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004359 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004360 .show_sd = h1_show_sd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004361 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004362 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02004363 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02004364 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02004365};
4366
Christopher Faulet3f612f72021-02-05 16:44:21 +01004367static const struct mux_ops mux_h1_ops = {
4368 .init = h1_init,
4369 .wake = h1_wake,
4370 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004371 .get_first_sc = h1_get_first_sc,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004372 .detach = h1_detach,
4373 .destroy = h1_destroy,
4374 .avail_streams = h1_avail_streams,
4375 .used_streams = h1_used_streams,
4376 .rcv_buf = h1_rcv_buf,
4377 .snd_buf = h1_snd_buf,
4378#if defined(USE_LINUX_SPLICE)
4379 .rcv_pipe = h1_rcv_pipe,
4380 .snd_pipe = h1_snd_pipe,
4381#endif
4382 .subscribe = h1_subscribe,
4383 .unsubscribe = h1_unsubscribe,
4384 .shutr = h1_shutr,
4385 .shutw = h1_shutw,
4386 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004387 .show_sd = h1_show_sd,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004388 .ctl = h1_ctl,
4389 .takeover = h1_takeover,
4390 .flags = MX_FL_HTX|MX_FL_NO_UPG,
4391 .name = "H1",
4392};
Christopher Faulet51dbc942018-09-13 09:05:15 +02004393
Christopher Faulet3f612f72021-02-05 16:44:21 +01004394/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
4395static struct mux_proto_list mux_proto_h1 =
4396 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
4397static struct mux_proto_list mux_proto_http =
4398 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02004399
Christopher Faulet3f612f72021-02-05 16:44:21 +01004400INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
4401INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01004402
Christopher Faulet51dbc942018-09-13 09:05:15 +02004403/*
4404 * Local variables:
4405 * c-indent-level: 8
4406 * c-basic-offset: 8
4407 * End:
4408 */