blob: 4b071f80ba5e5e1c600e683a549672cf4fa2702c [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);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200308
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200309/* returns the stconn associated to the H1 stream */
310static forceinline struct stconn *h1s_sc(const struct h1s *h1s)
311{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200312 return h1s->sd->sc;
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200313}
314
Christopher Faulet6b81df72019-10-01 22:08:43 +0200315/* the H1 traces always expect that arg1, if non-null, is of type connection
316 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
317 * that arg3, if non-null, is a htx for rx/tx headers.
318 */
319static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
320 const struct ist where, const struct ist func,
321 const void *a1, const void *a2, const void *a3, const void *a4)
322{
323 const struct connection *conn = a1;
324 const struct h1c *h1c = conn ? conn->ctx : NULL;
325 const struct h1s *h1s = a2;
326 const struct htx *htx = a3;
327 const size_t *val = a4;
328
329 if (!h1c)
330 h1c = (h1s ? h1s->h1c : NULL);
331
332 if (!h1c || src->verbosity < H1_VERB_CLEAN)
333 return;
334
335 /* Display frontend/backend info by default */
Christopher Fauletef93be22022-10-04 17:13:32 +0200336 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 +0200337
338 /* Display request and response states if h1s is defined */
Christopher Faulet6580f282021-11-26 17:31:35 +0100339 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200340 chunk_appendf(&trace_buf, " [%s, %s]",
341 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
342
Christopher Faulet6580f282021-11-26 17:31:35 +0100343 if (src->verbosity > H1_VERB_SIMPLE) {
344 chunk_appendf(&trace_buf, " - req=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
345 h1s->req.flags, (unsigned long)h1s->req.curr_len, (unsigned long)h1s->req.body_len);
346 chunk_appendf(&trace_buf, " res=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
347 h1s->res.flags, (unsigned long)h1s->res.curr_len, (unsigned long)h1s->res.body_len);
348 }
349
350 }
351
Christopher Faulet6b81df72019-10-01 22:08:43 +0200352 if (src->verbosity == H1_VERB_CLEAN)
353 return;
354
355 /* Display the value to the 4th argument (level > STATE) */
356 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100357 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200358
359 /* Display status-line if possible (verbosity > MINIMAL) */
360 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
361 const struct htx_blk *blk = htx_get_head_blk(htx);
362 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
363 enum htx_blk_type type = htx_get_blk_type(blk);
364
365 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
366 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
367 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
368 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
369 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
370 }
371
372 /* Display h1c info and, if defined, h1s info (pointer + flags) */
373 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100374 if (h1c->conn)
375 chunk_appendf(&trace_buf, " conn=%p(0x%08x)", h1c->conn, h1c->conn->flags);
376 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200377 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200378 if (h1s->sd)
379 chunk_appendf(&trace_buf, " sd=%p(0x%08x)", h1s->sd, se_fl_get(h1s->sd));
380 if (h1s->sd && h1s_sc(h1s))
Willy Tarreau000d63c2022-05-27 10:39:17 +0200381 chunk_appendf(&trace_buf, " sc=%p(0x%08x)", h1s_sc(h1s), h1s_sc(h1s)->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100382 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200383
384 if (src->verbosity == H1_VERB_MINIMAL)
385 return;
386
387 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
388 if (src->level > TRACE_LEVEL_USER) {
389 if (src->verbosity == H1_VERB_COMPLETE ||
390 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
391 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
392 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
393 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
394 if (src->verbosity == H1_VERB_COMPLETE ||
395 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
396 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
397 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
398 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
399 }
400
401 /* Display htx info if defined (level > USER) */
402 if (src->level > TRACE_LEVEL_USER && htx) {
403 int full = 0;
404
405 /* Full htx info (level > STATE && verbosity > SIMPLE) */
406 if (src->level > TRACE_LEVEL_STATE) {
407 if (src->verbosity == H1_VERB_COMPLETE)
408 full = 1;
409 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
410 full = 1;
411 }
412
413 chunk_memcat(&trace_buf, "\n\t", 2);
414 htx_dump(&trace_buf, htx, full);
415 }
416}
417
418
Christopher Faulet51dbc942018-09-13 09:05:15 +0200419/*****************************************************/
420/* functions below are for dynamic buffer management */
421/*****************************************************/
422/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100423 * Indicates whether or not we may receive data. The rules are the following :
Christopher Fauletfc473a62022-10-05 08:22:33 +0200424 * - if an error or a shutdown for reads was detected on the H1 connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200425 * must not attempt to receive
426 * - if we are waiting for the connection establishment, we must not attempt
427 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200428 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100429 * - if the input buffer failed to be allocated or is full , we must not try
430 * to receive
Christopher Fauletfc473a62022-10-05 08:22:33 +0200431 * - if the mux is blocked on an input condition, we must may not attempt to
432 * receive
433 * - otherwise we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434 */
435static inline int h1_recv_allowed(const struct h1c *h1c)
436{
Christopher Fauletfc473a62022-10-05 08:22:33 +0200437 if (h1c->flags & (H1C_F_EOS|H1C_F_ERROR)) {
438 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 +0200439 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200440 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200441
Christopher Fauletfc473a62022-10-05 08:22:33 +0200442 if (h1c->conn->flags & (CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
443 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 +0100444 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200445 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100446
Christopher Fauletfc473a62022-10-05 08:22:33 +0200447 if ((h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC))) {
448 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 +0200449 return 0;
450 }
451
Christopher Fauletfc473a62022-10-05 08:22:33 +0200452 return 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200453}
454
455/*
456 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
457 * flags are used to figure what buffer was requested. It returns 1 if the
458 * allocation succeeds, in which case the connection is woken up, or 0 if it's
459 * impossible to wake up and we prefer to be woken up later.
460 */
461static int h1_buf_available(void *target)
462{
463 struct h1c *h1c = target;
464
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100465 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200466 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 +0200467 h1c->flags &= ~H1C_F_IN_ALLOC;
468 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200469 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200470 return 1;
471 }
472
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100473 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200474 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 +0200475 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200476 if (h1c->h1s)
477 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200478 return 1;
479 }
480
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100481 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200482 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
483 h1c->flags &= ~H1C_F_IN_SALLOC;
484 tasklet_wakeup(h1c->wait_event.tasklet);
485 return 1;
486 }
487
Christopher Faulet51dbc942018-09-13 09:05:15 +0200488 return 0;
489}
490
491/*
492 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
493 */
494static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
495{
496 struct buffer *buf = NULL;
497
Willy Tarreau2b718102021-04-21 07:32:39 +0200498 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100499 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200500 h1c->buf_wait.target = h1c;
501 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200502 LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200503 }
504 return buf;
505}
506
507/*
508 * Release a buffer, if any, and try to wake up entities waiting in the buffer
509 * wait queue.
510 */
511static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
512{
513 if (bptr->size) {
514 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100515 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200516 }
517}
518
Christopher Fauletef93be22022-10-04 17:13:32 +0200519/* Returns 1 if the H1 connection is alive (IDLE, EMBRYONIC, RUNNING or
520 * RUNNING). Ortherwise 0 is returned.
521 */
522static inline int h1_is_alive(const struct h1c *h1c)
523{
524 return (h1c->state <= H1_CS_RUNNING);
525}
526
527/* Switch the H1 connection to CLOSING or CLOSED mode, depending on the output
528 * buffer state and if there is still a H1 stream or not. If there are sill
529 * pending outgoing data or if there is still a H1 stream, it is set to CLOSING
530 * state. Otherwise it is set to CLOSED mode. */
531static inline void h1_close(struct h1c *h1c)
532{
533 h1c->state = ((h1c->h1s || b_data(&h1c->obuf)) ? H1_CS_CLOSING : H1_CS_CLOSED);
534}
535
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100536/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet4e72b172022-10-04 17:35:19 +0200537 * or not. We rely on H1C state to know if the connection is in-use or not. It
538 * is IDLE only when no H1 stream is attached and when the previous stream, if
539 * any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100540 */
541static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200542{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100543 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200544
Christopher Faulet4e72b172022-10-04 17:35:19 +0200545 return ((h1c->state == H1_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200546}
547
Willy Tarreau00f18a32019-01-26 12:19:01 +0100548/* returns the number of streams still available on a connection */
549static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100550{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100551 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100552}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200553
Christopher Faulet7a145d62020-08-05 11:31:16 +0200554/* Refresh the h1c task timeout if necessary */
555static void h1_refresh_timeout(struct h1c *h1c)
556{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200557 int is_idle_conn = 0;
558
Christopher Faulet7a145d62020-08-05 11:31:16 +0200559 if (h1c->task) {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200560 if (!h1_is_alive(h1c)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200561 /* half-closed or dead connections : switch to clientfin/serverfin
562 * timeouts so that we don't hang too long on clients that have
563 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200564 */
565 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200566 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 +0200567 is_idle_conn = 1;
Christopher Fauletadcd7892020-10-05 17:13:05 +0200568 }
569 else if (b_data(&h1c->obuf)) {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200570 /* alive connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200571 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200572 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
573 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200574 else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->state != H1_CS_RUNNING)) {
575 /* alive front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200576 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100577 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 +0200578 /* A frontend connection not yet ready could be treated the same way as an idle
579 * one in case of soft-close.
580 */
581 is_idle_conn = 1;
Christopher Faulet7a145d62020-08-05 11:31:16 +0200582 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200583 else {
Willy Tarreau4596fe22022-05-17 19:07:51 +0200584 /* alive back connections of front connections with a stream connector attached */
Christopher Fauletadcd7892020-10-05 17:13:05 +0200585 h1c->task->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200586 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 +0200587 }
588
Christopher Fauletdbe57792020-10-05 17:50:58 +0200589 /* Finally set the idle expiration date if shorter */
590 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200591
592 if ((h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
593 is_idle_conn && tick_isset(global.close_spread_end)) {
594 /* If a soft-stop is in progress and a close-spread-time
595 * is set, we want to spread idle connection closing roughly
596 * evenly across the defined window. This should only
597 * act on idle frontend connections.
598 * If the window end is already in the past, we wake the
599 * timeout task up immediately so that it can be closed.
600 */
601 int remaining_window = tick_remain(now_ms, global.close_spread_end);
602 if (remaining_window) {
603 /* We don't need to reset the expire if it would
604 * already happen before the close window end.
605 */
606 if (tick_is_le(global.close_spread_end, h1c->task->expire)) {
607 /* Set an expire value shorter than the current value
608 * because the close spread window end comes earlier.
609 */
610 h1c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
611 TRACE_DEVEL("connection timeout set to value before close-spread window end", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
612 }
613 }
614 else {
615 /* We are past the soft close window end, wake the timeout
616 * task up immediately.
617 */
618 task_wakeup(h1c->task, TASK_WOKEN_TIMER);
619 }
620 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200621 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
622 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200623 }
624}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200625
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200626static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200627{
628 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
629 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
630 h1c->idle_exp = TICK_ETERNITY;
631 return;
632 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200633 if (h1c->state == H1_CS_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200634 if (!tick_isset(h1c->idle_exp)) {
635 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
636 !b_data(&h1c->ibuf) && /* No input data */
637 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
638 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
639 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
640 }
641 else {
642 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
643 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
644 }
645 }
646 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200647 else if (h1c->state < H1_CS_RUNNING) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200648 if (!tick_isset(h1c->idle_exp)) {
649 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
650 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
651 }
652 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200653 else {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200654 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200655 TRACE_DEVEL("unset idle expiration (running or closing)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletdbe57792020-10-05 17:50:58 +0200656 }
657}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200658/*****************************************************************/
659/* functions below are dedicated to the mux setup and management */
660/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200661
662/* returns non-zero if there are input data pending for stream h1s. */
663static inline size_t h1s_data_pending(const struct h1s *h1s)
664{
665 const struct h1m *h1m;
666
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200667 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100668 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200669}
670
Willy Tarreau4596fe22022-05-17 19:07:51 +0200671/* Creates a new stream connector and the associate stream. <input> is used as input
Christopher Faulet16df1782020-12-04 16:47:41 +0100672 * buffer for the stream. On success, it is transferred to the stream and the
673 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
674 * mux must still take care of it. However, there is nothing special to do
675 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
Willy Tarreau4596fe22022-05-17 19:07:51 +0200676 * b_free() on it is always safe. This function returns the stream connector on
Christopher Faulet16df1782020-12-04 16:47:41 +0100677 * success or NULL on error. */
Willy Tarreau000d63c2022-05-27 10:39:17 +0200678static struct stconn *h1s_new_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100679{
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100680 struct h1c *h1c = h1s->h1c;
Christopher Faulet47365272018-10-31 17:40:50 +0100681
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100682 TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100683
Christopher Fauletb669d682022-03-22 18:37:19 +0100684 if (h1s->flags & H1S_F_NOT_FIRST)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200685 se_fl_set(h1s->sd, SE_FL_NOT_FIRST);
Christopher Fauletb669d682022-03-22 18:37:19 +0100686 if (h1s->req.flags & H1_MF_UPG_WEBSOCKET)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200687 se_fl_set(h1s->sd, SE_FL_WEBSOCKET);
Christopher Fauletb669d682022-03-22 18:37:19 +0100688
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200689 if (!sc_new_from_endp(h1s->sd, h1c->conn->owner, input)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +0200690 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 +0100691 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200692 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200693
Christopher Faulet4e72b172022-10-04 17:35:19 +0200694 h1c->state = H1_CS_RUNNING;
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100695 TRACE_LEAVE(H1_EV_STRM_NEW, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200696 return h1s_sc(h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100697
Christopher Faulet91449b02022-03-22 18:45:55 +0100698 err:
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100699 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100700 return NULL;
701}
702
Willy Tarreau000d63c2022-05-27 10:39:17 +0200703static struct stconn *h1s_upgrade_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100704{
705 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
706
Willy Tarreaudf1a2fc2022-05-27 11:11:15 +0200707 if (stream_upgrade_from_sc(h1s_sc(h1s), input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100708 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 +0100709 goto err;
710 }
711
Christopher Faulet4e72b172022-10-04 17:35:19 +0200712 h1s->h1c->state = H1_CS_RUNNING;
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100713 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200714 return h1s_sc(h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100715
716 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100717 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100718 return NULL;
719}
720
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200721static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200722{
723 struct h1s *h1s;
724
Christopher Faulet6b81df72019-10-01 22:08:43 +0200725 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
726
Christopher Faulet51dbc942018-09-13 09:05:15 +0200727 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100728 if (!h1s) {
729 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 +0100730 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100731 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200732 h1s->h1c = h1c;
733 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200734 h1s->sess = NULL;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200735 h1s->sd = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100736 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100737 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200738 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100739 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200740
741 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100742 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200743
Christopher Faulet129817b2018-09-20 16:14:40 +0200744 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100745 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200746
747 h1s->status = 0;
748 h1s->meth = HTTP_METH_OTHER;
749
Christopher Faulet47365272018-10-31 17:40:50 +0100750 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
751 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200752 h1s->h1c->state = H1_CS_EMBRYONIC;
Christopher Faulet4427ea72022-11-23 15:58:59 +0100753 h1s->h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200754 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
755 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200756
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200757 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100758 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200759 return NULL;
760}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100761
Willy Tarreau000d63c2022-05-27 10:39:17 +0200762static struct h1s *h1c_frt_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200763{
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200764 struct h1s *h1s;
765
766 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
767
768 h1s = h1s_new(h1c);
769 if (!h1s)
770 goto fail;
771
Willy Tarreau000d63c2022-05-27 10:39:17 +0200772 if (sc) {
773 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200774 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200775 h1s->sd = sc->sedesc;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100776 }
777 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200778 h1s->sd = sedesc_new();
779 if (!h1s->sd)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100780 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200781 h1s->sd->se = h1s;
782 h1s->sd->conn = h1c->conn;
783 se_fl_set(h1s->sd, SE_FL_T_MUX | SE_FL_ORPHAN);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100784 }
Christopher Fauletf4b89f12023-02-23 13:58:13 +0100785 /* When a request starts, the H1S does not expect data while the request
786 * is not finished. It does not mean the response must not be received,
787 * especially if headers were already forwarded. But it is not
788 * mandatory.
789 */
790 se_expect_no_data(h1s->sd);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200791 h1s->sess = sess;
792
793 if (h1c->px->options2 & PR_O2_REQBUG_OK)
794 h1s->req.err_pos = -1;
795
Christopher Fauletaf5336f2022-09-12 07:46:11 +0200796 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
797 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
798
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200799 h1c->idle_exp = TICK_ETERNITY;
800 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200801 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200802 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100803
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200804 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100805 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100806 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200807 return NULL;
808}
809
Willy Tarreau000d63c2022-05-27 10:39:17 +0200810static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200811{
812 struct h1s *h1s;
813
814 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
815
816 h1s = h1s_new(h1c);
817 if (!h1s)
818 goto fail;
819
Willy Tarreau000d63c2022-05-27 10:39:17 +0200820 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200821 goto fail;
822
Christopher Faulet10a86702021-04-07 14:18:11 +0200823 h1s->flags |= H1S_F_RX_BLK;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200824 h1s->sd = sc->sedesc;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200825 h1s->sess = sess;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200826
Christopher Faulet4e72b172022-10-04 17:35:19 +0200827 h1c->state = H1_CS_RUNNING;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200828
829 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
830 h1s->res.err_pos = -1;
831
Christopher Faulet60fa0512021-11-26 18:10:39 +0100832 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100833 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100834
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200835 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
836 return h1s;
837
838 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100839 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +0200840 pool_free(pool_head_h1s, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100841 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200842}
843
844static void h1s_destroy(struct h1s *h1s)
845{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200846 if (h1s) {
847 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200848
Christopher Faulet6b81df72019-10-01 22:08:43 +0200849 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200850 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200851
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100852 if (h1s->subs)
853 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200854
Christopher Fauletd17ad822020-09-24 15:14:29 +0200855 h1_release_buf(h1c, &h1s->rxbuf);
856
Christopher Faulet10a86702021-04-07 14:18:11 +0200857 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200858 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
859 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100860
Christopher Faulet31da34d2022-10-10 16:36:10 +0200861 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 +0200862 h1_is_alive(h1c) && /* still alive */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100863 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100864 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200865 h1c->state = H1_CS_IDLE;
866 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100867 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
868 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200869 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200870 h1_close(h1c);
871 TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200872 }
Christopher Faulet60fa0512021-11-26 18:10:39 +0100873
874 HA_ATOMIC_DEC(&h1c->px_counters->open_streams);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200875 BUG_ON(h1s->sd && !se_fl_test(h1s->sd, SE_FL_ORPHAN));
876 sedesc_free(h1s->sd);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200877 pool_free(pool_head_h1s, h1s);
878 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200879}
880
881/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200882 * Initialize the mux once it's attached. It is expected that conn->ctx points
Willy Tarreau4596fe22022-05-17 19:07:51 +0200883 * to the existing stream connector (for outgoing connections or for incoming
884 * ones during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200885 * establishment). <input> is always used as Input buffer and may contain
886 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
887 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200888 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200889static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
890 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200891{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200892 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100893 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200894 void *conn_ctx = conn->ctx;
895
896 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200897
898 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100899 if (!h1c) {
900 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200901 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100902 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200903 h1c->conn = conn;
904 h1c->px = proxy;
905
Christopher Fauletef93be22022-10-04 17:13:32 +0200906 h1c->state = H1_CS_IDLE;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200907 h1c->flags = H1C_F_NONE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200908 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200909 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200910 h1c->obuf = BUF_NULL;
911 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200912 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200913
Willy Tarreau90f366b2021-02-20 11:49:49 +0100914 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200915 h1c->wait_event.tasklet = tasklet_new();
916 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200917 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200918 h1c->wait_event.tasklet->process = h1_io_cb;
919 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100920 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200921 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200922
Christopher Faulete9b70722019-04-08 10:46:02 +0200923 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200924 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100925 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
926 if (tick_isset(proxy->timeout.serverfin))
927 h1c->shut_timeout = proxy->timeout.serverfin;
Christopher Faulet563c3452021-11-26 17:37:51 +0100928
929 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be,
930 &h1_stats_module);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100931 } else {
932 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
933 if (tick_isset(proxy->timeout.clientfin))
934 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200935
Christopher Faulet563c3452021-11-26 17:37:51 +0100936 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe,
937 &h1_stats_module);
938
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200939 LIST_APPEND(&mux_stopping_data[tid].list,
940 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100941 }
942 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200943 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100944 if (!t) {
945 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 +0100946 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100947 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100948
949 h1c->task = t;
950 t->process = h1_timeout_task;
951 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200952
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100953 t->expire = tick_add(now_ms, h1c->timeout);
954 }
955
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100956 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200957
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200958 if (h1c->flags & H1C_F_IS_BACK) {
959 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200960 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
961 goto fail;
962 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100963 else if (conn_ctx) {
964 /* Upgraded frontend connection (from TCP) */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100965 if (!h1c_frt_stream_new(h1c, conn_ctx, h1c->conn->owner))
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100966 goto fail;
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100967
Willy Tarreaue68bc612022-05-27 11:23:05 +0200968 /* Attach the SC but Not ready yet */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200969 h1c->state = H1_CS_UPGRADING;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200970 TRACE_DEVEL("Inherit the SC from TCP connection to perform an upgrade",
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100971 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
972 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100973
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200974 if (t) {
975 h1_set_idle_expiration(h1c);
976 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100977 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200978 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100979
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200980 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100981 if (b_data(&h1c->ibuf))
982 tasklet_wakeup(h1c->wait_event.tasklet);
983 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200984 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200985
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100986 if (!conn_is_back(conn))
987 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, proxy, 1);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100988 HA_ATOMIC_INC(&h1c->px_counters->open_conns);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100989 HA_ATOMIC_INC(&h1c->px_counters->total_conns);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100990
Christopher Faulet51dbc942018-09-13 09:05:15 +0200991 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200992 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200993 return 0;
994
995 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200996 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200997 if (h1c->wait_event.tasklet)
998 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 Faulet4de1bff2022-04-14 11:36:41 +02001049 if (h1c->wait_event.tasklet)
1050 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet60fa0512021-11-26 18:10:39 +01001051
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001052 h1s_destroy(h1c->h1s);
1053 if (conn) {
1054 if (h1c->wait_event.events != 0)
1055 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
1056 &h1c->wait_event);
1057 h1_shutw_conn(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001058 }
1059
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001060 HA_ATOMIC_DEC(&h1c->px_counters->open_conns);
1061 pool_free(pool_head_h1c, h1c);
1062
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001063 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001064 if (!conn_is_back(conn))
1065 LIST_DEL_INIT(&conn->stopping_list);
1066
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001067 conn->mux = NULL;
1068 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001069 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001070
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001071 conn_stop_tracking(conn);
1072 conn_full_close(conn);
1073 if (conn->destroy_cb)
1074 conn->destroy_cb(conn);
1075 conn_free(conn);
1076 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001077}
1078
1079/******************************************************/
1080/* functions below are for the H1 protocol processing */
1081/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +02001082/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
1083 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +02001084 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001085static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001086{
Christopher Faulet570d1612018-11-26 11:13:57 +01001087 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001088
Christopher Faulet570d1612018-11-26 11:13:57 +01001089 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001090 (*(p + 5) > '1' ||
1091 (*(p + 5) == '1' && *(p + 7) >= '1')))
1092 h1m->flags |= H1_MF_VER_11;
1093}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001094
Christopher Faulet9768c262018-10-22 09:34:31 +02001095/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
1096 * greater or equal to 1.1
1097 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001098static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +02001099{
Christopher Faulet570d1612018-11-26 11:13:57 +01001100 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001101
Christopher Faulet570d1612018-11-26 11:13:57 +01001102 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001103 (*(p + 5) > '1' ||
1104 (*(p + 5) == '1' && *(p + 7) >= '1')))
1105 h1m->flags |= H1_MF_VER_11;
1106}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001107
Christopher Fauletf2824e62018-10-01 12:12:37 +02001108/* Deduce the connection mode of the client connection, depending on the
1109 * configuration and the H1 message flags. This function is called twice, the
1110 * first time when the request is parsed and the second time when the response
1111 * is parsed.
1112 */
1113static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
1114{
1115 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116
1117 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001118 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001119 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001120 h1s->status == 101) {
1121 /* Either we've established an explicit tunnel, or we're
1122 * switching the protocol. In both cases, we're very unlikely to
1123 * understand the next protocols. We have to switch to tunnel
1124 * mode, so that we transfer the request and responses then let
1125 * this protocol pass unmodified. When we later implement
1126 * specific parsers for such protocols, we'll want to check the
1127 * Upgrade header which contains information about that protocol
1128 * for responses with status 101 (eg: see RFC2817 about TLS).
1129 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001130 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001131 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 +01001132 }
1133 else if (h1s->flags & H1S_F_WANT_KAL) {
1134 /* By default the client is in KAL mode. CLOSE mode mean
1135 * it is imposed by the client itself. So only change
1136 * KAL mode here. */
1137 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1138 /* no length known or explicit close => close */
1139 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001140 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 +01001141 }
1142 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1143 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001144 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001145 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001146 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 +01001147 }
1148 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001149 }
1150 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001151 /* Input direction: first pass */
1152 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1153 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001154 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001155 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 +01001156 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001157 }
1158
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001159 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001160 * unless a 'close-spread-time' option is set (either to define a
1161 * soft-close window or to disable active closing (close-spread-time
1162 * option set to 0).
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001163 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001164 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001165 int want_clo = 1;
1166 /* If a close-spread-time option is set, we want to avoid
1167 * closing all the active HTTP connections at once so we add a
1168 * random factor that will spread the closing.
1169 */
1170 if (tick_isset(global.close_spread_end)) {
1171 int remaining_window = tick_remain(now_ms, global.close_spread_end);
1172 if (remaining_window) {
1173 /* This should increase the closing rate the further along
1174 * the window we are.
1175 */
1176 want_clo = (remaining_window <= statistical_prng_range(global.close_spread_time));
1177 }
1178 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001179 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
1180 want_clo = 0;
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001181
1182 if (want_clo) {
1183 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1184 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);
1185 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001186 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001187}
1188
1189/* Deduce the connection mode of the client connection, depending on the
1190 * configuration and the H1 message flags. This function is called twice, the
1191 * first time when the request is parsed and the second time when the response
1192 * is parsed.
1193 */
1194static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1195{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001196 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001197 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001198 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001199
Christopher Fauletf2824e62018-10-01 12:12:37 +02001200 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001201 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001202 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001203 h1s->status == 101) {
1204 /* Either we've established an explicit tunnel, or we're
1205 * switching the protocol. In both cases, we're very unlikely to
1206 * understand the next protocols. We have to switch to tunnel
1207 * mode, so that we transfer the request and responses then let
1208 * this protocol pass unmodified. When we later implement
1209 * specific parsers for such protocols, we'll want to check the
1210 * Upgrade header which contains information about that protocol
1211 * for responses with status 101 (eg: see RFC2817 about TLS).
1212 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001213 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001214 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 +01001215 }
1216 else if (h1s->flags & H1S_F_WANT_KAL) {
1217 /* By default the server is in KAL mode. CLOSE mode mean
1218 * it is imposed by haproxy itself. So only change KAL
1219 * mode here. */
1220 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1221 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1222 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1223 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001224 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 +01001225 }
1226 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001227 }
Christopher Faulet70033782018-12-05 13:50:11 +01001228 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001229 /* Output direction: first pass */
1230 if (h1m->flags & H1_MF_CONN_CLO) {
1231 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001232 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001233 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 +01001234 }
1235 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1236 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1237 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001238 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1239 /* no explicit keep-alive option httpclose/server-close => close */
1240 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001241 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 +01001242 }
Christopher Faulet70033782018-12-05 13:50:11 +01001243 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001244
1245 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001246 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001247 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001248 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);
1249 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001250}
1251
Christopher Fauletb992af02019-03-28 15:42:24 +01001252static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001253{
1254 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001255
1256 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1257 * token is found
1258 */
1259 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001261
1262 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001263 if (!(h1m->flags & H1_MF_VER_11)) {
1264 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 +01001265 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001266 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001267 }
1268 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001269 if (h1m->flags & H1_MF_VER_11) {
1270 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001271 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001272 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001273 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001274}
1275
Christopher Fauletb992af02019-03-28 15:42:24 +01001276static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001277{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001278 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1279 * token is found
1280 */
1281 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001282 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001283
1284 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001285 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001286 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1287 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 +01001288 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001289 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001290 }
1291 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001292 if (h1m->flags & H1_MF_VER_11) {
1293 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001294 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001295 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001296 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001297}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001298
Christopher Fauletb992af02019-03-28 15:42:24 +01001299static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001300{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001301 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001302 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001303 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001305}
1306
Christopher Fauletb992af02019-03-28 15:42:24 +01001307static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1308{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001309 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001310 h1_set_cli_conn_mode(h1s, h1m);
1311 else
1312 h1_set_srv_conn_mode(h1s, h1m);
1313
1314 if (!(h1m->flags & H1_MF_RESP))
1315 h1_update_req_conn_value(h1s, h1m, conn_val);
1316 else
1317 h1_update_res_conn_value(h1s, h1m, conn_val);
1318}
Christopher Faulete44769b2018-11-29 23:01:45 +01001319
Christopher Faulet98fbe952019-07-22 16:18:24 +02001320/* Try to adjust the case of the message header name using the global map
1321 * <hdrs_map>.
1322 */
1323static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1324{
1325 struct ebpt_node *node;
1326 struct h1_hdr_entry *entry;
1327
1328 /* No entry in the map, do nothing */
1329 if (eb_is_empty(&hdrs_map.map))
1330 return;
1331
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001332 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001333 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1334 return;
1335
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001336 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001337 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1338 return;
1339
1340 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1341 if (!node)
1342 return;
1343 entry = container_of(node, struct h1_hdr_entry, node);
1344 name->ptr = entry->name.ptr;
1345 name->len = entry->name.len;
1346}
1347
Christopher Faulete44769b2018-11-29 23:01:45 +01001348/* Append the description of what is present in error snapshot <es> into <out>.
1349 * The description must be small enough to always fit in a buffer. The output
1350 * buffer may be the trash so the trash must not be used inside this function.
1351 */
1352static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1353{
1354 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001355 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1356 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1357 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1358 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1359 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1360 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001361}
1362/*
1363 * Capture a bad request or response and archive it in the proxy's structure.
1364 * By default it tries to report the error position as h1m->err_pos. However if
1365 * this one is not set, it will then report h1m->next, which is the last known
1366 * parsing point. The function is able to deal with wrapping buffers. It always
1367 * displays buffers as a contiguous area starting at buf->p. The direction is
1368 * determined thanks to the h1m's flags.
1369 */
1370static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1371 struct h1m *h1m, struct buffer *buf)
1372{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001373 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001374 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001375 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001376 union error_snapshot_ctx ctx;
1377
Christopher Faulet4e72b172022-10-04 17:35:19 +02001378 if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001379 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02001380 sess = __sc_strm(h1s_sc(h1s))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001381 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02001382 other_end = __sc_strm(h1s_sc(h1s))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001383 else
1384 other_end = sess->fe;
1385 } else
1386 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001387
1388 /* http-specific part now */
1389 ctx.h1.state = h1m->state;
1390 ctx.h1.c_flags = h1c->flags;
1391 ctx.h1.s_flags = h1s->flags;
1392 ctx.h1.m_flags = h1m->flags;
1393 ctx.h1.m_clen = h1m->curr_len;
1394 ctx.h1.m_blen = h1m->body_len;
1395
1396 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1397 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001398 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1399 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001400}
1401
Christopher Fauletadb22202018-12-12 10:32:09 +01001402/* Emit the chunksize followed by a CRLF in front of data of the buffer
1403 * <buf>. It goes backwards and starts with the byte before the buffer's
1404 * head. The caller is responsible for ensuring there is enough room left before
1405 * the buffer's head for the string.
1406 */
1407static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1408{
1409 char *beg, *end;
1410
1411 beg = end = b_head(buf);
1412 *--beg = '\n';
1413 *--beg = '\r';
1414 do {
1415 *--beg = hextab[chksz & 0xF];
1416 } while (chksz >>= 4);
1417 buf->head -= (end - beg);
1418 b_add(buf, end - beg);
1419}
1420
1421/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1422 * ensuring there is enough room left in the buffer for the string. */
1423static void h1_emit_chunk_crlf(struct buffer *buf)
1424{
1425 *(b_peek(buf, b_data(buf))) = '\r';
1426 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1427 b_add(buf, 2);
1428}
1429
Christopher Faulet129817b2018-09-20 16:14:40 +02001430/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001431 * Switch the stream to tunnel mode. This function must only be called on 2xx
1432 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001433 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001434static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001435{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001436 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001437
Christopher Faulet5be651d2021-01-22 15:28:03 +01001438 h1s->req.state = H1_MSG_TUNNEL;
1439 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001440
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001441 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001442 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001443
Christopher Faulet5be651d2021-01-22 15:28:03 +01001444 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 +01001445
Christopher Faulet10a86702021-04-07 14:18:11 +02001446 if (h1s->flags & H1S_F_RX_BLK) {
1447 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001448 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001449 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 +02001450 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001451 if (h1s->flags & H1S_F_TX_BLK) {
1452 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001453 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001454 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 +01001455 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001456}
1457
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001458/* Search for a websocket key header. The message should have been identified
1459 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001460 *
1461 * On the request side, if found the key is stored in the session. It might be
1462 * needed to calculate response key if the server side is using http/2.
1463 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001464 * On the response side, the key might be verified if haproxy has been
1465 * responsible for the generation of a key. This happens when a h2 client is
1466 * interfaced with a h1 server.
1467 *
1468 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001469 */
1470static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1471{
1472 struct htx_blk *blk;
1473 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001474 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001475 int ws_key_found = 0, idx;
1476
1477 idx = htx_get_head(htx); // returns the SL that we skip
1478 while ((idx = htx_get_next(htx, idx)) != -1) {
1479 blk = htx_get_blk(htx, idx);
1480 type = htx_get_blk_type(blk);
1481
1482 if (type == HTX_BLK_UNUSED)
1483 continue;
1484
1485 if (type != HTX_BLK_HDR)
1486 break;
1487
1488 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001489 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001490
Amaury Denoyellec1938232020-12-11 17:53:03 +01001491 /* Websocket key is base64 encoded of 16 bytes */
1492 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001493 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001494 /* Copy the key on request side
1495 * we might need it if the server is using h2 and does
1496 * not provide the response
1497 */
1498 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001499 ws_key_found = 1;
1500 break;
1501 }
1502 else if (isteqi(n, ist("sec-websocket-accept")) &&
1503 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001504 /* Need to verify the response key if the input was
1505 * generated by haproxy
1506 */
1507 if (h1s->ws_key[0]) {
1508 char key[29];
1509 h1_calculate_ws_output_key(h1s->ws_key, key);
1510 if (!isteqi(ist(key), v))
1511 break;
1512 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001513 ws_key_found = 1;
1514 break;
1515 }
1516 }
1517
1518 /* missing websocket key, reject the message */
1519 if (!ws_key_found) {
1520 htx->flags |= HTX_FL_PARSING_ERROR;
1521 return 0;
1522 }
1523
1524 return 1;
1525}
1526
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001527/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001528 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001529 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001530 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1531 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001532 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001533static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1534 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001535{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001536 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001537 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001538
Willy Tarreau022e5e52020-09-10 09:33:15 +02001539 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 +02001540
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001541 if (h1s->meth == HTTP_METH_CONNECT)
1542 h1m->flags |= H1_MF_METH_CONNECT;
1543 if (h1s->meth == HTTP_METH_HEAD)
1544 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001545
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001546 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001547 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001548 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 +02001549 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001550 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001551 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 +02001552 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1553 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001554 else if (ret == -2) {
1555 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);
1556 h1s->flags |= H1S_F_RX_CONGESTED;
1557 }
1558 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001559 goto end;
1560 }
1561
Christopher Faulete136bd12021-09-21 18:44:55 +02001562
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001563 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload except if
1564 * accept_payload_with_any_method global option is set.
1565 *There is a payload if the c-l is not null or the the payload is
1566 * chunk-encoded. A parsing error is reported but a A
1567 * 413-Payload-Too-Large is returned instead of a 400-Bad-Request.
Christopher Faulete136bd12021-09-21 18:44:55 +02001568 */
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001569 if (!accept_payload_with_any_method &&
1570 !(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
Christopher Faulete136bd12021-09-21 18:44:55 +02001571 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1572 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1573 h1s->flags |= H1S_F_PARSING_ERROR;
1574 htx->flags |= HTX_FL_PARSING_ERROR;
1575 h1s->h1c->errcode = 413;
1576 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);
1577 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1578 ret = 0;
1579 goto end;
1580 }
1581
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001582 /* Reject any message with an unknown transfer-encoding. In fact if any
1583 * encoding other than "chunked". A 422-Unprocessable-Content is
1584 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1585 * response.
1586 */
1587 if (h1m->flags & H1_MF_TE_OTHER) {
1588 h1s->flags |= H1S_F_PARSING_ERROR;
1589 htx->flags |= HTX_FL_PARSING_ERROR;
1590 if (!(h1m->flags & H1_MF_RESP))
1591 h1s->h1c->errcode = 422;
1592 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1593 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1594 ret = 0;
1595 goto end;
1596 }
1597
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001598 /* If websocket handshake, search for the websocket key */
1599 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1600 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1601 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1602 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001603 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001604 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 +01001605 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1606
1607 ret = 0;
1608 goto end;
1609 }
1610 }
1611
Christopher Faulet486498c2019-10-11 14:22:00 +02001612 if (h1m->err_pos >= 0) {
1613 /* Maybe we found an error during the parsing while we were
1614 * configured not to block on that, so we have to capture it
1615 * now.
1616 */
1617 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1618 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1619 }
1620
Christopher Faulet5696f542020-12-02 16:08:38 +01001621 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001622 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001623 if (h1s->meth == HTTP_METH_HEAD)
1624 h1s->flags |= H1S_F_BODYLESS_RESP;
1625 }
1626 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001627 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001628 if (h1s->status == 204 || h1s->status == 304)
1629 h1s->flags |= H1S_F_BODYLESS_RESP;
1630 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001631 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001632 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001633
Christopher Faulet129817b2018-09-20 16:14:40 +02001634 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001635 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 +02001636 return ret;
1637}
1638
1639/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001640 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001641 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1642 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001643 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001644static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1645 struct buffer *buf, size_t *ofs, size_t max,
1646 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001647{
Christopher Fauletde471a42021-02-01 16:37:28 +01001648 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001649
Willy Tarreau022e5e52020-09-10 09:33:15 +02001650 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 +02001651 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001652 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001653 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 +01001654 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001655 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001656 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 +02001657 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001658 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001659 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001660 }
1661
Christopher Faulet02a02532019-11-15 09:36:28 +01001662 *ofs += ret;
1663
1664 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001665 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1666 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);
1667 h1s->flags |= H1S_F_RX_CONGESTED;
1668 }
1669
Willy Tarreau022e5e52020-09-10 09:33:15 +02001670 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 +02001671 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001672}
1673
1674/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001675 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1676 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1677 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001678 * responsible to update the parser state <h1m>. If more room is requested,
1679 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001680 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001681static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1682 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001683{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001684 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001685
Willy Tarreau022e5e52020-09-10 09:33:15 +02001686 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 +02001687 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001688 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001689 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 +02001690 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001691 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001692 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 +02001693 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1694 }
Christopher Fauletae660be2022-04-13 17:48:54 +02001695 else if (ret == -2) {
Christopher Faulet46e058d2021-09-20 07:47:27 +02001696 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);
1697 h1s->flags |= H1S_F_RX_CONGESTED;
1698 }
1699 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001700 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001701 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001702
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001703 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001704
1705 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001706 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 +02001707 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001708}
1709
1710/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001711 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001712 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1713 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001714 *
1715 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001716 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001717static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001718{
Christopher Faulet539e0292018-11-19 10:40:09 +01001719 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001720 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001721 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001722 size_t data;
1723 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001724 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001725
Christopher Faulet539e0292018-11-19 10:40:09 +01001726 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001727 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001728
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001729 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001730 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001731
Christopher Fauletbef89002022-10-05 07:50:19 +02001732 if (h1s->flags & (H1S_F_INTERNAL_ERROR|H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001733 goto end;
1734
Christopher Faulet10a86702021-04-07 14:18:11 +02001735 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001736 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001737
Christopher Faulet46e058d2021-09-20 07:47:27 +02001738 /* Always remove congestion flags and try to process more input data */
1739 h1s->flags &= ~H1S_F_RX_CONGESTED;
1740
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001741 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001742 size_t used = htx_used_space(htx);
1743
Christopher Faulet129817b2018-09-20 16:14:40 +02001744 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001745 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001746 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001747 if (!ret)
1748 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001749
1750 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1751 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1752
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001753 if ((h1m->flags & H1_MF_RESP) &&
1754 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1755 h1m_init_res(&h1s->res);
1756 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001757 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001758 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001759 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001760 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001761 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001762 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001763 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001764 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001765
1766 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1767 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001768 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001769 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001770 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001771 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001772 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001773 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001774
Christopher Faulet76014fd2019-12-10 11:47:22 +01001775 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1776 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001777 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001778 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001779 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1780 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001781
Christopher Fauletf4b89f12023-02-23 13:58:13 +01001782 if (!(h1c->flags & H1C_F_IS_BACK)) {
1783 /* The request was fully received. It means the H1S now
1784 * expect data from the opposite side
1785 */
1786 se_expect_data(h1s->sd);
1787 }
1788
Christopher Faulet5be651d2021-01-22 15:28:03 +01001789 if ((h1m->flags & H1_MF_RESP) &&
1790 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1791 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001792 else {
1793 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1794 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001795 h1s->flags |= H1S_F_RX_BLK;
1796 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001797 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001798 if (h1s->flags & H1S_F_TX_BLK) {
1799 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001800 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001801 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 +01001802 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001803 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001804 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001805 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001806 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001807 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001808 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001809 if (!ret)
1810 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001811
1812 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1813 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001814 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001815 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001816 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001817 break;
1818 }
1819
Christopher Faulet30db3d72019-05-17 15:35:33 +02001820 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001821 } 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 +01001822
Christopher Faulet129817b2018-09-20 16:14:40 +02001823
Christopher Faulet2eed8002020-12-07 11:26:13 +01001824 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001825 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 +02001826 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001827 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001828
Christopher Faulet539e0292018-11-19 10:40:09 +01001829 b_del(&h1c->ibuf, total);
1830
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001831 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1832
Christopher Faulet30db3d72019-05-17 15:35:33 +02001833 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001834 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001835 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001836 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 +01001837 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001838 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001839
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001840 if (!b_data(&h1c->ibuf))
1841 h1_release_buf(h1c, &h1c->ibuf);
1842
Christopher Faulet2177d962022-10-05 08:39:14 +02001843 if (h1m->state <= H1_MSG_LAST_LF)
1844 goto out;
1845
Christopher Faulet4e72b172022-10-04 17:35:19 +02001846 if (h1c->state < H1_CS_RUNNING) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001847 /* The H1 connection is not ready. Most of time, there is no SC
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001848 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1849 * cases, it is only possible on the client side.
1850 */
1851 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1852
Christopher Faulet4e72b172022-10-04 17:35:19 +02001853 if (h1c->state == H1_CS_EMBRYONIC) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001854 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 +02001855 BUG_ON(h1s_sc(h1s));
Willy Tarreau000d63c2022-05-27 10:39:17 +02001856 if (!h1s_new_sc(h1s, buf)) {
Christopher Fauletbef89002022-10-05 07:50:19 +02001857 h1s->flags |= H1S_F_INTERNAL_ERROR;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001858 goto err;
1859 }
1860 }
1861 else {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001862 TRACE_DEVEL("request headers fully parsed, upgrade the inherited SC", H1_EV_RX_DATA, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001863 BUG_ON(h1s_sc(h1s) == NULL);
Willy Tarreau000d63c2022-05-27 10:39:17 +02001864 if (!h1s_upgrade_sc(h1s, buf)) {
Christopher Fauletbef89002022-10-05 07:50:19 +02001865 h1s->flags |= H1S_F_INTERNAL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001866 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001867 goto err;
1868 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001869 }
1870 }
1871
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001872 /* Here h1s_sc(h1s) is always defined */
Christopher Fauletf5ce3202021-11-26 17:26:19 +01001873 if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) {
Christopher Fauleta583af62020-09-24 15:35:37 +02001874 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 +02001875 se_fl_set(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001876 }
1877 else {
1878 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 +02001879 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001880 }
1881
Willy Tarreau4596fe22022-05-17 19:07:51 +02001882 /* Set EOI on stream connector in DONE state iff:
Christopher Fauleta22782b2021-02-08 17:18:01 +01001883 * - it is a response
1884 * - it is a request but no a protocol upgrade nor a CONNECT
1885 *
1886 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001887 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001888 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001889 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1890 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001891 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauleta583af62020-09-24 15:35:37 +02001892
Christopher Fauletec4207c2021-04-08 18:34:30 +02001893 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001894 /* When Input data are pending for this message, notify upper layer that
1895 * the mux need more space in the HTX buffer to continue if :
1896 *
1897 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1898 * - Headers or trailers are pending to be copied.
1899 */
1900 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001901 se_fl_set(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001902 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1903 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001904 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001905 se_fl_clr(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001906 if (h1c->flags & H1C_F_EOS) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001907 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001908 TRACE_STATE("report EOS to SE", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet10498562022-10-10 18:05:25 +02001909 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 +01001910 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
Willy Tarreau4596fe22022-05-17 19:07:51 +02001911 * EOI on the stream connector */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001912 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001913 TRACE_STATE("report EOI to SE", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001914 }
Christopher Faulet91ff7092023-03-01 16:04:23 +01001915 else if (h1m->state < H1_MSG_DONE) {
1916 if (h1m->state > H1_MSG_LAST_LF) {
1917 se_fl_set(h1s->sd, SE_FL_ERROR);
1918 TRACE_ERROR("message aborted, set error on SC", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
1919 }
1920 else if (b_data(&h1c->ibuf)) {
1921 htx->flags |= HTX_FL_PARSING_ERROR;
1922 TRACE_ERROR("truncated message, set error on SC", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
1923 }
1924 /* Otherwise (no data was never received) don't report any error just EOS */
Christopher Faulet26a26432021-01-27 11:27:50 +01001925 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001926
Christopher Faulet10a86702021-04-07 14:18:11 +02001927 if (h1s->flags & H1S_F_TX_BLK) {
1928 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001929 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001930 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 +01001931 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001932 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001933 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001934
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001935 end:
Christopher Faulet5966e402022-07-08 09:02:59 +02001936 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001937 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001938 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001939
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001940 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001941 htx_to_buf(htx, buf);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001942 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet75028f82022-12-16 10:43:11 +01001943 if (h1c->state < H1_CS_RUNNING) {
1944 h1c->flags |= H1C_F_EOS;
1945 se_fl_set(h1s->sd, SE_FL_EOS);
1946 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001947 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001948 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001949}
1950
Christopher Faulet129817b2018-09-20 16:14:40 +02001951/*
1952 * Process outgoing data. It parses data and transfer them from the channel buffer into
1953 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1954 * 0 if it couldn't proceed.
1955 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001956static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001957{
Christopher Faulet129817b2018-09-20 16:14:40 +02001958 struct h1s *h1s = h1c->h1s;
1959 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001960 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001961 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001962 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001963 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001964 int last_data = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001965
Christopher Faulet94b2c762019-05-24 15:28:57 +02001966 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001967 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1968
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001969 if (htx_is_empty(chn_htx))
1970 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001971
Christopher Fauletbef89002022-10-05 07:50:19 +02001972 if (h1s->flags & (H1S_F_INTERNAL_ERROR|H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01001973 goto end;
1974
Christopher Faulet51dbc942018-09-13 09:05:15 +02001975 if (!h1_get_buf(h1c, &h1c->obuf)) {
1976 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001977 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 +02001978 goto end;
1979 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001980
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001981 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001982
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001983 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001984 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001985
Willy Tarreau3815b222018-12-11 19:50:43 +01001986 /* Perform some optimizations to reduce the number of buffer copies.
1987 * First, if the mux's buffer is empty and the htx area contains
1988 * exactly one data block of the same size as the requested count,
1989 * then it's possible to simply swap the caller's buffer with the
1990 * mux's output buffer and adjust offsets and length to match the
1991 * entire DATA HTX block in the middle. In this case we perform a
1992 * true zero-copy operation from end-to-end. This is the situation
1993 * that happens all the time with large files. Second, if this is not
1994 * possible, but the mux's output buffer is empty, we still have an
1995 * opportunity to avoid the copy to the intermediary buffer, by making
1996 * the intermediary buffer's area point to the output buffer's area.
1997 * In this case we want to skip the HTX header to make sure that copies
1998 * remain aligned and that this operation remains possible all the
1999 * time. This goes for headers, data blocks and any data extracted from
2000 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01002001 */
2002 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01002003 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002004 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01002005 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01002006 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01002007 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01002008 void *old_area;
2009
Christopher Faulet6b81df72019-10-01 22:08:43 +02002010 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 +01002011 if (h1m->state == H1_MSG_DATA) {
2012 if (h1m->flags & H1_MF_CLEN) {
2013 if (count > h1m->curr_len) {
2014 TRACE_ERROR("too much payload, more than announced",
2015 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2016 goto error;
2017 }
2018 h1m->curr_len -= count;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002019 if (!h1m->curr_len)
2020 last_data = 1;
Christopher Faulet140f1a52021-11-26 16:37:55 +01002021 }
2022 if (chn_htx->flags & HTX_FL_EOM) {
2023 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2024 last_data = 1;
2025 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002026 }
2027
Christopher Faulete5596bf2020-12-02 16:13:22 +01002028 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01002029 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02002030 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01002031 h1c->obuf.data = count;
2032
2033 buf->area = old_area;
2034 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01002035
Christopher Faulet6b81df72019-10-01 22:08:43 +02002036 chn_htx = (struct htx *)buf->area;
2037 htx_reset(chn_htx);
2038
Christopher Fauletadb22202018-12-12 10:32:09 +01002039 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002040 * size and eventually the last chunk. We have at least
2041 * the size of the struct htx to write the chunk
2042 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01002043 */
2044 if (h1m->flags & H1_MF_CHNK) {
2045 h1_emit_chunk_size(&h1c->obuf, count);
2046 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002047 if (last_data) {
2048 /* Emit the last chunk too at the buffer's end */
2049 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
2050 }
Christopher Fauletadb22202018-12-12 10:32:09 +01002051 }
2052
Christopher Faulet6b81df72019-10-01 22:08:43 +02002053 if (h1m->state == H1_MSG_DATA)
2054 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002055 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002056 else
2057 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002058 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002059
Christopher Faulete5596bf2020-12-02 16:13:22 +01002060 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002061 if (last_data) {
2062 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02002063 if (h1s->flags & H1S_F_RX_BLK) {
2064 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002065 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002066 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 +01002067 }
2068
2069 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2070 H1_EV_TX_DATA, h1c->conn, h1s);
2071 }
Willy Tarreau3815b222018-12-11 19:50:43 +01002072 goto out;
2073 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002074 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002075 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002076 else
2077 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02002078
Christopher Fauletc2518a52019-06-25 21:41:02 +02002079 tmp.data = 0;
2080 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02002081 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01002082 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02002083 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01002084 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02002085 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02002086 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02002087
Christopher Fauletb2e84162018-12-06 11:39:49 +01002088 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002089 if (type != HTX_BLK_DATA && vlen > count)
2090 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002091
Christopher Faulet94b2c762019-05-24 15:28:57 +02002092 if (type == HTX_BLK_UNUSED)
2093 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02002094
Christopher Faulet94b2c762019-05-24 15:28:57 +02002095 switch (h1m->state) {
2096 case H1_MSG_RQBEFORE:
2097 if (type != HTX_BLK_REQ_SL)
2098 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002099 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 +02002100 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002101 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02002102 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002103 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002104 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002105 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002106 if (sl->flags & HTX_SL_F_BODYLESS)
2107 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02002108 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01002109 if (h1s->meth == HTTP_METH_HEAD)
2110 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02002111 if (h1s->flags & H1S_F_RX_BLK) {
2112 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002113 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002114 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 +02002115 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002116 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02002117
Christopher Faulet94b2c762019-05-24 15:28:57 +02002118 case H1_MSG_RPBEFORE:
2119 if (type != HTX_BLK_RES_SL)
2120 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002121 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 +02002122 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002123 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02002124 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002125 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002126 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01002127 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002128 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01002129 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02002130 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01002131 else if (h1s->status == 204 || h1s->status == 304)
2132 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02002133 h1m->state = H1_MSG_HDR_FIRST;
2134 break;
2135
Christopher Faulet94b2c762019-05-24 15:28:57 +02002136 case H1_MSG_HDR_FIRST:
2137 case H1_MSG_HDR_NAME:
2138 case H1_MSG_HDR_L2_LWS:
2139 if (type == HTX_BLK_EOH)
2140 goto last_lf;
2141 if (type != HTX_BLK_HDR)
2142 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002143
Christopher Faulet9768c262018-10-22 09:34:31 +02002144 h1m->state = H1_MSG_HDR_NAME;
2145 n = htx_get_blk_name(chn_htx, blk);
2146 v = htx_get_blk_value(chn_htx, blk);
2147
Christopher Faulet86d144c2019-08-14 16:32:25 +02002148 /* Skip all pseudo-headers */
2149 if (*(n.ptr) == ':')
2150 goto skip_hdr;
2151
Christopher Faulet91fcf212020-12-02 16:17:15 +01002152 if (isteq(n, ist("transfer-encoding"))) {
2153 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2154 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02002155 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01002156 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002157 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01002158 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2159 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01002160 /* Only skip C-L header with invalid value. */
2161 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01002162 goto skip_hdr;
2163 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002164 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01002165 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02002166 if (!v.len)
2167 goto skip_hdr;
2168 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01002169 else if (isteq(n, ist("upgrade"))) {
2170 h1_parse_upgrade_header(h1m, v);
2171 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002172 else if ((isteq(n, ist("sec-websocket-accept")) &&
2173 h1m->flags & H1_MF_RESP) ||
2174 (isteq(n, ist("sec-websocket-key")) &&
2175 !(h1m->flags & H1_MF_RESP))) {
Christopher Faulet62138aa2022-11-02 08:42:08 +01002176 h1s->flags |= H1S_F_HAVE_WS_KEY;
Amaury Denoyellec1938232020-12-11 17:53:03 +01002177 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02002178 else if (isteq(n, ist("te"))) {
2179 /* "te" may only be sent with "trailers" if this value
2180 * is present, otherwise it must be deleted.
2181 */
2182 v = istist(v, ist("trailers"));
2183 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2184 goto skip_hdr;
2185 v = ist("trailers");
2186 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002187
Christopher Faulet67d58092019-10-02 10:51:38 +02002188 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002189 if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) &&
2190 isteqi(n, h1c->px->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02002191 goto skip_hdr;
2192
Christopher Faulet98fbe952019-07-22 16:18:24 +02002193 /* Try to adjust the case of the header name */
2194 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2195 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002196 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002197 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002198 skip_hdr:
2199 h1m->state = H1_MSG_HDR_L2_LWS;
2200 break;
2201
Christopher Faulet94b2c762019-05-24 15:28:57 +02002202 case H1_MSG_LAST_LF:
2203 if (type != HTX_BLK_EOH)
2204 goto error;
2205 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002206 h1m->state = H1_MSG_LAST_LF;
2207 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002208 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002209 /* If the reply comes from haproxy while the request is
2210 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002211 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2212 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2213 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002214 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2215 /* T-E + C-L: force close */
2216 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2217 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2218 }
2219 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2220 /* T-E + HTTP/1.0: force close */
2221 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2222 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2223 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002224
2225 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002226 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002227 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002228 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002229 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002230 /* Try to adjust the case of the header name */
2231 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2232 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002233 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002234 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002235 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002236 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002237 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002238
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002239 if ((h1s->meth != HTTP_METH_CONNECT &&
2240 (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 +01002241 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002242 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002243 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002244 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2245 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002246 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002247 n = ist("transfer-encoding");
2248 v = ist("chunked");
2249 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2250 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002251 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002252 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002253 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002254 h1m->flags |= H1_MF_CHNK;
2255 }
2256
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002257 /* Now add the server name to a header (if requested) */
2258 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002259 !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002260 struct server *srv = objt_server(h1c->conn->target);
2261
2262 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002263 n = h1c->px->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002264 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002265
2266 /* Try to adjust the case of the header name */
2267 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2268 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002269 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002270 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002271 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002272 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002273 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2274 }
2275
Amaury Denoyellec1938232020-12-11 17:53:03 +01002276 /* Add websocket handshake key if needed */
Christopher Faulet62138aa2022-11-02 08:42:08 +01002277 if (!(h1s->flags & H1S_F_HAVE_WS_KEY) &&
2278 (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 +01002279 if (!(h1m->flags & H1_MF_RESP)) {
2280 /* generate a random websocket key
2281 * stored in the session to
2282 * verify it on the response side
2283 */
2284 h1_generate_random_ws_input_key(h1s->ws_key);
2285
2286 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2287 ist(h1s->ws_key),
2288 &tmp)) {
2289 goto full;
2290 }
2291 }
2292 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002293 /* add the response header key */
2294 char key[29];
2295 h1_calculate_ws_output_key(h1s->ws_key, key);
2296 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2297 ist(key),
2298 &tmp)) {
2299 goto full;
2300 }
2301 }
Christopher Faulet62138aa2022-11-02 08:42:08 +01002302 h1s->flags |= H1S_F_HAVE_WS_KEY;
Amaury Denoyellec1938232020-12-11 17:53:03 +01002303 }
2304
Christopher Faulet6b81df72019-10-01 22:08:43 +02002305 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2306 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2307
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002308 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002309 if (!chunk_memcat(&tmp, "\r\n", 2))
2310 goto full;
2311 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002312 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002313 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002314 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002315 if (!chunk_memcat(&tmp, "\r\n", 2))
2316 goto full;
2317 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002318 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002319 else if ((h1m->flags & H1_MF_RESP) &&
2320 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002321 if (!chunk_memcat(&tmp, "\r\n", 2))
2322 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002323 h1m_init_res(&h1s->res);
2324 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002325 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002326 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002327 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002328 else {
Christopher Faulet2eb52432022-04-07 10:29:38 +02002329 /* EOM flag is set or empty payload (C-L to 0) and it is the last block */
2330 if (htx_is_unique_blk(chn_htx, blk) &&
2331 ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) {
Mickael Torres226082d2022-11-16 14:29:37 +01002332 if ((h1m->flags & H1_MF_CHNK) && !(h1s->flags & H1S_F_BODYLESS_RESP)) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002333 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2334 goto full;
2335 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002336 else if (!chunk_memcat(&tmp, "\r\n", 2))
2337 goto full;
2338 goto done;
2339 }
2340 else if (!chunk_memcat(&tmp, "\r\n", 2))
2341 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002342 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002343 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002344 break;
2345
Christopher Faulet94b2c762019-05-24 15:28:57 +02002346 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002347 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002348 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002349 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2350 goto trailers;
2351
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002352 /* If the message is not chunked, never
2353 * add the last chunk. */
2354 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002355 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002356 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 +02002357 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002358 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002359 else if (type != HTX_BLK_DATA)
2360 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002361
2362 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 +02002363
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002364 /* It is the last block of this message. After this one,
2365 * only tunneled data may be forwarded. */
2366 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2367 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2368 last_data = 1;
2369 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002370
2371 if (vlen > count) {
2372 /* Get the maximum amount of data we can xferred */
2373 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002374 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002375 }
2376
Christopher Faulet140f1a52021-11-26 16:37:55 +01002377 if (h1m->state == H1_MSG_DATA) {
2378 if (h1m->flags & H1_MF_CLEN) {
2379 if (vlen > h1m->curr_len) {
2380 TRACE_ERROR("too much payload, more than announced",
2381 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2382 goto error;
2383 }
Christopher Faulet140f1a52021-11-26 16:37:55 +01002384 }
2385 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2386 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2387 goto skip_data;
2388 }
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002389 }
2390
Christopher Fauletd9233f02019-10-14 14:01:24 +02002391 chklen = 0;
2392 if (h1m->flags & H1_MF_CHNK) {
2393 chklen = b_room(&tmp);
2394 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2395 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2396 (chklen < 1048576) ? 5 : 8);
2397 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002398
2399 /* If it is the end of the chunked message (without EOT), reserve the
2400 * last chunk size */
2401 if (last_data)
2402 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002403 }
2404
2405 if (vlen + chklen > b_room(&tmp)) {
2406 /* too large for the buffer */
2407 if (chklen >= b_room(&tmp))
2408 goto full;
2409 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002410 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002411 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002412 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002413 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002414 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002415 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002416
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002417 /* Space already reserved, so it must succeed */
2418 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2419 goto error;
2420
Christopher Faulet6b81df72019-10-01 22:08:43 +02002421 if (h1m->state == H1_MSG_DATA)
2422 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002423 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002424 else
2425 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled 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 Faulet0d7e6342021-02-08 15:56:36 +01002427
2428 skip_data:
Christopher Faulet2eb52432022-04-07 10:29:38 +02002429 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Fauletb4eca0e2022-01-10 17:27:51 +01002430 h1m->curr_len -= vlen;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002431 if (!h1m->curr_len)
2432 last_data = 1;
2433 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002434 if (last_data)
2435 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002436 break;
2437
Christopher Faulet94b2c762019-05-24 15:28:57 +02002438 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002439 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002440 goto error;
2441 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002442 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002443
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002444 if (!(h1m->flags & H1_MF_CHNK))
2445 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002446
Christopher Faulete5596bf2020-12-02 16:13:22 +01002447 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2448 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 +01002449 if (type == HTX_BLK_EOT)
2450 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002451 break;
2452 }
2453
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002454 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002455 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002456 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002457 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2458 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002459 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002460 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002461 else { // HTX_BLK_TLR
2462 n = htx_get_blk_name(chn_htx, blk);
2463 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002464
2465 /* Try to adjust the case of the header name */
2466 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2467 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002468 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002469 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002470 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002471 break;
2472
Christopher Faulet94b2c762019-05-24 15:28:57 +02002473 case H1_MSG_DONE:
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002474 /* If the message is not chunked, ignore
2475 * trailers. It may happen with H2 messages. */
2476 if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && !(h1m->flags & H1_MF_CHNK))
2477 break;
2478
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002479 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2480 goto error; /* For now return an error */
2481
Christopher Faulet94b2c762019-05-24 15:28:57 +02002482 done:
2483 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002484 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002485 h1s->flags |= H1S_F_TX_BLK;
2486 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002487 }
2488 else if ((h1m->flags & H1_MF_RESP) &&
2489 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2490 /* a successful reply to a CONNECT or a protocol switching is sent
2491 * to the client. Switch the response to tunnel mode.
2492 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002493 h1_set_tunnel_mode(h1s);
Christopher Fauletf3158e92019-11-15 11:14:23 +01002494 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002495
Christopher Faulet10a86702021-04-07 14:18:11 +02002496 if (h1s->flags & H1S_F_RX_BLK) {
2497 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002498 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002499 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 +02002500 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002501
2502 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2503 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002504 break;
2505
Christopher Faulet9768c262018-10-22 09:34:31 +02002506 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002507 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002508 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002509 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002510 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletfc473a62022-10-05 08:22:33 +02002511 se_fl_set(h1s->sd, SE_FL_ERROR);
2512 TRACE_ERROR("processing output error, set error on h1s",
2513 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01002514 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02002515 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002516
2517 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002518 total += vlen;
2519 count -= vlen;
2520 if (sz == vlen)
2521 blk = htx_remove_blk(chn_htx, blk);
2522 else {
2523 htx_cut_data_blk(chn_htx, blk, vlen);
2524 break;
2525 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002526 }
2527
Christopher Faulet9768c262018-10-22 09:34:31 +02002528 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002529 /* when the output buffer is empty, tmp shares the same area so that we
2530 * only have to update pointers and lengths.
2531 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002532 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2533 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002534 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002535 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002536
Willy Tarreau3815b222018-12-11 19:50:43 +01002537 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002538 out:
2539 if (!buf_room_for_htx_data(&h1c->obuf)) {
2540 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002541 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002542 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002543 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002544 /* Both the request and the response reached the DONE state. So set EOI
Willy Tarreau4596fe22022-05-17 19:07:51 +02002545 * flag on the stream connector. Most of time, the flag will already be set,
Christopher Fauletdea24742021-01-22 15:12:30 +01002546 * except for protocol upgrades. Report an error if data remains blocked
2547 * in the output buffer.
2548 */
2549 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002550 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletdea24742021-01-22 15:12:30 +01002551 if (!htx_is_empty(chn_htx)) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002552 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
2553 h1s->flags |= H1S_F_PROCESSING_ERROR;
2554 se_fl_set(h1s->sd, SE_FL_ERROR);
2555 TRACE_ERROR("txn done but data waiting to be sent, set error on h1s",
2556 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002557 }
Christopher Fauletdea24742021-01-22 15:12:30 +01002558 }
2559
Christopher Faulet6b81df72019-10-01 22:08:43 +02002560 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002561 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002562
2563 full:
2564 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2565 h1c->flags |= H1C_F_OUT_FULL;
2566 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002567}
2568
Christopher Faulet51dbc942018-09-13 09:05:15 +02002569/*********************************************************/
2570/* functions below are I/O callbacks from the connection */
2571/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002572static void h1_wake_stream_for_recv(struct h1s *h1s)
2573{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002574 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002575 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002576 tasklet_wakeup(h1s->subs->tasklet);
2577 h1s->subs->events &= ~SUB_RETRY_RECV;
2578 if (!h1s->subs->events)
2579 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002580 }
2581}
2582static void h1_wake_stream_for_send(struct h1s *h1s)
2583{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002584 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002585 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002586 tasklet_wakeup(h1s->subs->tasklet);
2587 h1s->subs->events &= ~SUB_RETRY_SEND;
2588 if (!h1s->subs->events)
2589 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002590 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002591}
2592
Christopher Fauletad4daf62021-01-21 17:49:01 +01002593/* alerts the data layer following this sequence :
2594 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2595 * - if its subscribed to send, then it's woken up for send
2596 * - if it was subscribed to neither, its ->wake() callback is called
2597 */
2598static void h1_alert(struct h1s *h1s)
2599{
2600 if (h1s->subs) {
2601 h1_wake_stream_for_recv(h1s);
2602 h1_wake_stream_for_send(h1s);
2603 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002604 else if (h1s_sc(h1s) && h1s_sc(h1s)->app_ops->wake != NULL) {
Christopher Fauletad4daf62021-01-21 17:49:01 +01002605 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002606 h1s_sc(h1s)->app_ops->wake(h1s_sc(h1s));
Christopher Fauletad4daf62021-01-21 17:49:01 +01002607 }
2608}
2609
Christopher Fauletc18fc232020-10-06 17:41:36 +02002610/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
Christopher Faulet56a49942022-10-04 17:45:24 +02002611 * and 0 on error. The flag H1C_F_ABRT_PENDING is set on the H1 connection for
Christopher Fauletc18fc232020-10-06 17:41:36 +02002612 * retryable errors (allocation error or buffer full). On success, the error is
2613 * copied in the output buffer.
2614*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002615static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002616{
2617 int rc = http_get_status_idx(h1c->errcode);
2618 int ret = 0;
2619
2620 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2621
2622 /* Verify if the error is mapped on /dev/null or any empty file */
2623 /// XXX: do a function !
2624 if (h1c->px->replies[rc] &&
2625 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2626 h1c->px->replies[rc]->body.errmsg &&
2627 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2628 /* Empty error, so claim a success */
2629 ret = 1;
2630 goto out;
2631 }
2632
2633 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002634 h1c->flags |= H1C_F_ABRT_PENDING;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002635 goto out;
2636 }
2637
2638 if (!h1_get_buf(h1c, &h1c->obuf)) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002639 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ABRT_PENDING);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002640 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2641 goto out;
2642 }
Tim Duesterhus77508502022-03-15 13:11:06 +01002643 ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc]));
Christopher Fauletc18fc232020-10-06 17:41:36 +02002644 if (unlikely(ret <= 0)) {
2645 if (!ret) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002646 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ABRT_PENDING);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002647 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2648 goto out;
2649 }
2650 else {
2651 /* we cannot report this error, so claim a success */
2652 ret = 1;
2653 }
2654 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02002655
Christopher Fauletda938022022-12-15 09:22:35 +01002656 if (h1c->state == H1_CS_EMBRYONIC) {
Christopher Faulet8f1f1b02022-12-15 09:59:50 +01002657 BUG_ON(h1c->h1s == NULL || h1s_sc(h1c->h1s) != NULL);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002658 TRACE_DEVEL("Abort embryonic H1S", H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2659 h1s_destroy(h1c->h1s);
2660 }
2661
Christopher Fauleta1a76ce2022-11-23 17:07:48 +01002662 h1c->flags = (h1c->flags & ~(H1C_F_WAIT_NEXT_REQ|H1C_F_ABRT_PENDING)) | H1C_F_ABRTED;
Christopher Faulet061a0982022-11-29 17:16:30 +01002663 h1_close(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002664 out:
2665 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2666 return ret;
2667}
2668
2669/* Try to send a 500 internal error. It relies on h1_send_error to send the
2670 * error. This function takes care of incrementing stats and tracked counters.
2671 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002672static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002673{
2674 struct session *sess = h1c->conn->owner;
Christopher Fauletd1b57302022-11-23 17:13:12 +01002675 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002676
2677 session_inc_http_req_ctr(sess);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002678 proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1);
Willy Tarreau4781b152021-04-06 13:53:36 +02002679 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2680 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002681 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002682 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002683
2684 h1c->errcode = 500;
2685 ret = h1_send_error(h1c);
2686 sess_log(sess);
2687 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002688}
2689
Christopher Fauletb3230f72021-09-21 18:38:20 +02002690/* Try to send an error because of a parsing error. By default a 400 bad request
2691 * error is returned. But the status code may be specified by setting
2692 * h1c->errcode. It relies on h1_send_error to send the error. This function
2693 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002694 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002695static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002696{
2697 struct session *sess = h1c->conn->owner;
Christopher Fauletd1b57302022-11-23 17:13:12 +01002698 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002699
Christopher Faulet4e72b172022-10-04 17:35:19 +02002700 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 +01002701 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED;
Christopher Faulet061a0982022-11-29 17:16:30 +01002702 h1_close(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002703 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002704 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002705
2706 session_inc_http_req_ctr(sess);
2707 session_inc_http_err_ctr(sess);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002708 proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1);
Willy Tarreau4781b152021-04-06 13:53:36 +02002709 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2710 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002711 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002712 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002713
Christopher Fauletb3230f72021-09-21 18:38:20 +02002714 if (!h1c->errcode)
2715 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002716 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002717 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2718 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002719
2720 end:
2721 return ret;
2722}
2723
Christopher Faulet2eed8002020-12-07 11:26:13 +01002724/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2725 * the error. This function takes care of incrementing stats and tracked
2726 * counters.
2727 */
2728static int h1_handle_not_impl_err(struct h1c *h1c)
2729{
2730 struct session *sess = h1c->conn->owner;
Christopher Fauletd1b57302022-11-23 17:13:12 +01002731 int ret = 0;
Christopher Faulet2eed8002020-12-07 11:26:13 +01002732
Christopher Faulet4e72b172022-10-04 17:35:19 +02002733 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 +01002734 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED;
Christopher Faulet061a0982022-11-29 17:16:30 +01002735 h1_close(h1c);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002736 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002737 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002738
2739 session_inc_http_req_ctr(sess);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002740 proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1);
Willy Tarreau4781b152021-04-06 13:53:36 +02002741 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2742 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002743 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002744 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002745
2746 h1c->errcode = 501;
2747 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002748 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2749 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002750
2751 end:
2752 return ret;
2753}
2754
Christopher Fauletc18fc232020-10-06 17:41:36 +02002755/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2756 * error. This function takes care of incrementing stats and tracked counters.
2757 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002758static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002759{
2760 struct session *sess = h1c->conn->owner;
Christopher Fauletd1b57302022-11-23 17:13:12 +01002761 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002762
Christopher Faulet4e72b172022-10-04 17:35:19 +02002763 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 +01002764 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED;
Christopher Faulet061a0982022-11-29 17:16:30 +01002765 h1_close(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002766 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002767 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002768
2769 session_inc_http_req_ctr(sess);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002770 proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1);
Willy Tarreau4781b152021-04-06 13:53:36 +02002771 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2772 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002773 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002774 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002775
2776 h1c->errcode = 408;
Christopher Faulet22742442022-11-23 16:58:22 +01002777 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002778 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
Christopher Faulet22742442022-11-23 16:58:22 +01002779 sess_log(sess);
2780
Christopher Fauletc18fc232020-10-06 17:41:36 +02002781 end:
2782 return ret;
2783}
2784
2785
Christopher Faulet51dbc942018-09-13 09:05:15 +02002786/*
2787 * Attempt to read data, and subscribe if none available
2788 */
2789static int h1_recv(struct h1c *h1c)
2790{
2791 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002792 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002793 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002794
Christopher Faulet6b81df72019-10-01 22:08:43 +02002795 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2796
2797 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2798 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002799 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002800 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002801
Christopher Fauletae635762020-09-21 11:47:16 +02002802 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2803 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002804 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002805 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002806
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002807 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2808 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002809 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002810 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002811 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002812
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002813 /*
2814 * If we only have a small amount of data, realign it,
2815 * it's probably cheaper than doing 2 recv() calls.
2816 */
2817 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002818 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002819
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002820 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002821 if (!h1c->h1s ||
2822 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2823 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002824 flags |= CO_RFL_READ_ONCE;
2825
Willy Tarreau45f2b892018-12-05 07:59:27 +01002826 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002827 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002828 if (h1c->flags & H1C_F_IN_FULL) {
2829 h1c->flags &= ~H1C_F_IN_FULL;
2830 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2831 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002832
2833 if (!b_data(&h1c->ibuf)) {
2834 /* try to pre-align the buffer like the rxbufs will be
2835 * to optimize memory copies.
2836 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002837 h1c->ibuf.head = sizeof(struct htx);
2838 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002839 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet41951ab2021-11-26 18:12:51 +01002840 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002841 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02002842
2843 if (conn_xprt_read0_pending(conn)) {
2844 TRACE_DEVEL("read0 on connection", H1_EV_H1C_RECV, h1c->conn);
2845 h1c->flags |= H1C_F_EOS;
2846 }
2847 if (h1c->conn->flags & CO_FL_ERROR) {
2848 TRACE_DEVEL("connection error", H1_EV_H1C_RECV, h1c->conn);
2849 h1c->flags |= H1C_F_ERROR;
2850 }
2851
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002852 if (max && !ret && h1_recv_allowed(h1c)) {
2853 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2854 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002855 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002856 else {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002857 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 +02002858 h1_wake_stream_for_recv(h1c->h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002859 }
2860
Christopher Faulet51dbc942018-09-13 09:05:15 +02002861 if (!b_data(&h1c->ibuf))
2862 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002863 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002864 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002865 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2866 }
2867
2868 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002869 return !!ret || (h1c->flags & (H1C_F_EOS|H1C_F_ERROR));
Christopher Faulet51dbc942018-09-13 09:05:15 +02002870}
2871
2872
2873/*
2874 * Try to send data if possible
2875 */
2876static int h1_send(struct h1c *h1c)
2877{
2878 struct connection *conn = h1c->conn;
2879 unsigned int flags = 0;
2880 size_t ret;
2881 int sent = 0;
2882
Christopher Faulet6b81df72019-10-01 22:08:43 +02002883 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2884
Christopher Fauletfc473a62022-10-05 08:22:33 +02002885 if (h1c->flags & (H1C_F_ERROR|H1C_F_ERR_PENDING)) {
2886 TRACE_DEVEL("leaving on H1C error|err_pending", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002887 b_reset(&h1c->obuf);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002888 if (h1c->flags & H1C_F_EOS)
2889 h1c->flags |= H1C_F_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002890 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002891 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002892
2893 if (!b_data(&h1c->obuf))
2894 goto end;
2895
Christopher Faulet46230362019-10-17 16:04:20 +02002896 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002897 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002898 if (h1c->flags & H1C_F_CO_STREAMER)
2899 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002900
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002901 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002902 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002903 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002904 if (h1c->flags & H1C_F_OUT_FULL) {
2905 h1c->flags &= ~H1C_F_OUT_FULL;
2906 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2907 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01002908 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002909 b_del(&h1c->obuf, ret);
2910 sent = 1;
2911 }
2912
Christopher Fauletfc473a62022-10-05 08:22:33 +02002913 if (conn->flags & CO_FL_ERROR) {
2914 /* connection error, nothing to send, clear the buffer to release it */
2915 TRACE_DEVEL("connection error", H1_EV_H1C_SEND, h1c->conn);
2916 h1c->flags |= H1C_F_ERR_PENDING;
2917 if (h1c->flags & H1C_F_EOS)
2918 h1c->flags |= H1C_F_ERROR;
Christopher Fauleta462ee02022-11-22 17:16:22 +01002919 else if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
2920 /* EOS not seen, so subscribe for reads to be able to
2921 * catch the error on the reading path. It is especially
2922 * important if EOI was reached.
2923 */
2924 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2925 }
Christopher Faulet145aa472018-12-06 10:56:20 +01002926 b_reset(&h1c->obuf);
2927 }
2928
Christopher Faulet51dbc942018-09-13 09:05:15 +02002929 end:
Christopher Fauletc17c31c2022-02-01 18:25:06 +01002930 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002931 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002932
Christopher Faulet51dbc942018-09-13 09:05:15 +02002933 /* We're done, no more to send */
2934 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002935 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002936 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002937 if (h1c->state == H1_CS_CLOSING) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002938 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002939 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002940 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002941 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002942 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2943 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002944 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002945 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002946
Christopher Faulet6b81df72019-10-01 22:08:43 +02002947 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002948 return sent || (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) || (h1c->state == H1_CS_CLOSED);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002949}
2950
Christopher Faulet51dbc942018-09-13 09:05:15 +02002951/* callback called on any event by the connection handler.
2952 * It applies changes and returns zero, or < 0 if it wants immediate
2953 * destruction of the connection.
2954 */
2955static int h1_process(struct h1c * h1c)
2956{
2957 struct connection *conn = h1c->conn;
2958
Christopher Faulet6b81df72019-10-01 22:08:43 +02002959 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2960
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002961 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002962 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Faulet4e72b172022-10-04 17:35:19 +02002963 (h1c->state < H1_CS_RUNNING) && /* IDLE, EMBRYONIC or UPGRADING */
Christopher Fauletfc473a62022-10-05 08:22:33 +02002964 !(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 +02002965 struct h1s *h1s = h1c->h1s;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002966 struct buffer *buf;
2967 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002968
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002969 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2970 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002971 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002972
2973 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002974 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002975 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2976 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002977 /* Try to match H2 preface before parsing the request headers. */
2978 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2979 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002980 if (h1c->state == H1_CS_UPGRADING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02002981 BUG_ON(!h1s);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002982 se_fl_set(h1s->sd, SE_FL_EOS); /* Set EOS here to release the SC */
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002983 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002984 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002985 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002986 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002987 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002988
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002989 /* Create the H1 stream if not already there */
2990 if (!h1s) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002991 h1s = h1c_frt_stream_new(h1c, NULL, h1c->conn->owner);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002992 if (!h1s) {
2993 b_reset(&h1c->ibuf);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002994 h1_handle_internal_err(h1c);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002995 TRACE_ERROR("alloc error", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002996 goto no_parsing;
2997 }
2998 }
2999
3000 if (h1s->sess->t_idle == -1)
3001 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
3002
3003 /* Get the stream rxbuf */
3004 buf = h1_get_buf(h1c, &h1s->rxbuf);
3005 if (!buf) {
3006 h1c->flags |= H1C_F_IN_SALLOC;
3007 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
3008 return 0;
3009 }
3010
3011 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003012 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003013 h1_release_buf(h1c, &h1s->rxbuf);
3014 h1_set_idle_expiration(h1c);
Christopher Fauletad4ed002022-12-16 11:13:00 +01003015 if (h1c->state < H1_CS_RUNNING) {
3016 if (h1s->flags & H1S_F_INTERNAL_ERROR) {
3017 h1_handle_internal_err(h1c);
3018 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
3019 }
3020 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
3021 h1_handle_not_impl_err(h1c);
3022 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
3023 }
3024 else if (h1s->flags & H1S_F_PARSING_ERROR || se_fl_test(h1s->sd, SE_FL_ERROR)) {
3025 h1_handle_parsing_error(h1c);
3026 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
3027 }
3028 else {
3029 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
3030 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3031 }
Christopher Faulet2177d962022-10-05 08:39:14 +02003032 }
Christopher Fauletae635762020-09-21 11:47:16 +02003033 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003034
Christopher Faulete6ef4cd2022-11-17 15:54:12 +01003035 no_parsing:
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003036 h1_send(h1c);
3037
Christopher Faulet75308302021-11-15 14:51:37 +01003038 /* H1 connection must be released ASAP if:
Christopher Fauletfc473a62022-10-05 08:22:33 +02003039 * - an error occurred on the H1C or
Christopher Faulet75308302021-11-15 14:51:37 +01003040 * - a read0 was received or
3041 * - a silent shutdown was emitted and all outgoing data sent
3042 */
Christopher Faulet31da34d2022-10-10 16:36:10 +02003043 if ((h1c->flags & (H1C_F_EOS|H1C_F_ERROR|H1C_F_ABRT_PENDING|H1C_F_ABRTED)) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003044 (h1c->state >= H1_CS_CLOSING && (h1c->flags & H1C_F_SILENT_SHUT) && !b_data(&h1c->obuf))) {
3045 if (h1c->state != H1_CS_RUNNING) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003046 /* No stream connector or upgrading */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003047 if (h1c->state < H1_CS_RUNNING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ERROR))) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003048 /* shutdown for reads and no error on the frontend connection: Send an error */
Christopher Fauletb3230f72021-09-21 18:38:20 +02003049 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003050 h1_send(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003051 }
Christopher Faulet56a49942022-10-04 17:45:24 +02003052 else if (h1c->flags & H1C_F_ABRT_PENDING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003053 /* Handle pending error, if any (only possible on frontend connection) */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003054 BUG_ON(h1c->flags & H1C_F_IS_BACK);
3055 if (h1_send_error(h1c))
3056 h1_send(h1c);
3057 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02003058 else {
3059 h1_close(h1c);
3060 TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn);
3061 }
Christopher Fauletae635762020-09-21 11:47:16 +02003062
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003063 /* If there is some pending outgoing data or error, just wait */
Christopher Faulet56a49942022-10-04 17:45:24 +02003064 if (h1c->state == H1_CS_CLOSING || (h1c->flags & H1C_F_ABRT_PENDING))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003065 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01003066
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003067 /* Otherwise we can release the H1 connection */
3068 goto release;
3069 }
3070 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003071 struct h1s *h1s = h1c->h1s;
3072
Willy Tarreau4596fe22022-05-17 19:07:51 +02003073 /* Here there is still a H1 stream with a stream connector.
Christopher Fauletfc473a62022-10-05 08:22:33 +02003074 * Report an error at the stream level and wake up the stream
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003075 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003076 BUG_ON(!h1s);
3077
Christopher Fauletfc473a62022-10-05 08:22:33 +02003078 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3079 se_fl_set_error(h1s->sd);
3080 TRACE_STATE("report (ERR_PENDING|ERROR) to SE", H1_EV_H1C_RECV, conn, h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003081 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003082 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01003083 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003084 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003085 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003086
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003087 if (!b_data(&h1c->ibuf))
3088 h1_release_buf(h1c, &h1c->ibuf);
3089
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003090 /* Check if a soft-stop is in progress.
3091 * Release idling front connection if this is the case.
3092 */
3093 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003094 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
William Dauchya9dd9012022-01-05 22:53:24 +01003095 if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) &&
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003096 h1c->flags & H1C_F_WAIT_NEXT_REQ) {
3097
3098 int send_close = 1;
3099 /* If a close-spread-time option is set, we want to avoid
3100 * closing all the active HTTP2 connections at once so we add a
3101 * random factor that will spread the closing.
3102 */
3103 if (tick_isset(global.close_spread_end)) {
3104 int remaining_window = tick_remain(now_ms, global.close_spread_end);
3105 if (remaining_window) {
3106 /* This should increase the closing rate the
3107 * further along the window we are.
3108 */
3109 send_close = (remaining_window <= statistical_prng_range(global.close_spread_time));
3110 }
3111 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02003112 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
3113 send_close = 0; /* let the client close his connection himself */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003114 if (send_close)
3115 goto release;
3116 }
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003117 }
3118 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003119
Christopher Faulet4e72b172022-10-04 17:35:19 +02003120 if (h1c->state == H1_CS_RUNNING && (h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1c->h1s)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003121 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 +02003122 h1_wake_stream_for_recv(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003123 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003124
Christopher Faulet47365272018-10-31 17:40:50 +01003125 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02003126 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003127 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003128 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01003129
3130 release:
Christopher Faulet4e72b172022-10-04 17:35:19 +02003131 if (h1c->state == H1_CS_UPGRADING) {
3132 struct h1s *h1s = h1c->h1s;
3133
3134 /* Don't release the H1 connection right now, we must destroy
3135 * the attached SC first */
3136 BUG_ON(!h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003137
Christopher Fauletfc473a62022-10-05 08:22:33 +02003138 if (h1c->flags & H1C_F_EOS) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003139 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003140 TRACE_STATE("report EOS to SE", H1_EV_H1C_RECV, conn, h1s);
3141 }
3142 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3143 se_fl_set_error(h1s->sd);
3144 TRACE_STATE("report (ERR_PENDING|ERROR) to SE", H1_EV_H1C_RECV, conn, h1s);
3145 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003146 h1_alert(h1s);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003147 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003148 }
3149 else {
3150 h1_release(h1c);
3151 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
3152 }
Christopher Faulet539e0292018-11-19 10:40:09 +01003153 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003154}
3155
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003156struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003157{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003158 struct connection *conn;
3159 struct tasklet *tl = (struct tasklet *)t;
3160 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003161 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003162 int ret = 0;
3163
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003164 if (state & TASK_F_USR1) {
3165 /* the tasklet was idling on an idle connection, it might have
3166 * been stolen, let's be careful!
3167 */
3168 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3169 if (tl->context == NULL) {
3170 /* The connection has been taken over by another thread,
3171 * we're no longer responsible for it, so just free the
3172 * tasklet, and do nothing.
3173 */
3174 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3175 tasklet_free(tl);
3176 return NULL;
3177 }
3178 conn = h1c->conn;
3179 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003180
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003181 /* Remove the connection from the list, to be sure nobody attempts
3182 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003183 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003184 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3185 if (conn_in_list)
3186 conn_delete_from_tree(&conn->hash_node->node);
3187
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003188 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003189 } else {
3190 /* we're certain the connection was not in an idle list */
3191 conn = h1c->conn;
3192 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
3193 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003194 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003195
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003196 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003197 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003198 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003199 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003200 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003201 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01003202
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003203 /* If we were in an idle list, we want to add it back into it,
3204 * unless h1_process() returned -1, which mean it has destroyed
3205 * the connection (testing !ret is enough, if h1_process() wasn't
3206 * called then ret will be 0 anyway.
3207 */
Willy Tarreau74163142021-03-13 11:30:19 +01003208 if (ret < 0)
3209 t = NULL;
3210
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003211 if (!ret && conn_in_list) {
3212 struct server *srv = objt_server(conn->target);
3213
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003214 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003215 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02003216 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003217 else
Willy Tarreau85223482022-09-29 20:32:43 +02003218 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003219 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003220 }
Willy Tarreau74163142021-03-13 11:30:19 +01003221 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003222}
3223
Christopher Faulet51dbc942018-09-13 09:05:15 +02003224static int h1_wake(struct connection *conn)
3225{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003226 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01003227 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003228
Christopher Faulet6b81df72019-10-01 22:08:43 +02003229 TRACE_POINT(H1_EV_H1C_WAKE, conn);
3230
Christopher Faulet539e0292018-11-19 10:40:09 +01003231 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01003232 ret = h1_process(h1c);
3233 if (ret == 0) {
3234 struct h1s *h1s = h1c->h1s;
3235
Christopher Faulet4e72b172022-10-04 17:35:19 +02003236 if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING)
Christopher Fauletad4daf62021-01-21 17:49:01 +01003237 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003238 }
3239 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003240}
3241
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003242/* Connection timeout management. The principle is that if there's no receipt
3243 * nor sending for a certain amount of time, the connection is closed.
3244 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003245struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003246{
3247 struct h1c *h1c = context;
3248 int expired = tick_is_expired(t->expire, now_ms);
3249
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003250 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003251
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003252 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003253 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003254 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003255
3256 /* Somebody already stole the connection from us, so we should not
3257 * free it, we just have to free the task.
3258 */
3259 if (!t->context) {
3260 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003261 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003262 goto do_leave;
3263 }
3264
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003265 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003266 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003267 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003268 return t;
3269 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003270
Willy Tarreau4596fe22022-05-17 19:07:51 +02003271 /* If a stream connector is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003272 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003273 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003274 if (h1c->state == H1_CS_RUNNING) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003275 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003276 t->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +02003277 TRACE_DEVEL("leaving (SC still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003278 return t;
3279 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003280
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003281 /* Try to send an error to the client */
Christopher Faulet56a49942022-10-04 17:45:24 +02003282 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 +01003283 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003284 if (h1_handle_req_tout(h1c))
3285 h1_send(h1c);
Christopher Faulet56a49942022-10-04 17:45:24 +02003286 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ABRT_PENDING)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003287 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003288 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003289 return t;
3290 }
3291 }
3292
Christopher Fauletf75cc542022-11-22 17:06:13 +01003293 if (h1c->h1s && !se_fl_test(h1c->h1s->sd, SE_FL_ORPHAN)) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003294 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet4e72b172022-10-04 17:35:19 +02003295 * attached SC first. */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003296 se_fl_set(h1c->h1s->sd, SE_FL_EOS | SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003297 h1_alert(h1c->h1s);
3298 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003299 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003300 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003301 return t;
3302 }
3303
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003304 /* We're about to destroy the connection, so make sure nobody attempts
3305 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003306 */
Christopher Faulet5e1b0e72023-02-28 15:39:38 +01003307 if (h1c->conn->flags & CO_FL_LIST_MASK) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003308 conn_delete_from_tree(&h1c->conn->hash_node->node);
Christopher Faulet5e1b0e72023-02-28 15:39:38 +01003309 h1c->conn->flags &= ~CO_FL_LIST_MASK;
3310 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003311
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003312 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003313 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003314
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003315 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003316 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003317
3318 if (!h1c) {
3319 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003320 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003321 return NULL;
3322 }
3323
3324 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003325 h1_release(h1c);
3326 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003327 return NULL;
3328}
3329
Christopher Faulet51dbc942018-09-13 09:05:15 +02003330/*******************************************/
3331/* functions below are used by the streams */
3332/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003333
Christopher Faulet51dbc942018-09-13 09:05:15 +02003334/*
3335 * Attach a new stream to a connection
3336 * (Used for outgoing connections)
3337 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003338static int h1_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003339{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003340 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003341 struct h1s *h1s;
3342
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003343 /* this connection is no more idle (if it was at all) */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003344 h1c->flags &= ~H1C_F_SILENT_SHUT;
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003345
Christopher Faulet6b81df72019-10-01 22:08:43 +02003346 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003347 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003348 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3349 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003350 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003351
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003352 h1s = h1c_bck_stream_new(h1c, sd->sc, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003353 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003354 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3355 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003356 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003357
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003358 /* the connection is not idle anymore, let's mark this */
3359 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003360 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003361
Christopher Faulet6b81df72019-10-01 22:08:43 +02003362 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulete00ad352021-12-16 14:44:31 +01003363 return 0;
Christopher Faulet26a26432021-01-27 11:27:50 +01003364 err:
Christopher Faulet26a26432021-01-27 11:27:50 +01003365 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 +01003366 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003367}
3368
Willy Tarreau4596fe22022-05-17 19:07:51 +02003369/* Retrieves a valid stream connector from this connection, or returns NULL.
3370 * For this mux, it's easy as we can only store a single stream connector.
Christopher Faulet51dbc942018-09-13 09:05:15 +02003371 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003372static struct stconn *h1_get_first_sc(const struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003373{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003374 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003375 struct h1s *h1s = h1c->h1s;
3376
3377 if (h1s)
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02003378 return h1s_sc(h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003379
3380 return NULL;
3381}
3382
Christopher Faulet73c12072019-04-08 11:23:22 +02003383static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003384{
Christopher Faulet73c12072019-04-08 11:23:22 +02003385 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003386
Christopher Faulet6b81df72019-10-01 22:08:43 +02003387 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet7c452cc2022-04-14 11:08:26 +02003388 if (!h1c->h1s || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003389 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003390}
3391
3392/*
3393 * Detach the stream from the connection and possibly release the connection.
3394 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003395static void h1_detach(struct sedesc *sd)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003396{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003397 struct h1s *h1s = sd->se;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003398 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003399 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003400 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003401
Christopher Faulet6b81df72019-10-01 22:08:43 +02003402 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3403
Christopher Faulet6b81df72019-10-01 22:08:43 +02003404 if (!h1s) {
3405 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003406 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003407 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003408
Olivier Houchardf502aca2018-12-14 19:42:40 +01003409 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003410 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003411
Christopher Faulet42849b02020-10-05 11:35:17 +02003412 sess->accept_date = date;
3413 sess->tv_accept = now;
3414 sess->t_handshake = 0;
3415 sess->t_idle = -1;
3416
Olivier Houchard8a786902018-12-15 16:05:40 +01003417 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3418 h1s_destroy(h1s);
3419
Christopher Faulet4e72b172022-10-04 17:35:19 +02003420 if (h1c->state == H1_CS_IDLE && (h1c->flags & H1C_F_IS_BACK)) {
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003421 /* this connection may be killed at any moment, we want it to
3422 * die "cleanly" (i.e. only an RST).
3423 */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003424 h1c->flags |= H1C_F_SILENT_SHUT;
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003425
Christopher Fauletf1204b82019-07-19 14:51:06 +02003426 /* If there are any excess server data in the input buffer,
3427 * release it and close the connection ASAP (some data may
3428 * remain in the output buffer). This happens if a server sends
3429 * invalid responses. So in such case, we don't want to reuse
3430 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003431 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003432 if (b_data(&h1c->ibuf)) {
3433 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet061a0982022-11-29 17:16:30 +01003434 h1_close(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003435 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003436 goto release;
3437 }
Christopher Faulet03627242019-07-19 11:34:08 +02003438
Christopher Faulet08016ab2020-07-01 16:10:06 +02003439 if (h1c->conn->flags & CO_FL_PRIVATE) {
3440 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003441 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3442 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003443 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003444 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003445 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003446 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003447 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003448 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003449 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3450 goto end;
3451 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003452 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003453 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003454 if (h1c->conn->owner == sess)
3455 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003456
3457 /* mark that the tasklet may lose its context to another thread and
3458 * that the handler needs to check it under the idle conns lock.
3459 */
3460 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003461 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003462 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3463
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003464 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003465 /* The server doesn't want it, let's kill the connection right away */
3466 h1c->conn->mux->destroy(h1c);
3467 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3468 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003469 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003470 /* At this point, the connection has been added to the
3471 * server idle list, so another thread may already have
3472 * hijacked it, so we can't do anything with it.
3473 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003474 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003475 }
3476 }
3477
Christopher Fauletf1204b82019-07-19 14:51:06 +02003478 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003479 /* 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 +02003480 if ((h1c->flags & H1C_F_ERROR) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003481 (h1c->state == H1_CS_CLOSED) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003482 (h1c->state == H1_CS_CLOSING && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003483 !h1c->conn->owner) {
3484 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003485 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003486 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003487 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003488 if (h1c->state == H1_CS_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003489 /* If we have a new request, process it immediately or
3490 * subscribe for reads waiting for new data
3491 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003492 if (unlikely(b_data(&h1c->ibuf))) {
3493 if (h1_process(h1c) == -1)
3494 goto end;
3495 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003496 else
3497 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3498 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003499 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003500 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003501 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003502 end:
3503 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003504}
3505
3506
Willy Tarreau000d63c2022-05-27 10:39:17 +02003507static void h1_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003508{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003509 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet7f366362019-04-08 10:51:20 +02003510 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003511
3512 if (!h1s)
3513 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003514 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003515
Christopher Fauletc3fe6f32022-10-05 10:24:11 +02003516 TRACE_POINT(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003517}
3518
Willy Tarreau000d63c2022-05-27 10:39:17 +02003519static void h1_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003520{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003521 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003522 struct h1c *h1c;
3523
3524 if (!h1s)
3525 return;
3526 h1c = h1s->h1c;
3527
Christopher Faulet99293b02021-11-10 10:30:15 +01003528 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003529
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003530 if (se_fl_test(h1s->sd, SE_FL_SHW))
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003531 goto end;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003532 if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003533 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003534 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003535 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003536 if (h1c->state == H1_CS_CLOSING || (h1c->flags & (H1C_F_EOS|H1C_F_ERR_PENDING|H1C_F_ERROR))) {
3537 TRACE_STATE("shutdown on connection (EOS || CLOSING || ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003538 goto do_shutw;
3539 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003540
Christopher Fauletfc473a62022-10-05 08:22:33 +02003541 if (h1c->state == H1_CS_UPGRADING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003542 TRACE_STATE("keep connection alive (UPGRADING)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003543 goto end;
3544 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003545 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3546 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003547 goto end;
3548 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003549
Christopher Faulet7f366362019-04-08 10:51:20 +02003550 do_shutw:
Christopher Faulet061a0982022-11-29 17:16:30 +01003551 h1_close(h1c);
Christopher Faulet07976562022-03-31 11:05:05 +02003552 if (mode != CO_SHW_NORMAL)
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003553 h1c->flags |= H1C_F_SILENT_SHUT;
Christopher Fauleta85c5222021-10-27 15:36:38 +02003554
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003555 if (!b_data(&h1c->obuf))
Christopher Faulet897d6122021-12-17 17:28:35 +01003556 h1_shutw_conn(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003557 end:
3558 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003559}
3560
Christopher Fauleta85c5222021-10-27 15:36:38 +02003561static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003562{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003563 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003564
Christopher Faulet4e72b172022-10-04 17:35:19 +02003565 TRACE_ENTER(H1_EV_H1C_END, conn);
3566 h1_close(h1c);
Willy Tarreau62592ad2021-03-26 09:09:42 +01003567 if (conn->flags & CO_FL_SOCK_WR_SH)
3568 return;
3569
Christopher Faulet666a0c42019-01-08 11:12:04 +01003570 conn_xprt_shutw(conn);
Christopher Fauletce7928d2022-11-18 08:44:44 +01003571 conn_sock_shutw(conn, !(h1c->flags & H1C_F_SILENT_SHUT));
Christopher Fauleta85c5222021-10-27 15:36:38 +02003572 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003573}
3574
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003575/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3576 * The <es> pointer is not allowed to differ from the one passed to the
3577 * subscribe() call. It always returns zero.
3578 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003579static int h1_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003580{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003581 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003582
3583 if (!h1s)
3584 return 0;
3585
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003586 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003587 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003588
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003589 es->events &= ~event_type;
3590 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003591 h1s->subs = NULL;
3592
3593 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003594 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003595
3596 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003597 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003598
Christopher Faulet51dbc942018-09-13 09:05:15 +02003599 return 0;
3600}
3601
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003602/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3603 * event subscriber <es> is not allowed to change from a previous call as long
3604 * as at least one event is still subscribed. The <event_type> must only be a
3605 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
Willy Tarreau000d63c2022-05-27 10:39:17 +02003606 * the stream connector <sc> was already detached, in which case it will return
Willy Tarreau4596fe22022-05-17 19:07:51 +02003607 * -1.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003608 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003609static int h1_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003610{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003611 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51bb1852019-09-04 10:22:34 +02003612 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003613
3614 if (!h1s)
3615 return -1;
3616
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003617 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003618 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003619
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003620 es->events |= event_type;
3621 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003622
3623 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003624 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003625
3626
Christopher Faulet6b81df72019-10-01 22:08:43 +02003627 if (event_type & SUB_RETRY_SEND) {
3628 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003629 /*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003630 * If the stconn attempts to subscribe, and the
Christopher Faulet6b81df72019-10-01 22:08:43 +02003631 * mux isn't subscribed to the connection, then it
3632 * probably means the connection wasn't established
3633 * yet, so we have to subscribe.
3634 */
3635 h1c = h1s->h1c;
3636 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3637 h1c->conn->xprt->subscribe(h1c->conn,
3638 h1c->conn->xprt_ctx,
3639 SUB_RETRY_SEND,
3640 &h1c->wait_event);
3641 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003642 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003643}
3644
Christopher Faulet564e39c2021-09-21 15:50:55 +02003645/* Called from the upper layer, to receive data.
3646 *
3647 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3648 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3649 * means the caller wants to flush input data (from the mux buffer and the
3650 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3651 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3652 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3653 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3654 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3655 * copy as much data as possible.
3656 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003657static size_t h1_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003658{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003659 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet539e0292018-11-19 10:40:09 +01003660 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003661 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003662 size_t ret = 0;
3663
Willy Tarreau022e5e52020-09-10 09:33:15 +02003664 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003665
Christopher Faulet4e72b172022-10-04 17:35:19 +02003666 /* Do nothing for now if not RUNNING (implies UPGRADING) */
3667 if (h1c->state < H1_CS_RUNNING) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003668 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3669 goto end;
3670 }
3671
Christopher Faulet539e0292018-11-19 10:40:09 +01003672 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003673 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003674 else
3675 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003676
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003677 if ((flags & CO_RFL_BUF_FLUSH) && se_fl_test(h1s->sd, SE_FL_MAY_SPLICE)) {
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003678 h1c->flags |= H1C_F_WANT_SPLICE;
3679 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 +02003680 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003681 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003682 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 +01003683 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003684 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003685
3686 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003687 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003688 return ret;
3689}
3690
3691
3692/* Called from the upper layer, to send data */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003693static size_t h1_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003694{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003695 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003696 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003697 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003698
3699 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003700 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003701 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003702
Willy Tarreau022e5e52020-09-10 09:33:15 +02003703 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003704
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003705 /* If we're not connected yet, or we're waiting for a handshake, stop
3706 * now, as we don't want to remove everything from the channel buffer
3707 * before we're sure we can send it.
3708 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003709 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003710 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003711 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003712 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003713
Christopher Fauletfc473a62022-10-05 08:22:33 +02003714 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3715 se_fl_set_error(h1s->sd);
Christopher Faulet26a26432021-01-27 11:27:50 +01003716 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 +01003717 return 0;
3718 }
3719
Christopher Faulet46230362019-10-17 16:04:20 +02003720 /* Inherit some flags from the upper layer */
3721 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3722 if (flags & CO_SFL_MSG_MORE)
3723 h1c->flags |= H1C_F_CO_MSG_MORE;
3724 if (flags & CO_SFL_STREAMER)
3725 h1c->flags |= H1C_F_CO_STREAMER;
3726
Christopher Fauletc31872f2019-06-04 22:09:36 +02003727 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003728 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003729
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003730 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003731 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003732 else
3733 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003734
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003735 if (!ret)
Christopher Faulet372b38f2022-07-08 15:20:31 +02003736 break;
3737
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003738 if ((count - ret) > 0)
3739 h1c->flags |= H1C_F_CO_MSG_MORE;
3740
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003741 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003742 count -= ret;
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003743
Olivier Houchard68787ef2020-01-15 19:13:32 +01003744 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003745 break;
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003746
3747 if ((h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)))
3748 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003749 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003750
Christopher Fauletfc473a62022-10-05 08:22:33 +02003751 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3752 // FIXME: following test was removed :
3753 // ((h1c->conn->flags & CO_FL_ERROR) && (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf)))) {
3754 se_fl_set_error(h1s->sd);
Christopher Faulet26a26432021-01-27 11:27:50 +01003755 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 +01003756 }
3757
Christopher Faulet7a145d62020-08-05 11:31:16 +02003758 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003759 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003760 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003761}
3762
Willy Tarreaue5733232019-05-22 19:24:06 +02003763#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003764/* Send and get, using splicing */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003765static int h1_rcv_pipe(struct stconn *sc, struct pipe *pipe, unsigned int count)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003766{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003767 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003768 struct h1c *h1c = h1s->h1c;
3769 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003770 int ret = 0;
3771
Christopher Faulet897d6122021-12-17 17:28:35 +01003772 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003773
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003774 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003775 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003776 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 +02003777 goto end;
3778 }
3779
Christopher Fauletcf307562021-07-26 10:49:39 +02003780 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003781 if (h1s_data_pending(h1s)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003782 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003783 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003784 }
3785
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003786 if (!h1_recv_allowed(h1c)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003787 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet0060be92020-07-03 15:02:25 +02003788 goto end;
3789 }
3790
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003791 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003792 count = h1m->curr_len;
Christopher Faulet897d6122021-12-17 17:28:35 +01003793 ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003794 if (ret >= 0) {
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003795 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003796 if (ret > h1m->curr_len) {
3797 h1s->flags |= H1S_F_PARSING_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003798 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003799 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003800 H1_EV_RX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003801 goto end;
3802 }
3803 h1m->curr_len -= ret;
3804 if (!h1m->curr_len) {
3805 h1m->state = H1_MSG_DONE;
3806 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletf4b89f12023-02-23 13:58:13 +01003807
3808 if (!(h1c->flags & H1C_F_IS_BACK)) {
3809 /* The request was fully received. It means the H1S now
3810 * expect data from the opposite side
3811 */
3812 se_expect_data(h1s->sd);
3813 }
3814
Christopher Faulet897d6122021-12-17 17:28:35 +01003815 TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003816 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003817 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003818 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
3819 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret);
Christopher Faulete18777b2019-04-16 16:46:36 +02003820 }
3821
Christopher Faulet1be55f92018-10-02 15:59:23 +02003822 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003823 if (conn_xprt_read0_pending(h1c->conn)) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003824 se_fl_set(h1s->sd, SE_FL_EOS);
3825 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_EOS;
Christopher Faulet897d6122021-12-17 17:28:35 +01003826 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003827 }
Christopher Faulet9009c972022-10-05 12:04:56 +02003828 if (h1c->conn->flags & CO_FL_ERROR) {
3829 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003830 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERROR;
Christopher Faulet9009c972022-10-05 12:04:56 +02003831 TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
3832 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003833
Christopher Faulet94d35102021-04-09 11:58:49 +02003834 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003835 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003836 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Faulet94d35102021-04-09 11:58:49 +02003837 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003838 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s);
3839 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet94d35102021-04-09 11:58:49 +02003840 }
Christopher Faulet27182292020-07-03 15:08:49 +02003841 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003842
Christopher Faulet897d6122021-12-17 17:28:35 +01003843 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003844 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003845}
3846
Willy Tarreau000d63c2022-05-27 10:39:17 +02003847static int h1_snd_pipe(struct stconn *sc, struct pipe *pipe)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003848{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003849 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003850 struct h1c *h1c = h1s->h1c;
3851 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003852 int ret = 0;
3853
Christopher Faulet897d6122021-12-17 17:28:35 +01003854 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003855
Christopher Faulet140f1a52021-11-26 16:37:55 +01003856 if (b_data(&h1c->obuf)) {
3857 if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003858 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s);
3859 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003860 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003861 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003862 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003863
Christopher Faulet897d6122021-12-17 17:28:35 +01003864 ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe);
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003865 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003866 if (ret > h1m->curr_len) {
3867 h1s->flags |= H1S_F_PROCESSING_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003868 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003869 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003870 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 +01003871 goto end;
3872 }
3873 h1m->curr_len -= ret;
3874 if (!h1m->curr_len) {
3875 h1m->state = H1_MSG_DONE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003876 TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003877 }
3878 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003879 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
3880 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret);
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003881
3882 end:
Christopher Faulet9009c972022-10-05 12:04:56 +02003883 if (h1c->conn->flags & CO_FL_ERROR) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003884 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERR_PENDING;
3885 if (h1c->flags & H1C_F_EOS)
3886 h1c->flags |= H1C_F_ERROR;
Christopher Fauleta462ee02022-11-22 17:16:22 +01003887 else if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
3888 /* EOS not seen, so subscribe for reads to be able to
3889 * catch the error on the reading path. It is especially
3890 * important if EOI was reached.
3891 */
3892 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3893 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003894 se_fl_set_error(h1s->sd);
Christopher Faulet9009c972022-10-05 12:04:56 +02003895 TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
3896 }
3897
Christopher Faulet897d6122021-12-17 17:28:35 +01003898 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003899 return ret;
3900}
3901#endif
3902
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003903static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3904{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003905 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003906 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003907
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003908 switch (mux_ctl) {
3909 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003910 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003911 ret |= MUX_STATUS_READY;
3912 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003913 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003914 if (output)
3915 *((int *)output) = h1c->errcode;
3916 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3917 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3918 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3919 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003920 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003921 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003922 default:
3923 return -1;
3924 }
3925}
3926
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003927/* appends some info about connection <h1c> to buffer <msg>, or does nothing if
3928 * <h1c> is NULL. Returns non-zero if the connection is considered suspicious.
3929 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
3930 * not NULL, otherwise a single line is used.
3931 */
3932static int h1_dump_h1c_info(struct buffer *msg, struct h1c *h1c, const char *pfx)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003933{
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003934 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003935
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003936 if (!h1c)
3937 return ret;
3938
Christopher Fauletf376a312019-01-04 15:16:06 +01003939 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3940 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003941 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3942 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003943 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003944 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
Christopher Faulet38f61352022-11-30 14:49:56 +01003945
3946 chunk_appendf(msg, " .task=%p", h1c->task);
3947 if (h1c->task) {
3948 chunk_appendf(msg, " .exp=%s",
3949 h1c->task->expire ? tick_is_expired(h1c->task->expire, now_ms) ? "<PAST>" :
3950 human_time(TICKS_TO_MS(h1c->task->expire - now_ms), TICKS_TO_MS(1000)) : "<NEVER>");
3951 }
3952
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003953 return ret;
3954}
3955
3956/* appends some info about stream <h1s> to buffer <msg>, or does nothing if
3957 * <h1s> is NULL. Returns non-zero if the stream is considered suspicious. May
3958 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
3959 * NULL, otherwise a single line is used.
3960 */
3961static int h1_dump_h1s_info(struct buffer *msg, const struct h1s *h1s, const char *pfx)
3962{
3963 const char *method;
3964 int ret = 0;
3965
3966 if (!h1s)
3967 return ret;
3968
3969 if (h1s->meth < HTTP_METH_OTHER)
3970 method = http_known_methods[h1s->meth].ptr;
3971 else
3972 method = "UNKNOWN";
3973
3974 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .sd.flg=0x%x .req.state=%s .res.state=%s",
3975 h1s, h1s->flags, se_fl_get(h1s->sd),
3976 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003977
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003978 if (pfx)
3979 chunk_appendf(msg, "\n%s", pfx);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003980
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003981 chunk_appendf(msg, " .meth=%s status=%d",
3982 method, h1s->status);
Christopher Faulet186367f2022-05-30 08:45:15 +02003983
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003984 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(h1s->sd));
3985 if (!se_fl_test(h1s->sd, SE_FL_ORPHAN))
3986 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
3987 h1s_sc(h1s)->flags, h1s_sc(h1s)->app);
Christopher Faulet186367f2022-05-30 08:45:15 +02003988
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003989 if (pfx && h1s->subs)
3990 chunk_appendf(msg, "\n%s", pfx);
3991
3992 chunk_appendf(msg, " .subs=%p", h1s->subs);
3993 if (h1s->subs) {
3994 chunk_appendf(msg, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3995 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
3996 h1s->subs->tasklet->calls,
3997 h1s->subs->tasklet->context);
3998 if (h1s->subs->tasklet->calls >= 1000000)
3999 ret = 1;
4000 resolve_sym_name(msg, NULL, h1s->subs->tasklet->process);
4001 chunk_appendf(msg, ")");
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004002 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01004003 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02004004}
4005
Willy Tarreau7079c0f2022-09-02 16:11:28 +02004006/* for debugging with CLI's "show fd" command */
4007static int h1_show_fd(struct buffer *msg, struct connection *conn)
4008{
4009 struct h1c *h1c = conn->ctx;
4010 struct h1s *h1s = h1c->h1s;
4011 int ret = 0;
4012
4013 ret |= h1_dump_h1c_info(msg, h1c, NULL);
4014
4015 if (h1s)
4016 ret |= h1_dump_h1s_info(msg, h1s, NULL);
4017
4018 return ret;
4019}
4020
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004021/* for debugging with CLI's "show sess" command. May emit multiple lines, each
4022 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
4023 * line is used. Each field starts with a space so it's safe to print it after
4024 * existing fields.
4025 */
4026static int h1_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
4027{
4028 struct h1s *h1s = sd->se;
4029 int ret = 0;
4030
4031 if (!h1s)
4032 return ret;
4033
4034 ret |= h1_dump_h1s_info(msg, h1s, pfx);
4035 if (pfx)
4036 chunk_appendf(msg, "\n%s", pfx);
4037 chunk_appendf(msg, " h1c=%p", h1s->h1c);
4038 ret |= h1_dump_h1c_info(msg, h1s->h1c, pfx);
4039 return ret;
4040}
4041
Christopher Faulet98fbe952019-07-22 16:18:24 +02004042
4043/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
4044static int add_hdr_case_adjust(const char *from, const char *to, char **err)
4045{
4046 struct h1_hdr_entry *entry;
4047
4048 /* Be sure there is a non-empty <to> */
4049 if (!strlen(to)) {
4050 memprintf(err, "expect <to>");
4051 return -1;
4052 }
4053
4054 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004055 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02004056 memprintf(err, "<from> and <to> must not differ execpt the case");
4057 return -1;
4058 }
4059
4060 /* Be sure <from> does not already existsin the tree */
4061 if (ebis_lookup(&hdrs_map.map, from)) {
4062 memprintf(err, "duplicate entry '%s'", from);
4063 return -1;
4064 }
4065
4066 /* Create the entry and insert it in the tree */
4067 entry = malloc(sizeof(*entry));
4068 if (!entry) {
4069 memprintf(err, "out of memory");
4070 return -1;
4071 }
4072
4073 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004074 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01004075 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02004076 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004077 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004078 free(entry);
4079 memprintf(err, "out of memory");
4080 return -1;
4081 }
4082 ebis_insert(&hdrs_map.map, &entry->node);
4083 return 0;
4084}
4085
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004086/* Migrate the the connection to the current thread.
4087 * Return 0 if successful, non-zero otherwise.
4088 * Expected to be called with the old thread lock held.
4089 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004090static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004091{
4092 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004093 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004094
4095 if (fd_takeover(conn->handle.fd, conn) != 0)
4096 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004097
4098 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4099 /* We failed to takeover the xprt, even if the connection may
4100 * still be valid, flag it as error'd, as we have already
4101 * taken over the fd, and wake the tasklet, so that it will
4102 * destroy it.
4103 */
4104 conn->flags |= CO_FL_ERROR;
4105 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
4106 return -1;
4107 }
4108
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004109 if (h1c->wait_event.events)
4110 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
4111 h1c->wait_event.events, &h1c->wait_event);
4112 /* To let the tasklet know it should free itself, and do nothing else,
4113 * set its context to NULL.
4114 */
4115 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004116 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004117
4118 task = h1c->task;
4119 if (task) {
4120 task->context = NULL;
4121 h1c->task = NULL;
4122 __ha_barrier_store();
4123 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004124
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004125 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004126 if (!h1c->task) {
4127 h1_release(h1c);
4128 return -1;
4129 }
4130 h1c->task->process = h1_timeout_task;
4131 h1c->task->context = h1c;
4132 }
4133 h1c->wait_event.tasklet = tasklet_new();
4134 if (!h1c->wait_event.tasklet) {
4135 h1_release(h1c);
4136 return -1;
4137 }
4138 h1c->wait_event.tasklet->process = h1_io_cb;
4139 h1c->wait_event.tasklet->context = h1c;
4140 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
4141 SUB_RETRY_RECV, &h1c->wait_event);
4142
4143 return 0;
4144}
4145
4146
Christopher Faulet98fbe952019-07-22 16:18:24 +02004147static void h1_hdeaders_case_adjust_deinit()
4148{
4149 struct ebpt_node *node, *next;
4150 struct h1_hdr_entry *entry;
4151
4152 node = ebpt_first(&hdrs_map.map);
4153 while (node) {
4154 next = ebpt_next(node);
4155 ebpt_delete(node);
4156 entry = container_of(node, struct h1_hdr_entry, node);
4157 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004158 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004159 free(entry);
4160 node = next;
4161 }
4162 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004163}
4164
Christopher Faulet98fbe952019-07-22 16:18:24 +02004165static int cfg_h1_headers_case_adjust_postparser()
4166{
4167 FILE *file = NULL;
4168 char *c, *key_beg, *key_end, *value_beg, *value_end;
4169 char *err;
4170 int rc, line = 0, err_code = 0;
4171
4172 if (!hdrs_map.name)
4173 goto end;
4174
4175 file = fopen(hdrs_map.name, "r");
4176 if (!file) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004177 ha_alert("h1-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004178 hdrs_map.name);
4179 err_code |= ERR_ALERT | ERR_FATAL;
4180 goto end;
4181 }
4182
4183 /* now parse all lines. The file may contain only two header name per
4184 * line, separated by spaces. All heading and trailing spaces will be
4185 * ignored. Lines starting with a # are ignored.
4186 */
4187 while (fgets(trash.area, trash.size, file) != NULL) {
4188 line++;
4189 c = trash.area;
4190
4191 /* strip leading spaces and tabs */
4192 while (*c == ' ' || *c == '\t')
4193 c++;
4194
4195 /* ignore emptu lines, or lines beginning with a dash */
4196 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
4197 continue;
4198
4199 /* look for the end of the key */
4200 key_beg = c;
4201 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
4202 c++;
4203 key_end = c;
4204
4205 /* strip middle spaces and tabs */
4206 while (*c == ' ' || *c == '\t')
4207 c++;
4208
4209 /* look for the end of the value, it is the end of the line */
4210 value_beg = c;
4211 while (*c && *c != '\n' && *c != '\r')
4212 c++;
4213 value_end = c;
4214
4215 /* trim possibly trailing spaces and tabs */
4216 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
4217 value_end--;
4218
4219 /* set final \0 and check entries */
4220 *key_end = '\0';
4221 *value_end = '\0';
4222
4223 err = NULL;
4224 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
4225 if (rc < 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004226 ha_alert("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004227 hdrs_map.name, err, line);
4228 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004229 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004230 goto end;
4231 }
4232 if (rc > 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004233 ha_warning("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004234 hdrs_map.name, err, line);
4235 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004236 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004237 }
4238 }
4239
4240 end:
4241 if (file)
4242 fclose(file);
4243 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
4244 return err_code;
4245}
4246
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004247/* config parser for global "h1-accept-payload_=-with-any-method" */
4248static int cfg_parse_h1_accept_payload_with_any_method(char **args, int section_type, struct proxy *curpx,
4249 const struct proxy *defpx, const char *file, int line,
4250 char **err)
4251{
4252 if (too_many_args(0, args, err, NULL))
4253 return -1;
4254 accept_payload_with_any_method = 1;
4255 return 0;
4256}
4257
Christopher Faulet98fbe952019-07-22 16:18:24 +02004258
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004259/* config parser for global "h1-header-case-adjust" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004260static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004261 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004262 char **err)
4263{
4264 if (too_many_args(2, args, err, NULL))
4265 return -1;
4266 if (!*(args[1]) || !*(args[2])) {
4267 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
4268 return -1;
4269 }
4270 return add_hdr_case_adjust(args[1], args[2], err);
4271}
4272
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004273/* config parser for global "h1-headers-case-adjust-file" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004274static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004275 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004276 char **err)
4277{
4278 if (too_many_args(1, args, err, NULL))
4279 return -1;
4280 if (!*(args[1])) {
4281 memprintf(err, "'%s' expects <file> as argument.", args[0]);
4282 return -1;
4283 }
4284 free(hdrs_map.name);
4285 hdrs_map.name = strdup(args[1]);
4286 return 0;
4287}
4288
Christopher Faulet98fbe952019-07-22 16:18:24 +02004289/* config keyword parsers */
4290static struct cfg_kw_list cfg_kws = {{ }, {
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004291 { CFG_GLOBAL, "h1-accept-payload-with-any-method", cfg_parse_h1_accept_payload_with_any_method },
Christopher Faulet98fbe952019-07-22 16:18:24 +02004292 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
4293 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
4294 { 0, NULL, NULL },
4295 }
4296};
4297
4298INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4299REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
4300
4301
Christopher Faulet51dbc942018-09-13 09:05:15 +02004302/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05004303/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02004304/****************************************/
4305
4306/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01004307static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02004308 .init = h1_init,
4309 .wake = h1_wake,
4310 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004311 .get_first_sc = h1_get_first_sc,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004312 .detach = h1_detach,
4313 .destroy = h1_destroy,
4314 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01004315 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004316 .rcv_buf = h1_rcv_buf,
4317 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02004318#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02004319 .rcv_pipe = h1_rcv_pipe,
4320 .snd_pipe = h1_snd_pipe,
4321#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02004322 .subscribe = h1_subscribe,
4323 .unsubscribe = h1_unsubscribe,
4324 .shutr = h1_shutr,
4325 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004326 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004327 .show_sd = h1_show_sd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004328 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004329 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02004330 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02004331 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02004332};
4333
Christopher Faulet3f612f72021-02-05 16:44:21 +01004334static const struct mux_ops mux_h1_ops = {
4335 .init = h1_init,
4336 .wake = h1_wake,
4337 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004338 .get_first_sc = h1_get_first_sc,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004339 .detach = h1_detach,
4340 .destroy = h1_destroy,
4341 .avail_streams = h1_avail_streams,
4342 .used_streams = h1_used_streams,
4343 .rcv_buf = h1_rcv_buf,
4344 .snd_buf = h1_snd_buf,
4345#if defined(USE_LINUX_SPLICE)
4346 .rcv_pipe = h1_rcv_pipe,
4347 .snd_pipe = h1_snd_pipe,
4348#endif
4349 .subscribe = h1_subscribe,
4350 .unsubscribe = h1_unsubscribe,
4351 .shutr = h1_shutr,
4352 .shutw = h1_shutw,
4353 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004354 .show_sd = h1_show_sd,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004355 .ctl = h1_ctl,
4356 .takeover = h1_takeover,
4357 .flags = MX_FL_HTX|MX_FL_NO_UPG,
4358 .name = "H1",
4359};
Christopher Faulet51dbc942018-09-13 09:05:15 +02004360
Christopher Faulet3f612f72021-02-05 16:44:21 +01004361/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
4362static struct mux_proto_list mux_proto_h1 =
4363 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
4364static struct mux_proto_list mux_proto_http =
4365 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02004366
Christopher Faulet3f612f72021-02-05 16:44:21 +01004367INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
4368INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01004369
Christopher Faulet51dbc942018-09-13 09:05:15 +02004370/*
4371 * Local variables:
4372 * c-indent-level: 8
4373 * c-basic-offset: 8
4374 * End:
4375 */