blob: 9eca08c33769dc8751a9876164c78ae68b5961e8 [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 Faulet47365272018-10-31 17:40:50 +0100753
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 }
785
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200786 h1s->sess = sess;
787
788 if (h1c->px->options2 & PR_O2_REQBUG_OK)
789 h1s->req.err_pos = -1;
790
Christopher Fauletaf5336f2022-09-12 07:46:11 +0200791 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
792 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
793
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200794 h1c->idle_exp = TICK_ETERNITY;
795 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200796 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200797 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100798
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200799 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100800 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100801 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200802 return NULL;
803}
804
Willy Tarreau000d63c2022-05-27 10:39:17 +0200805static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200806{
807 struct h1s *h1s;
808
809 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
810
811 h1s = h1s_new(h1c);
812 if (!h1s)
813 goto fail;
814
Willy Tarreau000d63c2022-05-27 10:39:17 +0200815 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200816 goto fail;
817
Christopher Faulet10a86702021-04-07 14:18:11 +0200818 h1s->flags |= H1S_F_RX_BLK;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200819 h1s->sd = sc->sedesc;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200820 h1s->sess = sess;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200821
Christopher Faulet4e72b172022-10-04 17:35:19 +0200822 h1c->state = H1_CS_RUNNING;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200823
824 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
825 h1s->res.err_pos = -1;
826
Christopher Faulet60fa0512021-11-26 18:10:39 +0100827 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100828 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100829
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200830 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
831 return h1s;
832
833 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100834 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +0200835 pool_free(pool_head_h1s, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100836 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200837}
838
839static void h1s_destroy(struct h1s *h1s)
840{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200841 if (h1s) {
842 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200843
Christopher Faulet6b81df72019-10-01 22:08:43 +0200844 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200845 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200846
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100847 if (h1s->subs)
848 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200849
Christopher Fauletd17ad822020-09-24 15:14:29 +0200850 h1_release_buf(h1c, &h1s->rxbuf);
851
Christopher Faulet10a86702021-04-07 14:18:11 +0200852 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200853 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
854 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100855
Christopher Faulet31da34d2022-10-10 16:36:10 +0200856 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 +0200857 h1_is_alive(h1c) && /* still alive */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100858 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100859 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200860 h1c->state = H1_CS_IDLE;
861 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100862 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
863 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200864 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200865 h1_close(h1c);
866 TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200867 }
Christopher Faulet60fa0512021-11-26 18:10:39 +0100868
869 HA_ATOMIC_DEC(&h1c->px_counters->open_streams);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200870 BUG_ON(h1s->sd && !se_fl_test(h1s->sd, SE_FL_ORPHAN));
871 sedesc_free(h1s->sd);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200872 pool_free(pool_head_h1s, h1s);
873 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200874}
875
876/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200877 * Initialize the mux once it's attached. It is expected that conn->ctx points
Willy Tarreau4596fe22022-05-17 19:07:51 +0200878 * to the existing stream connector (for outgoing connections or for incoming
879 * ones during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200880 * establishment). <input> is always used as Input buffer and may contain
881 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
882 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200883 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200884static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
885 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200886{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200887 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100888 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200889 void *conn_ctx = conn->ctx;
890
891 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200892
893 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100894 if (!h1c) {
895 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200896 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100897 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200898 h1c->conn = conn;
899 h1c->px = proxy;
900
Christopher Fauletef93be22022-10-04 17:13:32 +0200901 h1c->state = H1_CS_IDLE;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200902 h1c->flags = H1C_F_NONE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200903 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200904 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200905 h1c->obuf = BUF_NULL;
906 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200907 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200908
Willy Tarreau90f366b2021-02-20 11:49:49 +0100909 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200910 h1c->wait_event.tasklet = tasklet_new();
911 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200912 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200913 h1c->wait_event.tasklet->process = h1_io_cb;
914 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100915 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200916 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200917
Christopher Faulete9b70722019-04-08 10:46:02 +0200918 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200919 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100920 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
921 if (tick_isset(proxy->timeout.serverfin))
922 h1c->shut_timeout = proxy->timeout.serverfin;
Christopher Faulet563c3452021-11-26 17:37:51 +0100923
924 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be,
925 &h1_stats_module);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100926 } else {
927 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
928 if (tick_isset(proxy->timeout.clientfin))
929 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200930
Christopher Faulet563c3452021-11-26 17:37:51 +0100931 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe,
932 &h1_stats_module);
933
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200934 LIST_APPEND(&mux_stopping_data[tid].list,
935 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100936 }
937 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200938 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100939 if (!t) {
940 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 +0100941 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100942 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100943
944 h1c->task = t;
945 t->process = h1_timeout_task;
946 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200947
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100948 t->expire = tick_add(now_ms, h1c->timeout);
949 }
950
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100951 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200952
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200953 if (h1c->flags & H1C_F_IS_BACK) {
954 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200955 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
956 goto fail;
957 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100958 else if (conn_ctx) {
959 /* Upgraded frontend connection (from TCP) */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100960 if (!h1c_frt_stream_new(h1c, conn_ctx, h1c->conn->owner))
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100961 goto fail;
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100962
Willy Tarreaue68bc612022-05-27 11:23:05 +0200963 /* Attach the SC but Not ready yet */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200964 h1c->state = H1_CS_UPGRADING;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200965 TRACE_DEVEL("Inherit the SC from TCP connection to perform an upgrade",
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100966 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
967 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100968
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200969 if (t) {
970 h1_set_idle_expiration(h1c);
971 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100972 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200973 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100974
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200975 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100976 if (b_data(&h1c->ibuf))
977 tasklet_wakeup(h1c->wait_event.tasklet);
978 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200979 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200980
Christopher Faulet60fa0512021-11-26 18:10:39 +0100981 HA_ATOMIC_INC(&h1c->px_counters->open_conns);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100982 HA_ATOMIC_INC(&h1c->px_counters->total_conns);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100983
Christopher Faulet51dbc942018-09-13 09:05:15 +0200984 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200985 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200986 return 0;
987
988 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200989 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200990 if (h1c->wait_event.tasklet)
991 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200992 pool_free(pool_head_h1c, h1c);
993 fail_h1c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +0100994 if (!conn_is_back(conn))
995 LIST_DEL_INIT(&conn->stopping_list);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200996 conn->ctx = conn_ctx; // restore saved context
997 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200998 return -1;
999}
1000
Christopher Faulet73c12072019-04-08 11:23:22 +02001001/* release function. This one should be called to free all resources allocated
1002 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +02001003 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001004static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001005{
Christopher Faulet61840e72019-04-15 09:33:32 +02001006 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001007
Christopher Faulet6b81df72019-10-01 22:08:43 +02001008 TRACE_POINT(H1_EV_H1C_END);
1009
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001010 /* The connection must be aattached to this mux to be released */
1011 if (h1c->conn && h1c->conn->ctx == h1c)
1012 conn = h1c->conn;
Christopher Faulet61840e72019-04-15 09:33:32 +02001013
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001014 if (conn && h1c->flags & H1C_F_UPG_H2C) {
1015 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
1016 /* Make sure we're no longer subscribed to anything */
1017 if (h1c->wait_event.events)
1018 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1019 h1c->wait_event.events, &h1c->wait_event);
1020 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
1021 /* connection successfully upgraded to H2, this
1022 * mux was already released */
1023 return;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001024 }
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001025 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
1026 sess_log(conn->owner); /* Log if the upgrade failed */
1027 }
Olivier Houchard45c44372019-06-11 14:06:23 +02001028
Christopher Faulet51dbc942018-09-13 09:05:15 +02001029
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001030 if (LIST_INLIST(&h1c->buf_wait.list))
1031 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001032
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001033 h1_release_buf(h1c, &h1c->ibuf);
1034 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001035
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001036 if (h1c->task) {
1037 h1c->task->context = NULL;
1038 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
1039 h1c->task = NULL;
1040 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001041
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001042 if (h1c->wait_event.tasklet)
1043 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet60fa0512021-11-26 18:10:39 +01001044
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001045 h1s_destroy(h1c->h1s);
1046 if (conn) {
1047 if (h1c->wait_event.events != 0)
1048 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
1049 &h1c->wait_event);
1050 h1_shutw_conn(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001051 }
1052
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001053 HA_ATOMIC_DEC(&h1c->px_counters->open_conns);
1054 pool_free(pool_head_h1c, h1c);
1055
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001056 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001057 if (!conn_is_back(conn))
1058 LIST_DEL_INIT(&conn->stopping_list);
1059
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001060 conn->mux = NULL;
1061 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001062 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001063
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001064 conn_stop_tracking(conn);
1065 conn_full_close(conn);
1066 if (conn->destroy_cb)
1067 conn->destroy_cb(conn);
1068 conn_free(conn);
1069 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001070}
1071
1072/******************************************************/
1073/* functions below are for the H1 protocol processing */
1074/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +02001075/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
1076 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +02001077 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001078static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001079{
Christopher Faulet570d1612018-11-26 11:13:57 +01001080 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001081
Christopher Faulet570d1612018-11-26 11:13:57 +01001082 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001083 (*(p + 5) > '1' ||
1084 (*(p + 5) == '1' && *(p + 7) >= '1')))
1085 h1m->flags |= H1_MF_VER_11;
1086}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001087
Christopher Faulet9768c262018-10-22 09:34:31 +02001088/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
1089 * greater or equal to 1.1
1090 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001091static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +02001092{
Christopher Faulet570d1612018-11-26 11:13:57 +01001093 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001094
Christopher Faulet570d1612018-11-26 11:13:57 +01001095 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001096 (*(p + 5) > '1' ||
1097 (*(p + 5) == '1' && *(p + 7) >= '1')))
1098 h1m->flags |= H1_MF_VER_11;
1099}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001100
Christopher Fauletf2824e62018-10-01 12:12:37 +02001101/* Deduce the connection mode of the client connection, depending on the
1102 * configuration and the H1 message flags. This function is called twice, the
1103 * first time when the request is parsed and the second time when the response
1104 * is parsed.
1105 */
1106static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
1107{
1108 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001109
1110 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001111 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001112 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001113 h1s->status == 101) {
1114 /* Either we've established an explicit tunnel, or we're
1115 * switching the protocol. In both cases, we're very unlikely to
1116 * understand the next protocols. We have to switch to tunnel
1117 * mode, so that we transfer the request and responses then let
1118 * this protocol pass unmodified. When we later implement
1119 * specific parsers for such protocols, we'll want to check the
1120 * Upgrade header which contains information about that protocol
1121 * for responses with status 101 (eg: see RFC2817 about TLS).
1122 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001123 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001124 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 +01001125 }
1126 else if (h1s->flags & H1S_F_WANT_KAL) {
1127 /* By default the client is in KAL mode. CLOSE mode mean
1128 * it is imposed by the client itself. So only change
1129 * KAL mode here. */
1130 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1131 /* no length known or explicit close => close */
1132 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001133 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 +01001134 }
1135 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1136 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001137 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001138 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001139 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 +01001140 }
1141 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001142 }
1143 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001144 /* Input direction: first pass */
1145 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1146 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001147 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001148 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 +01001149 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001150 }
1151
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001152 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001153 * unless a 'close-spread-time' option is set (either to define a
1154 * soft-close window or to disable active closing (close-spread-time
1155 * option set to 0).
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001156 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001157 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001158 int want_clo = 1;
1159 /* If a close-spread-time option is set, we want to avoid
1160 * closing all the active HTTP connections at once so we add a
1161 * random factor that will spread the closing.
1162 */
1163 if (tick_isset(global.close_spread_end)) {
1164 int remaining_window = tick_remain(now_ms, global.close_spread_end);
1165 if (remaining_window) {
1166 /* This should increase the closing rate the further along
1167 * the window we are.
1168 */
1169 want_clo = (remaining_window <= statistical_prng_range(global.close_spread_time));
1170 }
1171 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001172 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
1173 want_clo = 0;
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001174
1175 if (want_clo) {
1176 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1177 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);
1178 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001179 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001180}
1181
1182/* Deduce the connection mode of the client connection, depending on the
1183 * configuration and the H1 message flags. This function is called twice, the
1184 * first time when the request is parsed and the second time when the response
1185 * is parsed.
1186 */
1187static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1188{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001189 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001190 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001191 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001192
Christopher Fauletf2824e62018-10-01 12:12:37 +02001193 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001194 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001195 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001196 h1s->status == 101) {
1197 /* Either we've established an explicit tunnel, or we're
1198 * switching the protocol. In both cases, we're very unlikely to
1199 * understand the next protocols. We have to switch to tunnel
1200 * mode, so that we transfer the request and responses then let
1201 * this protocol pass unmodified. When we later implement
1202 * specific parsers for such protocols, we'll want to check the
1203 * Upgrade header which contains information about that protocol
1204 * for responses with status 101 (eg: see RFC2817 about TLS).
1205 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001206 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001207 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 +01001208 }
1209 else if (h1s->flags & H1S_F_WANT_KAL) {
1210 /* By default the server is in KAL mode. CLOSE mode mean
1211 * it is imposed by haproxy itself. So only change KAL
1212 * mode here. */
1213 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1214 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1215 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1216 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001217 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 +01001218 }
1219 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001220 }
Christopher Faulet70033782018-12-05 13:50:11 +01001221 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001222 /* Output direction: first pass */
1223 if (h1m->flags & H1_MF_CONN_CLO) {
1224 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001225 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001226 TRACE_STATE("detect close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001227 }
1228 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1229 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1230 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1231 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1232 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1233 /* no explicit keep-alive option httpclose/server-close => close */
1234 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001235 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 +01001236 }
Christopher Faulet70033782018-12-05 13:50:11 +01001237 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001238
1239 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001240 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001241 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001242 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);
1243 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001244}
1245
Christopher Fauletb992af02019-03-28 15:42:24 +01001246static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001247{
1248 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001249
1250 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1251 * token is found
1252 */
1253 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001254 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001255
1256 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001257 if (!(h1m->flags & H1_MF_VER_11)) {
1258 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 +01001259 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001260 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001261 }
1262 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001263 if (h1m->flags & H1_MF_VER_11) {
1264 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001265 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001266 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001267 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001268}
1269
Christopher Fauletb992af02019-03-28 15:42:24 +01001270static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001271{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001272 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1273 * token is found
1274 */
1275 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001276 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001277
1278 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001279 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001280 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1281 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 +01001282 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001283 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001284 }
1285 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001286 if (h1m->flags & H1_MF_VER_11) {
1287 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001288 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001289 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001290 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001291}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001292
Christopher Fauletb992af02019-03-28 15:42:24 +01001293static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001294{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001295 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001296 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001297 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001298 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001299}
1300
Christopher Fauletb992af02019-03-28 15:42:24 +01001301static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1302{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001303 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001304 h1_set_cli_conn_mode(h1s, h1m);
1305 else
1306 h1_set_srv_conn_mode(h1s, h1m);
1307
1308 if (!(h1m->flags & H1_MF_RESP))
1309 h1_update_req_conn_value(h1s, h1m, conn_val);
1310 else
1311 h1_update_res_conn_value(h1s, h1m, conn_val);
1312}
Christopher Faulete44769b2018-11-29 23:01:45 +01001313
Christopher Faulet98fbe952019-07-22 16:18:24 +02001314/* Try to adjust the case of the message header name using the global map
1315 * <hdrs_map>.
1316 */
1317static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1318{
1319 struct ebpt_node *node;
1320 struct h1_hdr_entry *entry;
1321
1322 /* No entry in the map, do nothing */
1323 if (eb_is_empty(&hdrs_map.map))
1324 return;
1325
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001326 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001327 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1328 return;
1329
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001330 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001331 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1332 return;
1333
1334 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1335 if (!node)
1336 return;
1337 entry = container_of(node, struct h1_hdr_entry, node);
1338 name->ptr = entry->name.ptr;
1339 name->len = entry->name.len;
1340}
1341
Christopher Faulete44769b2018-11-29 23:01:45 +01001342/* Append the description of what is present in error snapshot <es> into <out>.
1343 * The description must be small enough to always fit in a buffer. The output
1344 * buffer may be the trash so the trash must not be used inside this function.
1345 */
1346static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1347{
1348 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001349 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1350 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1351 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1352 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1353 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1354 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001355}
1356/*
1357 * Capture a bad request or response and archive it in the proxy's structure.
1358 * By default it tries to report the error position as h1m->err_pos. However if
1359 * this one is not set, it will then report h1m->next, which is the last known
1360 * parsing point. The function is able to deal with wrapping buffers. It always
1361 * displays buffers as a contiguous area starting at buf->p. The direction is
1362 * determined thanks to the h1m's flags.
1363 */
1364static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1365 struct h1m *h1m, struct buffer *buf)
1366{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001367 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001368 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001369 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001370 union error_snapshot_ctx ctx;
1371
Christopher Faulet4e72b172022-10-04 17:35:19 +02001372 if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001373 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02001374 sess = __sc_strm(h1s_sc(h1s))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001375 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02001376 other_end = __sc_strm(h1s_sc(h1s))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001377 else
1378 other_end = sess->fe;
1379 } else
1380 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001381
1382 /* http-specific part now */
1383 ctx.h1.state = h1m->state;
1384 ctx.h1.c_flags = h1c->flags;
1385 ctx.h1.s_flags = h1s->flags;
1386 ctx.h1.m_flags = h1m->flags;
1387 ctx.h1.m_clen = h1m->curr_len;
1388 ctx.h1.m_blen = h1m->body_len;
1389
1390 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1391 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001392 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1393 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001394}
1395
Christopher Fauletadb22202018-12-12 10:32:09 +01001396/* Emit the chunksize followed by a CRLF in front of data of the buffer
1397 * <buf>. It goes backwards and starts with the byte before the buffer's
1398 * head. The caller is responsible for ensuring there is enough room left before
1399 * the buffer's head for the string.
1400 */
1401static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1402{
1403 char *beg, *end;
1404
1405 beg = end = b_head(buf);
1406 *--beg = '\n';
1407 *--beg = '\r';
1408 do {
1409 *--beg = hextab[chksz & 0xF];
1410 } while (chksz >>= 4);
1411 buf->head -= (end - beg);
1412 b_add(buf, end - beg);
1413}
1414
1415/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1416 * ensuring there is enough room left in the buffer for the string. */
1417static void h1_emit_chunk_crlf(struct buffer *buf)
1418{
1419 *(b_peek(buf, b_data(buf))) = '\r';
1420 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1421 b_add(buf, 2);
1422}
1423
Christopher Faulet129817b2018-09-20 16:14:40 +02001424/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001425 * Switch the stream to tunnel mode. This function must only be called on 2xx
1426 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001427 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001428static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001429{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001430 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001431
Christopher Faulet5be651d2021-01-22 15:28:03 +01001432 h1s->req.state = H1_MSG_TUNNEL;
1433 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001434
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001435 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001436 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001437
Christopher Faulet5be651d2021-01-22 15:28:03 +01001438 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 +01001439
Christopher Faulet10a86702021-04-07 14:18:11 +02001440 if (h1s->flags & H1S_F_RX_BLK) {
1441 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001442 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001443 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 +02001444 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001445 if (h1s->flags & H1S_F_TX_BLK) {
1446 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001447 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001448 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 +01001449 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001450}
1451
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001452/* Search for a websocket key header. The message should have been identified
1453 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001454 *
1455 * On the request side, if found the key is stored in the session. It might be
1456 * needed to calculate response key if the server side is using http/2.
1457 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001458 * On the response side, the key might be verified if haproxy has been
1459 * responsible for the generation of a key. This happens when a h2 client is
1460 * interfaced with a h1 server.
1461 *
1462 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001463 */
1464static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1465{
1466 struct htx_blk *blk;
1467 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001468 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001469 int ws_key_found = 0, idx;
1470
1471 idx = htx_get_head(htx); // returns the SL that we skip
1472 while ((idx = htx_get_next(htx, idx)) != -1) {
1473 blk = htx_get_blk(htx, idx);
1474 type = htx_get_blk_type(blk);
1475
1476 if (type == HTX_BLK_UNUSED)
1477 continue;
1478
1479 if (type != HTX_BLK_HDR)
1480 break;
1481
1482 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001483 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001484
Amaury Denoyellec1938232020-12-11 17:53:03 +01001485 /* Websocket key is base64 encoded of 16 bytes */
1486 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001487 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001488 /* Copy the key on request side
1489 * we might need it if the server is using h2 and does
1490 * not provide the response
1491 */
1492 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001493 ws_key_found = 1;
1494 break;
1495 }
1496 else if (isteqi(n, ist("sec-websocket-accept")) &&
1497 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001498 /* Need to verify the response key if the input was
1499 * generated by haproxy
1500 */
1501 if (h1s->ws_key[0]) {
1502 char key[29];
1503 h1_calculate_ws_output_key(h1s->ws_key, key);
1504 if (!isteqi(ist(key), v))
1505 break;
1506 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001507 ws_key_found = 1;
1508 break;
1509 }
1510 }
1511
1512 /* missing websocket key, reject the message */
1513 if (!ws_key_found) {
1514 htx->flags |= HTX_FL_PARSING_ERROR;
1515 return 0;
1516 }
1517
1518 return 1;
1519}
1520
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001521/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001522 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001523 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001524 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1525 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001526 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001527static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1528 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001529{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001530 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001531 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001532
Willy Tarreau022e5e52020-09-10 09:33:15 +02001533 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 +02001534
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001535 if (h1s->meth == HTTP_METH_CONNECT)
1536 h1m->flags |= H1_MF_METH_CONNECT;
1537 if (h1s->meth == HTTP_METH_HEAD)
1538 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001539
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001540 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001541 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001542 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 +02001543 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001544 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001545 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 +02001546 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1547 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001548 else if (ret == -2) {
1549 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);
1550 h1s->flags |= H1S_F_RX_CONGESTED;
1551 }
1552 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001553 goto end;
1554 }
1555
Christopher Faulete136bd12021-09-21 18:44:55 +02001556
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001557 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload except if
1558 * accept_payload_with_any_method global option is set.
1559 *There is a payload if the c-l is not null or the the payload is
1560 * chunk-encoded. A parsing error is reported but a A
1561 * 413-Payload-Too-Large is returned instead of a 400-Bad-Request.
Christopher Faulete136bd12021-09-21 18:44:55 +02001562 */
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001563 if (!accept_payload_with_any_method &&
1564 !(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
Christopher Faulete136bd12021-09-21 18:44:55 +02001565 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1566 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1567 h1s->flags |= H1S_F_PARSING_ERROR;
1568 htx->flags |= HTX_FL_PARSING_ERROR;
1569 h1s->h1c->errcode = 413;
1570 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);
1571 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1572 ret = 0;
1573 goto end;
1574 }
1575
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001576 /* Reject any message with an unknown transfer-encoding. In fact if any
1577 * encoding other than "chunked". A 422-Unprocessable-Content is
1578 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1579 * response.
1580 */
1581 if (h1m->flags & H1_MF_TE_OTHER) {
1582 h1s->flags |= H1S_F_PARSING_ERROR;
1583 htx->flags |= HTX_FL_PARSING_ERROR;
1584 if (!(h1m->flags & H1_MF_RESP))
1585 h1s->h1c->errcode = 422;
1586 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1587 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1588 ret = 0;
1589 goto end;
1590 }
1591
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001592 /* If websocket handshake, search for the websocket key */
1593 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1594 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1595 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1596 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001597 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001598 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 +01001599 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1600
1601 ret = 0;
1602 goto end;
1603 }
1604 }
1605
Christopher Faulet486498c2019-10-11 14:22:00 +02001606 if (h1m->err_pos >= 0) {
1607 /* Maybe we found an error during the parsing while we were
1608 * configured not to block on that, so we have to capture it
1609 * now.
1610 */
1611 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1612 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1613 }
1614
Christopher Faulet5696f542020-12-02 16:08:38 +01001615 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001616 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001617 if (h1s->meth == HTTP_METH_HEAD)
1618 h1s->flags |= H1S_F_BODYLESS_RESP;
1619 }
1620 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001621 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001622 if (h1s->status == 204 || h1s->status == 304)
1623 h1s->flags |= H1S_F_BODYLESS_RESP;
1624 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001625 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001626 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001627
Christopher Faulet129817b2018-09-20 16:14:40 +02001628 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001629 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 +02001630 return ret;
1631}
1632
1633/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001634 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001635 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1636 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001637 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001638static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1639 struct buffer *buf, size_t *ofs, size_t max,
1640 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001641{
Christopher Fauletde471a42021-02-01 16:37:28 +01001642 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001643
Willy Tarreau022e5e52020-09-10 09:33:15 +02001644 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 +02001645 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001646 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001647 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 +01001648 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001649 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001650 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 +02001651 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001652 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001653 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001654 }
1655
Christopher Faulet02a02532019-11-15 09:36:28 +01001656 *ofs += ret;
1657
1658 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001659 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1660 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);
1661 h1s->flags |= H1S_F_RX_CONGESTED;
1662 }
1663
Willy Tarreau022e5e52020-09-10 09:33:15 +02001664 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 +02001665 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001666}
1667
1668/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001669 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1670 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1671 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001672 * responsible to update the parser state <h1m>. If more room is requested,
1673 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001674 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001675static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1676 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001677{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001678 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001679
Willy Tarreau022e5e52020-09-10 09:33:15 +02001680 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 +02001681 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001682 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001683 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 +02001684 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001685 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001686 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 +02001687 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1688 }
Christopher Fauletae660be2022-04-13 17:48:54 +02001689 else if (ret == -2) {
Christopher Faulet46e058d2021-09-20 07:47:27 +02001690 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);
1691 h1s->flags |= H1S_F_RX_CONGESTED;
1692 }
1693 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001694 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001695 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001696
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001697 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001698
1699 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001700 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 +02001701 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001702}
1703
1704/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001705 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001706 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1707 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001708 *
1709 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001710 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001711static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001712{
Christopher Faulet539e0292018-11-19 10:40:09 +01001713 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001714 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001715 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001716 size_t data;
1717 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001718 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001719
Christopher Faulet539e0292018-11-19 10:40:09 +01001720 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001721 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001722
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001723 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001724 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001725
Christopher Fauletbef89002022-10-05 07:50:19 +02001726 if (h1s->flags & (H1S_F_INTERNAL_ERROR|H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001727 goto end;
1728
Christopher Faulet10a86702021-04-07 14:18:11 +02001729 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001730 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001731
Christopher Faulet46e058d2021-09-20 07:47:27 +02001732 /* Always remove congestion flags and try to process more input data */
1733 h1s->flags &= ~H1S_F_RX_CONGESTED;
1734
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001735 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001736 size_t used = htx_used_space(htx);
1737
Christopher Faulet129817b2018-09-20 16:14:40 +02001738 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001739 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001740 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001741 if (!ret)
1742 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001743
1744 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1745 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1746
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001747 if ((h1m->flags & H1_MF_RESP) &&
1748 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1749 h1m_init_res(&h1s->res);
1750 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001751 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001752 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001753 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001754 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001755 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001756 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001757 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001758 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001759
1760 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1761 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001762 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001763 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001764 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001765 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001766 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001767 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001768
Christopher Faulet76014fd2019-12-10 11:47:22 +01001769 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1770 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001771 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001772 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001773 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1774 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001775
Christopher Faulet5be651d2021-01-22 15:28:03 +01001776 if ((h1m->flags & H1_MF_RESP) &&
1777 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1778 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001779 else {
1780 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1781 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001782 h1s->flags |= H1S_F_RX_BLK;
1783 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001784 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001785 if (h1s->flags & H1S_F_TX_BLK) {
1786 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001787 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001788 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 +01001789 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001790 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001791 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001792 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001793 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001794 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001795 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001796 if (!ret)
1797 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001798
1799 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1800 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001801 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001802 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001803 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001804 break;
1805 }
1806
Christopher Faulet30db3d72019-05-17 15:35:33 +02001807 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001808 } 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 +01001809
Christopher Faulet129817b2018-09-20 16:14:40 +02001810
Christopher Faulet2eed8002020-12-07 11:26:13 +01001811 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001812 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 +02001813 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001814 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001815
Christopher Faulet539e0292018-11-19 10:40:09 +01001816 b_del(&h1c->ibuf, total);
1817
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001818 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1819
Christopher Faulet30db3d72019-05-17 15:35:33 +02001820 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001821 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001822 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001823 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 +01001824 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001825 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001826
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001827 if (!b_data(&h1c->ibuf))
1828 h1_release_buf(h1c, &h1c->ibuf);
1829
Christopher Faulet2177d962022-10-05 08:39:14 +02001830 if (h1m->state <= H1_MSG_LAST_LF)
1831 goto out;
1832
Christopher Faulet4e72b172022-10-04 17:35:19 +02001833 if (h1c->state < H1_CS_RUNNING) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001834 /* The H1 connection is not ready. Most of time, there is no SC
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001835 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1836 * cases, it is only possible on the client side.
1837 */
1838 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1839
Christopher Faulet4e72b172022-10-04 17:35:19 +02001840 if (h1c->state == H1_CS_EMBRYONIC) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001841 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 +02001842 BUG_ON(h1s_sc(h1s));
Willy Tarreau000d63c2022-05-27 10:39:17 +02001843 if (!h1s_new_sc(h1s, buf)) {
Christopher Fauletbef89002022-10-05 07:50:19 +02001844 h1s->flags |= H1S_F_INTERNAL_ERROR;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001845 goto err;
1846 }
1847 }
1848 else {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001849 TRACE_DEVEL("request headers fully parsed, upgrade the inherited SC", H1_EV_RX_DATA, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001850 BUG_ON(h1s_sc(h1s) == NULL);
Willy Tarreau000d63c2022-05-27 10:39:17 +02001851 if (!h1s_upgrade_sc(h1s, buf)) {
Christopher Fauletbef89002022-10-05 07:50:19 +02001852 h1s->flags |= H1S_F_INTERNAL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001853 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001854 goto err;
1855 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001856 }
1857 }
1858
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001859 /* Here h1s_sc(h1s) is always defined */
Christopher Fauletf5ce3202021-11-26 17:26:19 +01001860 if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) {
Christopher Fauleta583af62020-09-24 15:35:37 +02001861 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 +02001862 se_fl_set(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001863 }
1864 else {
1865 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 +02001866 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001867 }
1868
Willy Tarreau4596fe22022-05-17 19:07:51 +02001869 /* Set EOI on stream connector in DONE state iff:
Christopher Fauleta22782b2021-02-08 17:18:01 +01001870 * - it is a response
1871 * - it is a request but no a protocol upgrade nor a CONNECT
1872 *
1873 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001874 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001875 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001876 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1877 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001878 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauleta583af62020-09-24 15:35:37 +02001879
Christopher Fauletec4207c2021-04-08 18:34:30 +02001880 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001881 /* When Input data are pending for this message, notify upper layer that
1882 * the mux need more space in the HTX buffer to continue if :
1883 *
1884 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1885 * - Headers or trailers are pending to be copied.
1886 */
1887 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001888 se_fl_set(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001889 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1890 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001891 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001892 se_fl_clr(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001893 if (h1c->flags & H1C_F_EOS) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001894 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001895 TRACE_STATE("report EOS to SE", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet10498562022-10-10 18:05:25 +02001896 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 +01001897 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
Willy Tarreau4596fe22022-05-17 19:07:51 +02001898 * EOI on the stream connector */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001899 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletfc473a62022-10-05 08:22:33 +02001900 TRACE_STATE("report EOI to SE", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001901 }
Christopher Faulet10498562022-10-10 18:05:25 +02001902 else if (h1m->state < H1_MSG_DONE) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001903 se_fl_set(h1s->sd, SE_FL_ERROR);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001904 TRACE_ERROR("message aborted, set error on SC", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +01001905 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001906
Christopher Faulet10a86702021-04-07 14:18:11 +02001907 if (h1s->flags & H1S_F_TX_BLK) {
1908 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001909 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001910 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 +01001911 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001912 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001913 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001914
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001915 end:
Christopher Faulet5966e402022-07-08 09:02:59 +02001916 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001917 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001918 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001919
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001920 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001921 htx_to_buf(htx, buf);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001922 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001923 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001924 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001925}
1926
Christopher Faulet129817b2018-09-20 16:14:40 +02001927/*
1928 * Process outgoing data. It parses data and transfer them from the channel buffer into
1929 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1930 * 0 if it couldn't proceed.
1931 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001932static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001933{
Christopher Faulet129817b2018-09-20 16:14:40 +02001934 struct h1s *h1s = h1c->h1s;
1935 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001936 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001937 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001938 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001939 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001940 int last_data = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001941
Christopher Faulet94b2c762019-05-24 15:28:57 +02001942 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001943 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1944
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001945 if (htx_is_empty(chn_htx))
1946 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001947
Christopher Fauletbef89002022-10-05 07:50:19 +02001948 if (h1s->flags & (H1S_F_INTERNAL_ERROR|H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01001949 goto end;
1950
Christopher Faulet51dbc942018-09-13 09:05:15 +02001951 if (!h1_get_buf(h1c, &h1c->obuf)) {
1952 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001953 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 +02001954 goto end;
1955 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001956
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001957 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001958
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001959 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001960 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001961
Willy Tarreau3815b222018-12-11 19:50:43 +01001962 /* Perform some optimizations to reduce the number of buffer copies.
1963 * First, if the mux's buffer is empty and the htx area contains
1964 * exactly one data block of the same size as the requested count,
1965 * then it's possible to simply swap the caller's buffer with the
1966 * mux's output buffer and adjust offsets and length to match the
1967 * entire DATA HTX block in the middle. In this case we perform a
1968 * true zero-copy operation from end-to-end. This is the situation
1969 * that happens all the time with large files. Second, if this is not
1970 * possible, but the mux's output buffer is empty, we still have an
1971 * opportunity to avoid the copy to the intermediary buffer, by making
1972 * the intermediary buffer's area point to the output buffer's area.
1973 * In this case we want to skip the HTX header to make sure that copies
1974 * remain aligned and that this operation remains possible all the
1975 * time. This goes for headers, data blocks and any data extracted from
1976 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001977 */
1978 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001979 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01001980 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01001981 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001982 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001983 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001984 void *old_area;
1985
Christopher Faulet6b81df72019-10-01 22:08:43 +02001986 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 +01001987 if (h1m->state == H1_MSG_DATA) {
1988 if (h1m->flags & H1_MF_CLEN) {
1989 if (count > h1m->curr_len) {
1990 TRACE_ERROR("too much payload, more than announced",
1991 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1992 goto error;
1993 }
1994 h1m->curr_len -= count;
Christopher Faulet2eb52432022-04-07 10:29:38 +02001995 if (!h1m->curr_len)
1996 last_data = 1;
Christopher Faulet140f1a52021-11-26 16:37:55 +01001997 }
1998 if (chn_htx->flags & HTX_FL_EOM) {
1999 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2000 last_data = 1;
2001 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002002 }
2003
Christopher Faulete5596bf2020-12-02 16:13:22 +01002004 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01002005 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02002006 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01002007 h1c->obuf.data = count;
2008
2009 buf->area = old_area;
2010 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01002011
Christopher Faulet6b81df72019-10-01 22:08:43 +02002012 chn_htx = (struct htx *)buf->area;
2013 htx_reset(chn_htx);
2014
Christopher Fauletadb22202018-12-12 10:32:09 +01002015 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002016 * size and eventually the last chunk. We have at least
2017 * the size of the struct htx to write the chunk
2018 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01002019 */
2020 if (h1m->flags & H1_MF_CHNK) {
2021 h1_emit_chunk_size(&h1c->obuf, count);
2022 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002023 if (last_data) {
2024 /* Emit the last chunk too at the buffer's end */
2025 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
2026 }
Christopher Fauletadb22202018-12-12 10:32:09 +01002027 }
2028
Christopher Faulet6b81df72019-10-01 22:08:43 +02002029 if (h1m->state == H1_MSG_DATA)
2030 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002031 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002032 else
2033 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002034 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002035
Christopher Faulete5596bf2020-12-02 16:13:22 +01002036 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002037 if (last_data) {
2038 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02002039 if (h1s->flags & H1S_F_RX_BLK) {
2040 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002041 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002042 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 +01002043 }
2044
2045 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2046 H1_EV_TX_DATA, h1c->conn, h1s);
2047 }
Willy Tarreau3815b222018-12-11 19:50:43 +01002048 goto out;
2049 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002050 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002051 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002052 else
2053 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02002054
Christopher Fauletc2518a52019-06-25 21:41:02 +02002055 tmp.data = 0;
2056 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02002057 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01002058 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02002059 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01002060 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02002061 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02002062 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02002063
Christopher Fauletb2e84162018-12-06 11:39:49 +01002064 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002065 if (type != HTX_BLK_DATA && vlen > count)
2066 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002067
Christopher Faulet94b2c762019-05-24 15:28:57 +02002068 if (type == HTX_BLK_UNUSED)
2069 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02002070
Christopher Faulet94b2c762019-05-24 15:28:57 +02002071 switch (h1m->state) {
2072 case H1_MSG_RQBEFORE:
2073 if (type != HTX_BLK_REQ_SL)
2074 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002075 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 +02002076 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002077 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02002078 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002079 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002080 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002081 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002082 if (sl->flags & HTX_SL_F_BODYLESS)
2083 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02002084 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01002085 if (h1s->meth == HTTP_METH_HEAD)
2086 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02002087 if (h1s->flags & H1S_F_RX_BLK) {
2088 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002089 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002090 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 +02002091 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002092 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02002093
Christopher Faulet94b2c762019-05-24 15:28:57 +02002094 case H1_MSG_RPBEFORE:
2095 if (type != HTX_BLK_RES_SL)
2096 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002097 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 +02002098 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002099 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02002100 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002101 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002102 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01002103 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002104 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01002105 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02002106 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01002107 else if (h1s->status == 204 || h1s->status == 304)
2108 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02002109 h1m->state = H1_MSG_HDR_FIRST;
2110 break;
2111
Christopher Faulet94b2c762019-05-24 15:28:57 +02002112 case H1_MSG_HDR_FIRST:
2113 case H1_MSG_HDR_NAME:
2114 case H1_MSG_HDR_L2_LWS:
2115 if (type == HTX_BLK_EOH)
2116 goto last_lf;
2117 if (type != HTX_BLK_HDR)
2118 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002119
Christopher Faulet9768c262018-10-22 09:34:31 +02002120 h1m->state = H1_MSG_HDR_NAME;
2121 n = htx_get_blk_name(chn_htx, blk);
2122 v = htx_get_blk_value(chn_htx, blk);
2123
Christopher Faulet86d144c2019-08-14 16:32:25 +02002124 /* Skip all pseudo-headers */
2125 if (*(n.ptr) == ':')
2126 goto skip_hdr;
2127
Christopher Faulet91fcf212020-12-02 16:17:15 +01002128 if (isteq(n, ist("transfer-encoding"))) {
2129 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2130 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02002131 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01002132 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002133 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01002134 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2135 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01002136 /* Only skip C-L header with invalid value. */
2137 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01002138 goto skip_hdr;
2139 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002140 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01002141 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02002142 if (!v.len)
2143 goto skip_hdr;
2144 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01002145 else if (isteq(n, ist("upgrade"))) {
2146 h1_parse_upgrade_header(h1m, v);
2147 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002148 else if ((isteq(n, ist("sec-websocket-accept")) &&
2149 h1m->flags & H1_MF_RESP) ||
2150 (isteq(n, ist("sec-websocket-key")) &&
2151 !(h1m->flags & H1_MF_RESP))) {
Christopher Faulet62138aa2022-11-02 08:42:08 +01002152 h1s->flags |= H1S_F_HAVE_WS_KEY;
Amaury Denoyellec1938232020-12-11 17:53:03 +01002153 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02002154 else if (isteq(n, ist("te"))) {
2155 /* "te" may only be sent with "trailers" if this value
2156 * is present, otherwise it must be deleted.
2157 */
2158 v = istist(v, ist("trailers"));
2159 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2160 goto skip_hdr;
2161 v = ist("trailers");
2162 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002163
Christopher Faulet67d58092019-10-02 10:51:38 +02002164 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002165 if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) &&
2166 isteqi(n, h1c->px->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02002167 goto skip_hdr;
2168
Christopher Faulet98fbe952019-07-22 16:18:24 +02002169 /* Try to adjust the case of the header name */
2170 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2171 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002172 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002173 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002174 skip_hdr:
2175 h1m->state = H1_MSG_HDR_L2_LWS;
2176 break;
2177
Christopher Faulet94b2c762019-05-24 15:28:57 +02002178 case H1_MSG_LAST_LF:
2179 if (type != HTX_BLK_EOH)
2180 goto error;
2181 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002182 h1m->state = H1_MSG_LAST_LF;
2183 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002184 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002185 /* If the reply comes from haproxy while the request is
2186 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002187 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2188 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2189 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002190 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2191 /* T-E + C-L: force close */
2192 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2193 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2194 }
2195 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2196 /* T-E + HTTP/1.0: force close */
2197 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2198 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2199 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002200
2201 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002202 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002203 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002204 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002205 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002206 /* Try to adjust the case of the header name */
2207 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2208 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002209 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002210 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002211 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002212 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002213 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002214
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002215 if ((h1s->meth != HTTP_METH_CONNECT &&
2216 (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 +01002217 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002218 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002219 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002220 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2221 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002222 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002223 n = ist("transfer-encoding");
2224 v = ist("chunked");
2225 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2226 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002227 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002228 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002229 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002230 h1m->flags |= H1_MF_CHNK;
2231 }
2232
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002233 /* Now add the server name to a header (if requested) */
2234 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002235 !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002236 struct server *srv = objt_server(h1c->conn->target);
2237
2238 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002239 n = h1c->px->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002240 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002241
2242 /* Try to adjust the case of the header name */
2243 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2244 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002245 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002246 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002247 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002248 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002249 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2250 }
2251
Amaury Denoyellec1938232020-12-11 17:53:03 +01002252 /* Add websocket handshake key if needed */
Christopher Faulet62138aa2022-11-02 08:42:08 +01002253 if (!(h1s->flags & H1S_F_HAVE_WS_KEY) &&
2254 (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 +01002255 if (!(h1m->flags & H1_MF_RESP)) {
2256 /* generate a random websocket key
2257 * stored in the session to
2258 * verify it on the response side
2259 */
2260 h1_generate_random_ws_input_key(h1s->ws_key);
2261
2262 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2263 ist(h1s->ws_key),
2264 &tmp)) {
2265 goto full;
2266 }
2267 }
2268 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002269 /* add the response header key */
2270 char key[29];
2271 h1_calculate_ws_output_key(h1s->ws_key, key);
2272 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2273 ist(key),
2274 &tmp)) {
2275 goto full;
2276 }
2277 }
Christopher Faulet62138aa2022-11-02 08:42:08 +01002278 h1s->flags |= H1S_F_HAVE_WS_KEY;
Amaury Denoyellec1938232020-12-11 17:53:03 +01002279 }
2280
Christopher Faulet6b81df72019-10-01 22:08:43 +02002281 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2282 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2283
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002284 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002285 if (!chunk_memcat(&tmp, "\r\n", 2))
2286 goto full;
2287 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002288 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002289 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002290 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002291 if (!chunk_memcat(&tmp, "\r\n", 2))
2292 goto full;
2293 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002294 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002295 else if ((h1m->flags & H1_MF_RESP) &&
2296 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002297 if (!chunk_memcat(&tmp, "\r\n", 2))
2298 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002299 h1m_init_res(&h1s->res);
2300 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002301 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002302 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002303 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002304 else {
Christopher Faulet2eb52432022-04-07 10:29:38 +02002305 /* EOM flag is set or empty payload (C-L to 0) and it is the last block */
2306 if (htx_is_unique_blk(chn_htx, blk) &&
2307 ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) {
Mickael Torres226082d2022-11-16 14:29:37 +01002308 if ((h1m->flags & H1_MF_CHNK) && !(h1s->flags & H1S_F_BODYLESS_RESP)) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002309 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2310 goto full;
2311 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002312 else if (!chunk_memcat(&tmp, "\r\n", 2))
2313 goto full;
2314 goto done;
2315 }
2316 else if (!chunk_memcat(&tmp, "\r\n", 2))
2317 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002318 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002319 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002320 break;
2321
Christopher Faulet94b2c762019-05-24 15:28:57 +02002322 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002323 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002324 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002325 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2326 goto trailers;
2327
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002328 /* If the message is not chunked, never
2329 * add the last chunk. */
2330 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002331 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002332 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 +02002333 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002334 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002335 else if (type != HTX_BLK_DATA)
2336 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002337
2338 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 +02002339
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002340 /* It is the last block of this message. After this one,
2341 * only tunneled data may be forwarded. */
2342 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2343 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2344 last_data = 1;
2345 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002346
2347 if (vlen > count) {
2348 /* Get the maximum amount of data we can xferred */
2349 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002350 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002351 }
2352
Christopher Faulet140f1a52021-11-26 16:37:55 +01002353 if (h1m->state == H1_MSG_DATA) {
2354 if (h1m->flags & H1_MF_CLEN) {
2355 if (vlen > h1m->curr_len) {
2356 TRACE_ERROR("too much payload, more than announced",
2357 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2358 goto error;
2359 }
Christopher Faulet140f1a52021-11-26 16:37:55 +01002360 }
2361 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2362 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2363 goto skip_data;
2364 }
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002365 }
2366
Christopher Fauletd9233f02019-10-14 14:01:24 +02002367 chklen = 0;
2368 if (h1m->flags & H1_MF_CHNK) {
2369 chklen = b_room(&tmp);
2370 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2371 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2372 (chklen < 1048576) ? 5 : 8);
2373 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002374
2375 /* If it is the end of the chunked message (without EOT), reserve the
2376 * last chunk size */
2377 if (last_data)
2378 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002379 }
2380
2381 if (vlen + chklen > b_room(&tmp)) {
2382 /* too large for the buffer */
2383 if (chklen >= b_room(&tmp))
2384 goto full;
2385 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002386 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002387 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002388 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002389 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002390 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002391 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002392
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002393 /* Space already reserved, so it must succeed */
2394 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2395 goto error;
2396
Christopher Faulet6b81df72019-10-01 22:08:43 +02002397 if (h1m->state == H1_MSG_DATA)
2398 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002399 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002400 else
2401 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002402 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002403
2404 skip_data:
Christopher Faulet2eb52432022-04-07 10:29:38 +02002405 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Fauletb4eca0e2022-01-10 17:27:51 +01002406 h1m->curr_len -= vlen;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002407 if (!h1m->curr_len)
2408 last_data = 1;
2409 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002410 if (last_data)
2411 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002412 break;
2413
Christopher Faulet94b2c762019-05-24 15:28:57 +02002414 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002415 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002416 goto error;
2417 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002418 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002419
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002420 if (!(h1m->flags & H1_MF_CHNK))
2421 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002422
Christopher Faulete5596bf2020-12-02 16:13:22 +01002423 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2424 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 +01002425 if (type == HTX_BLK_EOT)
2426 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002427 break;
2428 }
2429
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002430 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002431 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002432 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002433 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2434 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002435 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002436 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002437 else { // HTX_BLK_TLR
2438 n = htx_get_blk_name(chn_htx, blk);
2439 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002440
2441 /* Try to adjust the case of the header name */
2442 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2443 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002444 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002445 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002446 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002447 break;
2448
Christopher Faulet94b2c762019-05-24 15:28:57 +02002449 case H1_MSG_DONE:
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002450 /* If the message is not chunked, ignore
2451 * trailers. It may happen with H2 messages. */
2452 if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && !(h1m->flags & H1_MF_CHNK))
2453 break;
2454
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002455 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2456 goto error; /* For now return an error */
2457
Christopher Faulet94b2c762019-05-24 15:28:57 +02002458 done:
2459 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002460 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002461 h1s->flags |= H1S_F_TX_BLK;
2462 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002463 }
2464 else if ((h1m->flags & H1_MF_RESP) &&
2465 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2466 /* a successful reply to a CONNECT or a protocol switching is sent
2467 * to the client. Switch the response to tunnel mode.
2468 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002469 h1_set_tunnel_mode(h1s);
Christopher Fauletf3158e92019-11-15 11:14:23 +01002470 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002471
Christopher Faulet10a86702021-04-07 14:18:11 +02002472 if (h1s->flags & H1S_F_RX_BLK) {
2473 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002474 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002475 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 +02002476 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002477
2478 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2479 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002480 break;
2481
Christopher Faulet9768c262018-10-22 09:34:31 +02002482 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002483 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002484 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002485 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002486 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletfc473a62022-10-05 08:22:33 +02002487 se_fl_set(h1s->sd, SE_FL_ERROR);
2488 TRACE_ERROR("processing output error, set error on h1s",
2489 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01002490 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02002491 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002492
2493 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002494 total += vlen;
2495 count -= vlen;
2496 if (sz == vlen)
2497 blk = htx_remove_blk(chn_htx, blk);
2498 else {
2499 htx_cut_data_blk(chn_htx, blk, vlen);
2500 break;
2501 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002502 }
2503
Christopher Faulet9768c262018-10-22 09:34:31 +02002504 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002505 /* when the output buffer is empty, tmp shares the same area so that we
2506 * only have to update pointers and lengths.
2507 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002508 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2509 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002510 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002511 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002512
Willy Tarreau3815b222018-12-11 19:50:43 +01002513 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002514 out:
2515 if (!buf_room_for_htx_data(&h1c->obuf)) {
2516 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002517 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002518 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002519 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002520 /* Both the request and the response reached the DONE state. So set EOI
Willy Tarreau4596fe22022-05-17 19:07:51 +02002521 * flag on the stream connector. Most of time, the flag will already be set,
Christopher Fauletdea24742021-01-22 15:12:30 +01002522 * except for protocol upgrades. Report an error if data remains blocked
2523 * in the output buffer.
2524 */
2525 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002526 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletdea24742021-01-22 15:12:30 +01002527 if (!htx_is_empty(chn_htx)) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002528 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
2529 h1s->flags |= H1S_F_PROCESSING_ERROR;
2530 se_fl_set(h1s->sd, SE_FL_ERROR);
2531 TRACE_ERROR("txn done but data waiting to be sent, set error on h1s",
2532 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002533 }
Christopher Fauletdea24742021-01-22 15:12:30 +01002534 }
2535
Christopher Faulet6b81df72019-10-01 22:08:43 +02002536 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002537 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002538
2539 full:
2540 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2541 h1c->flags |= H1C_F_OUT_FULL;
2542 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002543}
2544
Christopher Faulet51dbc942018-09-13 09:05:15 +02002545/*********************************************************/
2546/* functions below are I/O callbacks from the connection */
2547/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002548static void h1_wake_stream_for_recv(struct h1s *h1s)
2549{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002550 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002551 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002552 tasklet_wakeup(h1s->subs->tasklet);
2553 h1s->subs->events &= ~SUB_RETRY_RECV;
2554 if (!h1s->subs->events)
2555 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002556 }
2557}
2558static void h1_wake_stream_for_send(struct h1s *h1s)
2559{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002560 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002561 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002562 tasklet_wakeup(h1s->subs->tasklet);
2563 h1s->subs->events &= ~SUB_RETRY_SEND;
2564 if (!h1s->subs->events)
2565 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002566 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002567}
2568
Christopher Fauletad4daf62021-01-21 17:49:01 +01002569/* alerts the data layer following this sequence :
2570 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2571 * - if its subscribed to send, then it's woken up for send
2572 * - if it was subscribed to neither, its ->wake() callback is called
2573 */
2574static void h1_alert(struct h1s *h1s)
2575{
2576 if (h1s->subs) {
2577 h1_wake_stream_for_recv(h1s);
2578 h1_wake_stream_for_send(h1s);
2579 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002580 else if (h1s_sc(h1s) && h1s_sc(h1s)->app_ops->wake != NULL) {
Christopher Fauletad4daf62021-01-21 17:49:01 +01002581 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002582 h1s_sc(h1s)->app_ops->wake(h1s_sc(h1s));
Christopher Fauletad4daf62021-01-21 17:49:01 +01002583 }
2584}
2585
Christopher Fauletc18fc232020-10-06 17:41:36 +02002586/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
Christopher Faulet56a49942022-10-04 17:45:24 +02002587 * and 0 on error. The flag H1C_F_ABRT_PENDING is set on the H1 connection for
Christopher Fauletc18fc232020-10-06 17:41:36 +02002588 * retryable errors (allocation error or buffer full). On success, the error is
2589 * copied in the output buffer.
2590*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002591static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002592{
2593 int rc = http_get_status_idx(h1c->errcode);
2594 int ret = 0;
2595
2596 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2597
2598 /* Verify if the error is mapped on /dev/null or any empty file */
2599 /// XXX: do a function !
2600 if (h1c->px->replies[rc] &&
2601 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2602 h1c->px->replies[rc]->body.errmsg &&
2603 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2604 /* Empty error, so claim a success */
2605 ret = 1;
2606 goto out;
2607 }
2608
2609 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002610 h1c->flags |= H1C_F_ABRT_PENDING;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002611 goto out;
2612 }
2613
2614 if (!h1_get_buf(h1c, &h1c->obuf)) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002615 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ABRT_PENDING);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002616 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2617 goto out;
2618 }
Tim Duesterhus77508502022-03-15 13:11:06 +01002619 ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc]));
Christopher Fauletc18fc232020-10-06 17:41:36 +02002620 if (unlikely(ret <= 0)) {
2621 if (!ret) {
Christopher Faulet56a49942022-10-04 17:45:24 +02002622 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ABRT_PENDING);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002623 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2624 goto out;
2625 }
2626 else {
2627 /* we cannot report this error, so claim a success */
2628 ret = 1;
2629 }
2630 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02002631
2632 if (h1c->h1s) {
2633 TRACE_DEVEL("Abort embryonic H1S", H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2634 h1s_destroy(h1c->h1s);
2635 }
2636
Christopher Faulet31da34d2022-10-10 16:36:10 +02002637 h1c->flags = (h1c->flags & ~H1C_F_ABRT_PENDING) | H1C_F_ABRTED;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002638 h1c->state = H1_CS_CLOSING;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002639 out:
2640 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2641 return ret;
2642}
2643
2644/* Try to send a 500 internal error. It relies on h1_send_error to send the
2645 * error. This function takes care of incrementing stats and tracked counters.
2646 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002647static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002648{
2649 struct session *sess = h1c->conn->owner;
2650 int ret = 1;
2651
2652 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002653 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002654 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2655 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002656 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002657 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002658
2659 h1c->errcode = 500;
2660 ret = h1_send_error(h1c);
2661 sess_log(sess);
2662 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002663}
2664
Christopher Fauletb3230f72021-09-21 18:38:20 +02002665/* Try to send an error because of a parsing error. By default a 400 bad request
2666 * error is returned. But the status code may be specified by setting
2667 * h1c->errcode. It relies on h1_send_error to send the error. This function
2668 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002669 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002670static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002671{
2672 struct session *sess = h1c->conn->owner;
2673 int ret = 1;
2674
Christopher Faulet4e72b172022-10-04 17:35:19 +02002675 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
2676 h1c->state = H1_CS_CLOSING;
Christopher Faulet31da34d2022-10-10 16:36:10 +02002677 h1c->flags |= H1C_F_ABRTED;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002678 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002679 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002680
2681 session_inc_http_req_ctr(sess);
2682 session_inc_http_err_ctr(sess);
2683 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002684 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2685 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002686 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002687 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002688
Christopher Fauletb3230f72021-09-21 18:38:20 +02002689 if (!h1c->errcode)
2690 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002691 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002692 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2693 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002694
2695 end:
2696 return ret;
2697}
2698
Christopher Faulet2eed8002020-12-07 11:26:13 +01002699/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2700 * the error. This function takes care of incrementing stats and tracked
2701 * counters.
2702 */
2703static int h1_handle_not_impl_err(struct h1c *h1c)
2704{
2705 struct session *sess = h1c->conn->owner;
2706 int ret = 1;
2707
Christopher Faulet4e72b172022-10-04 17:35:19 +02002708 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
2709 h1c->state = H1_CS_CLOSING;
Christopher Faulet31da34d2022-10-10 16:36:10 +02002710 h1c->flags |= H1C_F_ABRTED;
Christopher Faulet2eed8002020-12-07 11:26:13 +01002711 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002712 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002713
2714 session_inc_http_req_ctr(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002715 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002716 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2717 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002718 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002719 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002720
2721 h1c->errcode = 501;
2722 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002723 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2724 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002725
2726 end:
2727 return ret;
2728}
2729
Christopher Fauletc18fc232020-10-06 17:41:36 +02002730/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2731 * error. This function takes care of incrementing stats and tracked counters.
2732 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002733static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002734{
2735 struct session *sess = h1c->conn->owner;
2736 int ret = 1;
2737
Christopher Faulet4e72b172022-10-04 17:35:19 +02002738 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
2739 h1c->state = H1_CS_CLOSING;
Christopher Faulet31da34d2022-10-10 16:36:10 +02002740 h1c->flags |= H1C_F_ABRTED;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002741 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002742 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002743
2744 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002745 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002746 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2747 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002748 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002749 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002750
2751 h1c->errcode = 408;
Christopher Faulet07e10de2021-07-26 09:42:49 +02002752 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2753 ret = h1_send_error(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002754 sess_log(sess);
2755 end:
2756 return ret;
2757}
2758
2759
Christopher Faulet51dbc942018-09-13 09:05:15 +02002760/*
2761 * Attempt to read data, and subscribe if none available
2762 */
2763static int h1_recv(struct h1c *h1c)
2764{
2765 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002766 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002767 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002768
Christopher Faulet6b81df72019-10-01 22:08:43 +02002769 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2770
2771 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2772 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002773 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002774 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002775
Christopher Fauletae635762020-09-21 11:47:16 +02002776 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2777 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002778 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002779 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002780
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002781 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2782 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002783 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002784 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002785 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002786
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002787 /*
2788 * If we only have a small amount of data, realign it,
2789 * it's probably cheaper than doing 2 recv() calls.
2790 */
2791 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002792 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002793
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002794 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002795 if (!h1c->h1s ||
2796 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2797 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002798 flags |= CO_RFL_READ_ONCE;
2799
Willy Tarreau45f2b892018-12-05 07:59:27 +01002800 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002801 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002802 if (h1c->flags & H1C_F_IN_FULL) {
2803 h1c->flags &= ~H1C_F_IN_FULL;
2804 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2805 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002806
2807 if (!b_data(&h1c->ibuf)) {
2808 /* try to pre-align the buffer like the rxbufs will be
2809 * to optimize memory copies.
2810 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002811 h1c->ibuf.head = sizeof(struct htx);
2812 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002813 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet41951ab2021-11-26 18:12:51 +01002814 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002815 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02002816
2817 if (conn_xprt_read0_pending(conn)) {
2818 TRACE_DEVEL("read0 on connection", H1_EV_H1C_RECV, h1c->conn);
2819 h1c->flags |= H1C_F_EOS;
2820 }
2821 if (h1c->conn->flags & CO_FL_ERROR) {
2822 TRACE_DEVEL("connection error", H1_EV_H1C_RECV, h1c->conn);
2823 h1c->flags |= H1C_F_ERROR;
2824 }
2825
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002826 if (max && !ret && h1_recv_allowed(h1c)) {
2827 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2828 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002829 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002830 else {
Christopher Fauletfc473a62022-10-05 08:22:33 +02002831 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 +02002832 h1_wake_stream_for_recv(h1c->h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002833 }
2834
Christopher Faulet51dbc942018-09-13 09:05:15 +02002835 if (!b_data(&h1c->ibuf))
2836 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002837 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002838 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002839 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2840 }
2841
2842 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002843 return !!ret || (h1c->flags & (H1C_F_EOS|H1C_F_ERROR));
Christopher Faulet51dbc942018-09-13 09:05:15 +02002844}
2845
2846
2847/*
2848 * Try to send data if possible
2849 */
2850static int h1_send(struct h1c *h1c)
2851{
2852 struct connection *conn = h1c->conn;
2853 unsigned int flags = 0;
2854 size_t ret;
2855 int sent = 0;
2856
Christopher Faulet6b81df72019-10-01 22:08:43 +02002857 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2858
Christopher Fauletfc473a62022-10-05 08:22:33 +02002859 if (h1c->flags & (H1C_F_ERROR|H1C_F_ERR_PENDING)) {
2860 TRACE_DEVEL("leaving on H1C error|err_pending", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002861 b_reset(&h1c->obuf);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002862 if (h1c->flags & H1C_F_EOS)
2863 h1c->flags |= H1C_F_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002864 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002865 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002866
2867 if (!b_data(&h1c->obuf))
2868 goto end;
2869
Christopher Faulet46230362019-10-17 16:04:20 +02002870 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002871 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002872 if (h1c->flags & H1C_F_CO_STREAMER)
2873 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002874
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002875 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002876 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002877 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002878 if (h1c->flags & H1C_F_OUT_FULL) {
2879 h1c->flags &= ~H1C_F_OUT_FULL;
2880 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2881 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01002882 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002883 b_del(&h1c->obuf, ret);
2884 sent = 1;
2885 }
2886
Christopher Fauletfc473a62022-10-05 08:22:33 +02002887 if (conn->flags & CO_FL_ERROR) {
2888 /* connection error, nothing to send, clear the buffer to release it */
2889 TRACE_DEVEL("connection error", H1_EV_H1C_SEND, h1c->conn);
2890 h1c->flags |= H1C_F_ERR_PENDING;
2891 if (h1c->flags & H1C_F_EOS)
2892 h1c->flags |= H1C_F_ERROR;
Christopher Faulet145aa472018-12-06 10:56:20 +01002893 b_reset(&h1c->obuf);
2894 }
2895
Christopher Faulet51dbc942018-09-13 09:05:15 +02002896 end:
Christopher Fauletc17c31c2022-02-01 18:25:06 +01002897 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002898 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002899
Christopher Faulet51dbc942018-09-13 09:05:15 +02002900 /* We're done, no more to send */
2901 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002902 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002903 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002904 if (h1c->state == H1_CS_CLOSING) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002905 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002906 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002907 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002908 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002909 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2910 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002911 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002912 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002913
Christopher Faulet6b81df72019-10-01 22:08:43 +02002914 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002915 return sent || (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) || (h1c->state == H1_CS_CLOSED);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002916}
2917
Christopher Faulet51dbc942018-09-13 09:05:15 +02002918/* callback called on any event by the connection handler.
2919 * It applies changes and returns zero, or < 0 if it wants immediate
2920 * destruction of the connection.
2921 */
2922static int h1_process(struct h1c * h1c)
2923{
2924 struct connection *conn = h1c->conn;
2925
Christopher Faulet6b81df72019-10-01 22:08:43 +02002926 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2927
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002928 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002929 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Faulet4e72b172022-10-04 17:35:19 +02002930 (h1c->state < H1_CS_RUNNING) && /* IDLE, EMBRYONIC or UPGRADING */
Christopher Fauletfc473a62022-10-05 08:22:33 +02002931 !(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 +02002932 struct h1s *h1s = h1c->h1s;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002933 struct buffer *buf;
2934 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002935
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002936 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2937 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002938 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002939
2940 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002941 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002942 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2943 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002944 /* Try to match H2 preface before parsing the request headers. */
2945 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2946 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002947 if (h1c->state == H1_CS_UPGRADING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02002948 BUG_ON(!h1s);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002949 se_fl_set(h1s->sd, SE_FL_EOS); /* Set EOS here to release the SC */
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002950 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002951 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002952 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002953 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002954 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002955
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002956 /* Create the H1 stream if not already there */
2957 if (!h1s) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002958 h1s = h1c_frt_stream_new(h1c, NULL, h1c->conn->owner);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002959 if (!h1s) {
2960 b_reset(&h1c->ibuf);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002961 h1_handle_internal_err(h1c);
2962 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
2963 TRACE_ERROR("alloc error", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002964 goto no_parsing;
2965 }
2966 }
2967
2968 if (h1s->sess->t_idle == -1)
2969 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2970
2971 /* Get the stream rxbuf */
2972 buf = h1_get_buf(h1c, &h1s->rxbuf);
2973 if (!buf) {
2974 h1c->flags |= H1C_F_IN_SALLOC;
2975 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2976 return 0;
2977 }
2978
2979 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01002980 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002981 h1_release_buf(h1c, &h1s->rxbuf);
2982 h1_set_idle_expiration(h1c);
2983
Christopher Fauletbef89002022-10-05 07:50:19 +02002984 if (h1s->flags & H1S_F_INTERNAL_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002985 h1_handle_internal_err(h1c);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002986 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
Christopher Faulet26a26432021-01-27 11:27:50 +01002987 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002988 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002989 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2990 h1_handle_not_impl_err(h1c);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002991 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
Christopher Faulet26a26432021-01-27 11:27:50 +01002992 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002993 }
Christopher Faulet10498562022-10-10 18:05:25 +02002994 else if (h1s->flags & H1S_F_PARSING_ERROR || se_fl_test(h1s->sd, SE_FL_ERROR)) {
2995 h1_handle_parsing_error(h1c);
Christopher Fauletfc473a62022-10-05 08:22:33 +02002996 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
Christopher Faulet10498562022-10-10 18:05:25 +02002997 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
2998 }
Christopher Faulet2177d962022-10-05 08:39:14 +02002999 else if (h1c->state < H1_CS_RUNNING) {
3000 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
3001 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3002 }
Christopher Fauletae635762020-09-21 11:47:16 +02003003 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003004
Christopher Faulete6ef4cd2022-11-17 15:54:12 +01003005 no_parsing:
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003006 h1_send(h1c);
3007
Christopher Faulet75308302021-11-15 14:51:37 +01003008 /* H1 connection must be released ASAP if:
Christopher Fauletfc473a62022-10-05 08:22:33 +02003009 * - an error occurred on the H1C or
Christopher Faulet75308302021-11-15 14:51:37 +01003010 * - a read0 was received or
3011 * - a silent shutdown was emitted and all outgoing data sent
3012 */
Christopher Faulet31da34d2022-10-10 16:36:10 +02003013 if ((h1c->flags & (H1C_F_EOS|H1C_F_ERROR|H1C_F_ABRT_PENDING|H1C_F_ABRTED)) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003014 (h1c->state >= H1_CS_CLOSING && (h1c->flags & H1C_F_SILENT_SHUT) && !b_data(&h1c->obuf))) {
3015 if (h1c->state != H1_CS_RUNNING) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003016 /* No stream connector or upgrading */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003017 if (h1c->state < H1_CS_RUNNING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ERROR))) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003018 /* shutdown for reads and no error on the frontend connection: Send an error */
Christopher Fauletb3230f72021-09-21 18:38:20 +02003019 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003020 h1_send(h1c);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003021 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003022 }
Christopher Faulet56a49942022-10-04 17:45:24 +02003023 else if (h1c->flags & H1C_F_ABRT_PENDING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003024 /* Handle pending error, if any (only possible on frontend connection) */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003025 BUG_ON(h1c->flags & H1C_F_IS_BACK);
3026 if (h1_send_error(h1c))
3027 h1_send(h1c);
3028 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02003029 else {
3030 h1_close(h1c);
3031 TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn);
3032 }
Christopher Fauletae635762020-09-21 11:47:16 +02003033
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003034 /* If there is some pending outgoing data or error, just wait */
Christopher Faulet56a49942022-10-04 17:45:24 +02003035 if (h1c->state == H1_CS_CLOSING || (h1c->flags & H1C_F_ABRT_PENDING))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003036 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01003037
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003038 /* Otherwise we can release the H1 connection */
3039 goto release;
3040 }
3041 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003042 struct h1s *h1s = h1c->h1s;
3043
Willy Tarreau4596fe22022-05-17 19:07:51 +02003044 /* Here there is still a H1 stream with a stream connector.
Christopher Fauletfc473a62022-10-05 08:22:33 +02003045 * Report an error at the stream level and wake up the stream
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003046 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003047 BUG_ON(!h1s);
3048
Christopher Fauletfc473a62022-10-05 08:22:33 +02003049 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3050 se_fl_set_error(h1s->sd);
3051 TRACE_STATE("report (ERR_PENDING|ERROR) to SE", H1_EV_H1C_RECV, conn, h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003052 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003053 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01003054 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003055 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003056 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003057
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003058 if (!b_data(&h1c->ibuf))
3059 h1_release_buf(h1c, &h1c->ibuf);
3060
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003061 /* Check if a soft-stop is in progress.
3062 * Release idling front connection if this is the case.
3063 */
3064 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003065 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
William Dauchya9dd9012022-01-05 22:53:24 +01003066 if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) &&
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003067 h1c->flags & H1C_F_WAIT_NEXT_REQ) {
3068
3069 int send_close = 1;
3070 /* If a close-spread-time option is set, we want to avoid
3071 * closing all the active HTTP2 connections at once so we add a
3072 * random factor that will spread the closing.
3073 */
3074 if (tick_isset(global.close_spread_end)) {
3075 int remaining_window = tick_remain(now_ms, global.close_spread_end);
3076 if (remaining_window) {
3077 /* This should increase the closing rate the
3078 * further along the window we are.
3079 */
3080 send_close = (remaining_window <= statistical_prng_range(global.close_spread_time));
3081 }
3082 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02003083 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
3084 send_close = 0; /* let the client close his connection himself */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003085 if (send_close)
3086 goto release;
3087 }
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003088 }
3089 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003090
Christopher Faulet4e72b172022-10-04 17:35:19 +02003091 if (h1c->state == H1_CS_RUNNING && (h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1c->h1s)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003092 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 +02003093 h1_wake_stream_for_recv(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003094 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003095
Christopher Faulet47365272018-10-31 17:40:50 +01003096 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02003097 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003098 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003099 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01003100
3101 release:
Christopher Faulet4e72b172022-10-04 17:35:19 +02003102 if (h1c->state == H1_CS_UPGRADING) {
3103 struct h1s *h1s = h1c->h1s;
3104
3105 /* Don't release the H1 connection right now, we must destroy
3106 * the attached SC first */
3107 BUG_ON(!h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003108
Christopher Fauletfc473a62022-10-05 08:22:33 +02003109 if (h1c->flags & H1C_F_EOS) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003110 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003111 TRACE_STATE("report EOS to SE", H1_EV_H1C_RECV, conn, h1s);
3112 }
3113 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3114 se_fl_set_error(h1s->sd);
3115 TRACE_STATE("report (ERR_PENDING|ERROR) to SE", H1_EV_H1C_RECV, conn, h1s);
3116 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003117 h1_alert(h1s);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003118 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003119 }
3120 else {
3121 h1_release(h1c);
3122 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
3123 }
Christopher Faulet539e0292018-11-19 10:40:09 +01003124 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003125}
3126
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003127struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003128{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003129 struct connection *conn;
3130 struct tasklet *tl = (struct tasklet *)t;
3131 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003132 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003133 int ret = 0;
3134
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003135 if (state & TASK_F_USR1) {
3136 /* the tasklet was idling on an idle connection, it might have
3137 * been stolen, let's be careful!
3138 */
3139 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3140 if (tl->context == NULL) {
3141 /* The connection has been taken over by another thread,
3142 * we're no longer responsible for it, so just free the
3143 * tasklet, and do nothing.
3144 */
3145 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3146 tasklet_free(tl);
3147 return NULL;
3148 }
3149 conn = h1c->conn;
3150 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003151
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003152 /* Remove the connection from the list, to be sure nobody attempts
3153 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003154 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003155 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3156 if (conn_in_list)
3157 conn_delete_from_tree(&conn->hash_node->node);
3158
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003159 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003160 } else {
3161 /* we're certain the connection was not in an idle list */
3162 conn = h1c->conn;
3163 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
3164 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003165 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003166
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003167 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003168 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003169 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003170 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003171 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003172 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01003173
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003174 /* If we were in an idle list, we want to add it back into it,
3175 * unless h1_process() returned -1, which mean it has destroyed
3176 * the connection (testing !ret is enough, if h1_process() wasn't
3177 * called then ret will be 0 anyway.
3178 */
Willy Tarreau74163142021-03-13 11:30:19 +01003179 if (ret < 0)
3180 t = NULL;
3181
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003182 if (!ret && conn_in_list) {
3183 struct server *srv = objt_server(conn->target);
3184
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003185 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003186 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02003187 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003188 else
Willy Tarreau85223482022-09-29 20:32:43 +02003189 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003190 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003191 }
Willy Tarreau74163142021-03-13 11:30:19 +01003192 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003193}
3194
Christopher Faulet51dbc942018-09-13 09:05:15 +02003195static int h1_wake(struct connection *conn)
3196{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003197 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01003198 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003199
Christopher Faulet6b81df72019-10-01 22:08:43 +02003200 TRACE_POINT(H1_EV_H1C_WAKE, conn);
3201
Christopher Faulet539e0292018-11-19 10:40:09 +01003202 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01003203 ret = h1_process(h1c);
3204 if (ret == 0) {
3205 struct h1s *h1s = h1c->h1s;
3206
Christopher Faulet4e72b172022-10-04 17:35:19 +02003207 if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING)
Christopher Fauletad4daf62021-01-21 17:49:01 +01003208 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003209 }
3210 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003211}
3212
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003213/* Connection timeout management. The principle is that if there's no receipt
3214 * nor sending for a certain amount of time, the connection is closed.
3215 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003216struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003217{
3218 struct h1c *h1c = context;
3219 int expired = tick_is_expired(t->expire, now_ms);
3220
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003221 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003222
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003223 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003224 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003225 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003226
3227 /* Somebody already stole the connection from us, so we should not
3228 * free it, we just have to free the task.
3229 */
3230 if (!t->context) {
3231 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003232 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003233 goto do_leave;
3234 }
3235
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003236 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003237 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003238 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003239 return t;
3240 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003241
Willy Tarreau4596fe22022-05-17 19:07:51 +02003242 /* If a stream connector is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003243 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003244 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003245 if (h1c->state == H1_CS_RUNNING) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003246 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003247 t->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +02003248 TRACE_DEVEL("leaving (SC still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003249 return t;
3250 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003251
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003252 /* Try to send an error to the client */
Christopher Faulet56a49942022-10-04 17:45:24 +02003253 if (h1c->state != H1_CS_CLOSING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ERROR|H1C_F_ABRT_PENDING))) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003254 h1c->flags |= H1C_F_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003255 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003256 if (h1_handle_req_tout(h1c))
3257 h1_send(h1c);
Christopher Faulet56a49942022-10-04 17:45:24 +02003258 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ABRT_PENDING)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003259 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003260 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003261 return t;
3262 }
3263 }
3264
Christopher Faulet4e72b172022-10-04 17:35:19 +02003265 if (h1c->state == H1_CS_UPGRADING) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003266 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet4e72b172022-10-04 17:35:19 +02003267 * attached SC first. */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003268 se_fl_set(h1c->h1s->sd, SE_FL_EOS | SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003269 h1_alert(h1c->h1s);
3270 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003271 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003272 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003273 return t;
3274 }
3275
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003276 /* We're about to destroy the connection, so make sure nobody attempts
3277 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003278 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003279 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003280 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003281
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003282 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003283 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003284
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003285 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003286 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003287
3288 if (!h1c) {
3289 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003290 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003291 return NULL;
3292 }
3293
3294 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003295 h1_release(h1c);
3296 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003297 return NULL;
3298}
3299
Christopher Faulet51dbc942018-09-13 09:05:15 +02003300/*******************************************/
3301/* functions below are used by the streams */
3302/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003303
Christopher Faulet51dbc942018-09-13 09:05:15 +02003304/*
3305 * Attach a new stream to a connection
3306 * (Used for outgoing connections)
3307 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003308static int h1_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003309{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003310 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003311 struct h1s *h1s;
3312
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003313 /* this connection is no more idle (if it was at all) */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003314 h1c->flags &= ~H1C_F_SILENT_SHUT;
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003315
Christopher Faulet6b81df72019-10-01 22:08:43 +02003316 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Fauletfc473a62022-10-05 08:22:33 +02003317 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003318 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3319 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003320 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003321
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003322 h1s = h1c_bck_stream_new(h1c, sd->sc, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003323 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003324 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3325 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003326 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003327
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003328 /* the connection is not idle anymore, let's mark this */
3329 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003330 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003331
Christopher Faulet6b81df72019-10-01 22:08:43 +02003332 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulete00ad352021-12-16 14:44:31 +01003333 return 0;
Christopher Faulet26a26432021-01-27 11:27:50 +01003334 err:
Christopher Faulet26a26432021-01-27 11:27:50 +01003335 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 +01003336 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003337}
3338
Willy Tarreau4596fe22022-05-17 19:07:51 +02003339/* Retrieves a valid stream connector from this connection, or returns NULL.
3340 * For this mux, it's easy as we can only store a single stream connector.
Christopher Faulet51dbc942018-09-13 09:05:15 +02003341 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003342static struct stconn *h1_get_first_sc(const struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003343{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003344 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003345 struct h1s *h1s = h1c->h1s;
3346
3347 if (h1s)
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02003348 return h1s_sc(h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003349
3350 return NULL;
3351}
3352
Christopher Faulet73c12072019-04-08 11:23:22 +02003353static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003354{
Christopher Faulet73c12072019-04-08 11:23:22 +02003355 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003356
Christopher Faulet6b81df72019-10-01 22:08:43 +02003357 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet7c452cc2022-04-14 11:08:26 +02003358 if (!h1c->h1s || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003359 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003360}
3361
3362/*
3363 * Detach the stream from the connection and possibly release the connection.
3364 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003365static void h1_detach(struct sedesc *sd)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003366{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003367 struct h1s *h1s = sd->se;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003368 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003369 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003370 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003371
Christopher Faulet6b81df72019-10-01 22:08:43 +02003372 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3373
Christopher Faulet6b81df72019-10-01 22:08:43 +02003374 if (!h1s) {
3375 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003376 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003377 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003378
Olivier Houchardf502aca2018-12-14 19:42:40 +01003379 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003380 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003381
Christopher Faulet42849b02020-10-05 11:35:17 +02003382 sess->accept_date = date;
3383 sess->tv_accept = now;
3384 sess->t_handshake = 0;
3385 sess->t_idle = -1;
3386
Olivier Houchard8a786902018-12-15 16:05:40 +01003387 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3388 h1s_destroy(h1s);
3389
Christopher Faulet4e72b172022-10-04 17:35:19 +02003390 if (h1c->state == H1_CS_IDLE && (h1c->flags & H1C_F_IS_BACK)) {
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003391 /* this connection may be killed at any moment, we want it to
3392 * die "cleanly" (i.e. only an RST).
3393 */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003394 h1c->flags |= H1C_F_SILENT_SHUT;
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003395
Christopher Fauletf1204b82019-07-19 14:51:06 +02003396 /* If there are any excess server data in the input buffer,
3397 * release it and close the connection ASAP (some data may
3398 * remain in the output buffer). This happens if a server sends
3399 * invalid responses. So in such case, we don't want to reuse
3400 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003401 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003402 if (b_data(&h1c->ibuf)) {
3403 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet4e72b172022-10-04 17:35:19 +02003404 h1c->state = H1_CS_CLOSING;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003405 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003406 goto release;
3407 }
Christopher Faulet03627242019-07-19 11:34:08 +02003408
Christopher Faulet08016ab2020-07-01 16:10:06 +02003409 if (h1c->conn->flags & CO_FL_PRIVATE) {
3410 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003411 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3412 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003413 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003414 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003415 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003416 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003417 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003418 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003419 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3420 goto end;
3421 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003422 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003423 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003424 if (h1c->conn->owner == sess)
3425 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003426
3427 /* mark that the tasklet may lose its context to another thread and
3428 * that the handler needs to check it under the idle conns lock.
3429 */
3430 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003431 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003432 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3433
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003434 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003435 /* The server doesn't want it, let's kill the connection right away */
3436 h1c->conn->mux->destroy(h1c);
3437 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3438 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003439 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003440 /* At this point, the connection has been added to the
3441 * server idle list, so another thread may already have
3442 * hijacked it, so we can't do anything with it.
3443 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003444 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003445 }
3446 }
3447
Christopher Fauletf1204b82019-07-19 14:51:06 +02003448 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003449 /* 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 +02003450 if ((h1c->flags & H1C_F_ERROR) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003451 (h1c->state == H1_CS_CLOSED) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003452 (h1c->state == H1_CS_CLOSING && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003453 !h1c->conn->owner) {
3454 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003455 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003456 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003457 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003458 if (h1c->state == H1_CS_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003459 /* If we have a new request, process it immediately or
3460 * subscribe for reads waiting for new data
3461 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003462 if (unlikely(b_data(&h1c->ibuf))) {
3463 if (h1_process(h1c) == -1)
3464 goto end;
3465 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003466 else
3467 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3468 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003469 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003470 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003471 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003472 end:
3473 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003474}
3475
3476
Willy Tarreau000d63c2022-05-27 10:39:17 +02003477static void h1_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003478{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003479 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet7f366362019-04-08 10:51:20 +02003480 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003481
3482 if (!h1s)
3483 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003484 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003485
Christopher Fauletc3fe6f32022-10-05 10:24:11 +02003486 TRACE_POINT(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003487}
3488
Willy Tarreau000d63c2022-05-27 10:39:17 +02003489static void h1_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003490{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003491 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003492 struct h1c *h1c;
3493
3494 if (!h1s)
3495 return;
3496 h1c = h1s->h1c;
3497
Christopher Faulet99293b02021-11-10 10:30:15 +01003498 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003499
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003500 if (se_fl_test(h1s->sd, SE_FL_SHW))
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003501 goto end;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003502 if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003503 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003504 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003505 }
Christopher Fauletfc473a62022-10-05 08:22:33 +02003506 if (h1c->state == H1_CS_CLOSING || (h1c->flags & (H1C_F_EOS|H1C_F_ERR_PENDING|H1C_F_ERROR))) {
3507 TRACE_STATE("shutdown on connection (EOS || CLOSING || ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003508 goto do_shutw;
3509 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003510
Christopher Fauletfc473a62022-10-05 08:22:33 +02003511 if (h1c->state == H1_CS_UPGRADING) {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003512 TRACE_STATE("keep connection alive (UPGRADING)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003513 goto end;
3514 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003515 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3516 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003517 goto end;
3518 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003519
Christopher Faulet7f366362019-04-08 10:51:20 +02003520 do_shutw:
Christopher Faulet4e72b172022-10-04 17:35:19 +02003521 h1c->state = H1_CS_CLOSING;
Christopher Faulet07976562022-03-31 11:05:05 +02003522 if (mode != CO_SHW_NORMAL)
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003523 h1c->flags |= H1C_F_SILENT_SHUT;
Christopher Fauleta85c5222021-10-27 15:36:38 +02003524
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003525 if (!b_data(&h1c->obuf))
Christopher Faulet897d6122021-12-17 17:28:35 +01003526 h1_shutw_conn(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003527 end:
3528 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003529}
3530
Christopher Fauleta85c5222021-10-27 15:36:38 +02003531static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003532{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003533 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003534
Christopher Faulet4e72b172022-10-04 17:35:19 +02003535 TRACE_ENTER(H1_EV_H1C_END, conn);
3536 h1_close(h1c);
Willy Tarreau62592ad2021-03-26 09:09:42 +01003537 if (conn->flags & CO_FL_SOCK_WR_SH)
3538 return;
3539
Christopher Faulet666a0c42019-01-08 11:12:04 +01003540 conn_xprt_shutw(conn);
Christopher Fauletce7928d2022-11-18 08:44:44 +01003541 conn_sock_shutw(conn, !(h1c->flags & H1C_F_SILENT_SHUT));
Christopher Fauleta85c5222021-10-27 15:36:38 +02003542 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003543}
3544
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003545/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3546 * The <es> pointer is not allowed to differ from the one passed to the
3547 * subscribe() call. It always returns zero.
3548 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003549static int h1_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003550{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003551 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003552
3553 if (!h1s)
3554 return 0;
3555
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003556 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003557 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003558
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003559 es->events &= ~event_type;
3560 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003561 h1s->subs = NULL;
3562
3563 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003564 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003565
3566 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003567 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003568
Christopher Faulet51dbc942018-09-13 09:05:15 +02003569 return 0;
3570}
3571
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003572/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3573 * event subscriber <es> is not allowed to change from a previous call as long
3574 * as at least one event is still subscribed. The <event_type> must only be a
3575 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
Willy Tarreau000d63c2022-05-27 10:39:17 +02003576 * the stream connector <sc> was already detached, in which case it will return
Willy Tarreau4596fe22022-05-17 19:07:51 +02003577 * -1.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003578 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003579static int h1_subscribe(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 Faulet51bb1852019-09-04 10:22:34 +02003582 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003583
3584 if (!h1s)
3585 return -1;
3586
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003587 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003588 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003589
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003590 es->events |= event_type;
3591 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003592
3593 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003594 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003595
3596
Christopher Faulet6b81df72019-10-01 22:08:43 +02003597 if (event_type & SUB_RETRY_SEND) {
3598 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003599 /*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003600 * If the stconn attempts to subscribe, and the
Christopher Faulet6b81df72019-10-01 22:08:43 +02003601 * mux isn't subscribed to the connection, then it
3602 * probably means the connection wasn't established
3603 * yet, so we have to subscribe.
3604 */
3605 h1c = h1s->h1c;
3606 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3607 h1c->conn->xprt->subscribe(h1c->conn,
3608 h1c->conn->xprt_ctx,
3609 SUB_RETRY_SEND,
3610 &h1c->wait_event);
3611 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003612 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003613}
3614
Christopher Faulet564e39c2021-09-21 15:50:55 +02003615/* Called from the upper layer, to receive data.
3616 *
3617 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3618 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3619 * means the caller wants to flush input data (from the mux buffer and the
3620 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3621 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3622 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3623 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3624 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3625 * copy as much data as possible.
3626 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003627static size_t h1_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003628{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003629 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet539e0292018-11-19 10:40:09 +01003630 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003631 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003632 size_t ret = 0;
3633
Willy Tarreau022e5e52020-09-10 09:33:15 +02003634 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003635
Christopher Faulet4e72b172022-10-04 17:35:19 +02003636 /* Do nothing for now if not RUNNING (implies UPGRADING) */
3637 if (h1c->state < H1_CS_RUNNING) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003638 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3639 goto end;
3640 }
3641
Christopher Faulet539e0292018-11-19 10:40:09 +01003642 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003643 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003644 else
3645 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003646
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003647 if ((flags & CO_RFL_BUF_FLUSH) && se_fl_test(h1s->sd, SE_FL_MAY_SPLICE)) {
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003648 h1c->flags |= H1C_F_WANT_SPLICE;
3649 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 +02003650 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003651 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003652 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 +01003653 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003654 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003655
3656 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003657 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003658 return ret;
3659}
3660
3661
3662/* Called from the upper layer, to send data */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003663static size_t h1_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003664{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003665 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003666 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003667 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003668
3669 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003670 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003671 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003672
Willy Tarreau022e5e52020-09-10 09:33:15 +02003673 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003674
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003675 /* If we're not connected yet, or we're waiting for a handshake, stop
3676 * now, as we don't want to remove everything from the channel buffer
3677 * before we're sure we can send it.
3678 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003679 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003680 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003681 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003682 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003683
Christopher Fauletfc473a62022-10-05 08:22:33 +02003684 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3685 se_fl_set_error(h1s->sd);
Christopher Faulet26a26432021-01-27 11:27:50 +01003686 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 +01003687 return 0;
3688 }
3689
Christopher Faulet46230362019-10-17 16:04:20 +02003690 /* Inherit some flags from the upper layer */
3691 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3692 if (flags & CO_SFL_MSG_MORE)
3693 h1c->flags |= H1C_F_CO_MSG_MORE;
3694 if (flags & CO_SFL_STREAMER)
3695 h1c->flags |= H1C_F_CO_STREAMER;
3696
Christopher Fauletc31872f2019-06-04 22:09:36 +02003697 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003698 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003699
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003700 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003701 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003702 else
3703 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003704
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003705 if (!ret)
Christopher Faulet372b38f2022-07-08 15:20:31 +02003706 break;
3707
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003708 if ((count - ret) > 0)
3709 h1c->flags |= H1C_F_CO_MSG_MORE;
3710
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003711 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003712 count -= ret;
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003713
Olivier Houchard68787ef2020-01-15 19:13:32 +01003714 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003715 break;
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003716
3717 if ((h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)))
3718 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003719 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003720
Christopher Fauletfc473a62022-10-05 08:22:33 +02003721 if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) {
3722 // FIXME: following test was removed :
3723 // ((h1c->conn->flags & CO_FL_ERROR) && (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf)))) {
3724 se_fl_set_error(h1s->sd);
Christopher Faulet26a26432021-01-27 11:27:50 +01003725 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 +01003726 }
3727
Christopher Faulet7a145d62020-08-05 11:31:16 +02003728 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003729 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003730 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003731}
3732
Willy Tarreaue5733232019-05-22 19:24:06 +02003733#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003734/* Send and get, using splicing */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003735static int h1_rcv_pipe(struct stconn *sc, struct pipe *pipe, unsigned int count)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003736{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003737 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003738 struct h1c *h1c = h1s->h1c;
3739 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003740 int ret = 0;
3741
Christopher Faulet897d6122021-12-17 17:28:35 +01003742 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003743
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003744 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003745 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003746 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 +02003747 goto end;
3748 }
3749
Christopher Fauletcf307562021-07-26 10:49:39 +02003750 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003751 if (h1s_data_pending(h1s)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003752 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003753 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003754 }
3755
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003756 if (!h1_recv_allowed(h1c)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003757 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet0060be92020-07-03 15:02:25 +02003758 goto end;
3759 }
3760
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003761 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003762 count = h1m->curr_len;
Christopher Faulet897d6122021-12-17 17:28:35 +01003763 ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003764 if (ret >= 0) {
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003765 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003766 if (ret > h1m->curr_len) {
3767 h1s->flags |= H1S_F_PARSING_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003768 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003769 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003770 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 +01003771 goto end;
3772 }
3773 h1m->curr_len -= ret;
3774 if (!h1m->curr_len) {
3775 h1m->state = H1_MSG_DONE;
3776 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003777 TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003778 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003779 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003780 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
3781 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret);
Christopher Faulete18777b2019-04-16 16:46:36 +02003782 }
3783
Christopher Faulet1be55f92018-10-02 15:59:23 +02003784 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003785 if (conn_xprt_read0_pending(h1c->conn)) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003786 se_fl_set(h1s->sd, SE_FL_EOS);
3787 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_EOS;
Christopher Faulet897d6122021-12-17 17:28:35 +01003788 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003789 }
Christopher Faulet9009c972022-10-05 12:04:56 +02003790 if (h1c->conn->flags & CO_FL_ERROR) {
3791 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003792 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERROR;
Christopher Faulet9009c972022-10-05 12:04:56 +02003793 TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
3794 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003795
Christopher Faulet94d35102021-04-09 11:58:49 +02003796 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003797 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003798 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Faulet94d35102021-04-09 11:58:49 +02003799 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003800 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s);
3801 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet94d35102021-04-09 11:58:49 +02003802 }
Christopher Faulet27182292020-07-03 15:08:49 +02003803 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003804
Christopher Faulet897d6122021-12-17 17:28:35 +01003805 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003806 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003807}
3808
Willy Tarreau000d63c2022-05-27 10:39:17 +02003809static int h1_snd_pipe(struct stconn *sc, struct pipe *pipe)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003810{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003811 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003812 struct h1c *h1c = h1s->h1c;
3813 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003814 int ret = 0;
3815
Christopher Faulet897d6122021-12-17 17:28:35 +01003816 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003817
Christopher Faulet140f1a52021-11-26 16:37:55 +01003818 if (b_data(&h1c->obuf)) {
3819 if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003820 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s);
3821 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003822 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003823 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003824 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003825
Christopher Faulet897d6122021-12-17 17:28:35 +01003826 ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe);
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003827 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003828 if (ret > h1m->curr_len) {
3829 h1s->flags |= H1S_F_PROCESSING_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003830 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003831 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003832 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 +01003833 goto end;
3834 }
3835 h1m->curr_len -= ret;
3836 if (!h1m->curr_len) {
3837 h1m->state = H1_MSG_DONE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003838 TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003839 }
3840 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003841 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
3842 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret);
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003843
3844 end:
Christopher Faulet9009c972022-10-05 12:04:56 +02003845 if (h1c->conn->flags & CO_FL_ERROR) {
Christopher Fauletfc473a62022-10-05 08:22:33 +02003846 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERR_PENDING;
3847 if (h1c->flags & H1C_F_EOS)
3848 h1c->flags |= H1C_F_ERROR;
3849 se_fl_set_error(h1s->sd);
Christopher Faulet9009c972022-10-05 12:04:56 +02003850 TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
3851 }
3852
Christopher Faulet897d6122021-12-17 17:28:35 +01003853 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003854 return ret;
3855}
3856#endif
3857
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003858static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3859{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003860 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003861 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003862
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003863 switch (mux_ctl) {
3864 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003865 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003866 ret |= MUX_STATUS_READY;
3867 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003868 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003869 if (output)
3870 *((int *)output) = h1c->errcode;
3871 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3872 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3873 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3874 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003875 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003876 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003877 default:
3878 return -1;
3879 }
3880}
3881
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003882/* appends some info about connection <h1c> to buffer <msg>, or does nothing if
3883 * <h1c> is NULL. Returns non-zero if the connection is considered suspicious.
3884 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
3885 * not NULL, otherwise a single line is used.
3886 */
3887static int h1_dump_h1c_info(struct buffer *msg, struct h1c *h1c, const char *pfx)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003888{
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003889 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003890
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003891 if (!h1c)
3892 return ret;
3893
Christopher Fauletf376a312019-01-04 15:16:06 +01003894 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3895 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003896 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3897 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003898 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003899 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003900 return ret;
3901}
3902
3903/* appends some info about stream <h1s> to buffer <msg>, or does nothing if
3904 * <h1s> is NULL. Returns non-zero if the stream is considered suspicious. May
3905 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
3906 * NULL, otherwise a single line is used.
3907 */
3908static int h1_dump_h1s_info(struct buffer *msg, const struct h1s *h1s, const char *pfx)
3909{
3910 const char *method;
3911 int ret = 0;
3912
3913 if (!h1s)
3914 return ret;
3915
3916 if (h1s->meth < HTTP_METH_OTHER)
3917 method = http_known_methods[h1s->meth].ptr;
3918 else
3919 method = "UNKNOWN";
3920
3921 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .sd.flg=0x%x .req.state=%s .res.state=%s",
3922 h1s, h1s->flags, se_fl_get(h1s->sd),
3923 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003924
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003925 if (pfx)
3926 chunk_appendf(msg, "\n%s", pfx);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003927
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003928 chunk_appendf(msg, " .meth=%s status=%d",
3929 method, h1s->status);
Christopher Faulet186367f2022-05-30 08:45:15 +02003930
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003931 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(h1s->sd));
3932 if (!se_fl_test(h1s->sd, SE_FL_ORPHAN))
3933 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
3934 h1s_sc(h1s)->flags, h1s_sc(h1s)->app);
Christopher Faulet186367f2022-05-30 08:45:15 +02003935
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003936 if (pfx && h1s->subs)
3937 chunk_appendf(msg, "\n%s", pfx);
3938
3939 chunk_appendf(msg, " .subs=%p", h1s->subs);
3940 if (h1s->subs) {
3941 chunk_appendf(msg, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3942 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
3943 h1s->subs->tasklet->calls,
3944 h1s->subs->tasklet->context);
3945 if (h1s->subs->tasklet->calls >= 1000000)
3946 ret = 1;
3947 resolve_sym_name(msg, NULL, h1s->subs->tasklet->process);
3948 chunk_appendf(msg, ")");
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003949 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003950 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003951}
3952
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003953/* for debugging with CLI's "show fd" command */
3954static int h1_show_fd(struct buffer *msg, struct connection *conn)
3955{
3956 struct h1c *h1c = conn->ctx;
3957 struct h1s *h1s = h1c->h1s;
3958 int ret = 0;
3959
3960 ret |= h1_dump_h1c_info(msg, h1c, NULL);
3961
3962 if (h1s)
3963 ret |= h1_dump_h1s_info(msg, h1s, NULL);
3964
3965 return ret;
3966}
3967
Willy Tarreaue6f389d2022-09-02 16:32:31 +02003968/* for debugging with CLI's "show sess" command. May emit multiple lines, each
3969 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
3970 * line is used. Each field starts with a space so it's safe to print it after
3971 * existing fields.
3972 */
3973static int h1_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
3974{
3975 struct h1s *h1s = sd->se;
3976 int ret = 0;
3977
3978 if (!h1s)
3979 return ret;
3980
3981 ret |= h1_dump_h1s_info(msg, h1s, pfx);
3982 if (pfx)
3983 chunk_appendf(msg, "\n%s", pfx);
3984 chunk_appendf(msg, " h1c=%p", h1s->h1c);
3985 ret |= h1_dump_h1c_info(msg, h1s->h1c, pfx);
3986 return ret;
3987}
3988
Christopher Faulet98fbe952019-07-22 16:18:24 +02003989
3990/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3991static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3992{
3993 struct h1_hdr_entry *entry;
3994
3995 /* Be sure there is a non-empty <to> */
3996 if (!strlen(to)) {
3997 memprintf(err, "expect <to>");
3998 return -1;
3999 }
4000
4001 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004002 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02004003 memprintf(err, "<from> and <to> must not differ execpt the case");
4004 return -1;
4005 }
4006
4007 /* Be sure <from> does not already existsin the tree */
4008 if (ebis_lookup(&hdrs_map.map, from)) {
4009 memprintf(err, "duplicate entry '%s'", from);
4010 return -1;
4011 }
4012
4013 /* Create the entry and insert it in the tree */
4014 entry = malloc(sizeof(*entry));
4015 if (!entry) {
4016 memprintf(err, "out of memory");
4017 return -1;
4018 }
4019
4020 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004021 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01004022 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02004023 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004024 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004025 free(entry);
4026 memprintf(err, "out of memory");
4027 return -1;
4028 }
4029 ebis_insert(&hdrs_map.map, &entry->node);
4030 return 0;
4031}
4032
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004033/* Migrate the the connection to the current thread.
4034 * Return 0 if successful, non-zero otherwise.
4035 * Expected to be called with the old thread lock held.
4036 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004037static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004038{
4039 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004040 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004041
4042 if (fd_takeover(conn->handle.fd, conn) != 0)
4043 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004044
4045 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4046 /* We failed to takeover the xprt, even if the connection may
4047 * still be valid, flag it as error'd, as we have already
4048 * taken over the fd, and wake the tasklet, so that it will
4049 * destroy it.
4050 */
4051 conn->flags |= CO_FL_ERROR;
4052 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
4053 return -1;
4054 }
4055
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004056 if (h1c->wait_event.events)
4057 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
4058 h1c->wait_event.events, &h1c->wait_event);
4059 /* To let the tasklet know it should free itself, and do nothing else,
4060 * set its context to NULL.
4061 */
4062 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004063 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004064
4065 task = h1c->task;
4066 if (task) {
4067 task->context = NULL;
4068 h1c->task = NULL;
4069 __ha_barrier_store();
4070 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004071
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004072 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004073 if (!h1c->task) {
4074 h1_release(h1c);
4075 return -1;
4076 }
4077 h1c->task->process = h1_timeout_task;
4078 h1c->task->context = h1c;
4079 }
4080 h1c->wait_event.tasklet = tasklet_new();
4081 if (!h1c->wait_event.tasklet) {
4082 h1_release(h1c);
4083 return -1;
4084 }
4085 h1c->wait_event.tasklet->process = h1_io_cb;
4086 h1c->wait_event.tasklet->context = h1c;
4087 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
4088 SUB_RETRY_RECV, &h1c->wait_event);
4089
4090 return 0;
4091}
4092
4093
Christopher Faulet98fbe952019-07-22 16:18:24 +02004094static void h1_hdeaders_case_adjust_deinit()
4095{
4096 struct ebpt_node *node, *next;
4097 struct h1_hdr_entry *entry;
4098
4099 node = ebpt_first(&hdrs_map.map);
4100 while (node) {
4101 next = ebpt_next(node);
4102 ebpt_delete(node);
4103 entry = container_of(node, struct h1_hdr_entry, node);
4104 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004105 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004106 free(entry);
4107 node = next;
4108 }
4109 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004110}
4111
Christopher Faulet98fbe952019-07-22 16:18:24 +02004112static int cfg_h1_headers_case_adjust_postparser()
4113{
4114 FILE *file = NULL;
4115 char *c, *key_beg, *key_end, *value_beg, *value_end;
4116 char *err;
4117 int rc, line = 0, err_code = 0;
4118
4119 if (!hdrs_map.name)
4120 goto end;
4121
4122 file = fopen(hdrs_map.name, "r");
4123 if (!file) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004124 ha_alert("h1-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004125 hdrs_map.name);
4126 err_code |= ERR_ALERT | ERR_FATAL;
4127 goto end;
4128 }
4129
4130 /* now parse all lines. The file may contain only two header name per
4131 * line, separated by spaces. All heading and trailing spaces will be
4132 * ignored. Lines starting with a # are ignored.
4133 */
4134 while (fgets(trash.area, trash.size, file) != NULL) {
4135 line++;
4136 c = trash.area;
4137
4138 /* strip leading spaces and tabs */
4139 while (*c == ' ' || *c == '\t')
4140 c++;
4141
4142 /* ignore emptu lines, or lines beginning with a dash */
4143 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
4144 continue;
4145
4146 /* look for the end of the key */
4147 key_beg = c;
4148 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
4149 c++;
4150 key_end = c;
4151
4152 /* strip middle spaces and tabs */
4153 while (*c == ' ' || *c == '\t')
4154 c++;
4155
4156 /* look for the end of the value, it is the end of the line */
4157 value_beg = c;
4158 while (*c && *c != '\n' && *c != '\r')
4159 c++;
4160 value_end = c;
4161
4162 /* trim possibly trailing spaces and tabs */
4163 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
4164 value_end--;
4165
4166 /* set final \0 and check entries */
4167 *key_end = '\0';
4168 *value_end = '\0';
4169
4170 err = NULL;
4171 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
4172 if (rc < 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004173 ha_alert("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004174 hdrs_map.name, err, line);
4175 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004176 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004177 goto end;
4178 }
4179 if (rc > 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004180 ha_warning("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004181 hdrs_map.name, err, line);
4182 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004183 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004184 }
4185 }
4186
4187 end:
4188 if (file)
4189 fclose(file);
4190 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
4191 return err_code;
4192}
4193
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004194/* config parser for global "h1-accept-payload_=-with-any-method" */
4195static int cfg_parse_h1_accept_payload_with_any_method(char **args, int section_type, struct proxy *curpx,
4196 const struct proxy *defpx, const char *file, int line,
4197 char **err)
4198{
4199 if (too_many_args(0, args, err, NULL))
4200 return -1;
4201 accept_payload_with_any_method = 1;
4202 return 0;
4203}
4204
Christopher Faulet98fbe952019-07-22 16:18:24 +02004205
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004206/* config parser for global "h1-header-case-adjust" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004207static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004208 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004209 char **err)
4210{
4211 if (too_many_args(2, args, err, NULL))
4212 return -1;
4213 if (!*(args[1]) || !*(args[2])) {
4214 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
4215 return -1;
4216 }
4217 return add_hdr_case_adjust(args[1], args[2], err);
4218}
4219
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004220/* config parser for global "h1-headers-case-adjust-file" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004221static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004222 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004223 char **err)
4224{
4225 if (too_many_args(1, args, err, NULL))
4226 return -1;
4227 if (!*(args[1])) {
4228 memprintf(err, "'%s' expects <file> as argument.", args[0]);
4229 return -1;
4230 }
4231 free(hdrs_map.name);
4232 hdrs_map.name = strdup(args[1]);
4233 return 0;
4234}
4235
Christopher Faulet98fbe952019-07-22 16:18:24 +02004236/* config keyword parsers */
4237static struct cfg_kw_list cfg_kws = {{ }, {
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004238 { CFG_GLOBAL, "h1-accept-payload-with-any-method", cfg_parse_h1_accept_payload_with_any_method },
Christopher Faulet98fbe952019-07-22 16:18:24 +02004239 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
4240 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
4241 { 0, NULL, NULL },
4242 }
4243};
4244
4245INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4246REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
4247
4248
Christopher Faulet51dbc942018-09-13 09:05:15 +02004249/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05004250/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02004251/****************************************/
4252
4253/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01004254static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02004255 .init = h1_init,
4256 .wake = h1_wake,
4257 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004258 .get_first_sc = h1_get_first_sc,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004259 .detach = h1_detach,
4260 .destroy = h1_destroy,
4261 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01004262 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004263 .rcv_buf = h1_rcv_buf,
4264 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02004265#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02004266 .rcv_pipe = h1_rcv_pipe,
4267 .snd_pipe = h1_snd_pipe,
4268#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02004269 .subscribe = h1_subscribe,
4270 .unsubscribe = h1_unsubscribe,
4271 .shutr = h1_shutr,
4272 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004273 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004274 .show_sd = h1_show_sd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004275 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004276 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02004277 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02004278 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02004279};
4280
Christopher Faulet3f612f72021-02-05 16:44:21 +01004281static const struct mux_ops mux_h1_ops = {
4282 .init = h1_init,
4283 .wake = h1_wake,
4284 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004285 .get_first_sc = h1_get_first_sc,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004286 .detach = h1_detach,
4287 .destroy = h1_destroy,
4288 .avail_streams = h1_avail_streams,
4289 .used_streams = h1_used_streams,
4290 .rcv_buf = h1_rcv_buf,
4291 .snd_buf = h1_snd_buf,
4292#if defined(USE_LINUX_SPLICE)
4293 .rcv_pipe = h1_rcv_pipe,
4294 .snd_pipe = h1_snd_pipe,
4295#endif
4296 .subscribe = h1_subscribe,
4297 .unsubscribe = h1_unsubscribe,
4298 .shutr = h1_shutr,
4299 .shutw = h1_shutw,
4300 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004301 .show_sd = h1_show_sd,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004302 .ctl = h1_ctl,
4303 .takeover = h1_takeover,
4304 .flags = MX_FL_HTX|MX_FL_NO_UPG,
4305 .name = "H1",
4306};
Christopher Faulet51dbc942018-09-13 09:05:15 +02004307
Christopher Faulet3f612f72021-02-05 16:44:21 +01004308/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
4309static struct mux_proto_list mux_proto_h1 =
4310 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
4311static struct mux_proto_list mux_proto_http =
4312 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02004313
Christopher Faulet3f612f72021-02-05 16:44:21 +01004314INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
4315INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01004316
Christopher Faulet51dbc942018-09-13 09:05:15 +02004317/*
4318 * Local variables:
4319 * c-indent-level: 8
4320 * c-basic-offset: 8
4321 * End:
4322 */