blob: 1a6bfb8e112236cb55e5245ada3f31ed758008e7 [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 :
424 * - if an error or a shutdown for reads was detected on the 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
428 * - if an error was detected on the stream we must not attempt to receive
429 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100430 * - if the input buffer failed to be allocated or is full , we must not try
431 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200432 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200433 * - otherwise must may not attempt to receive
434 */
435static inline int h1_recv_allowed(const struct h1c *h1c)
436{
Christopher Faulet71abc0c2022-10-04 17:06:52 +0200437 if (h1c->flags & H1C_F_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100438 TRACE_DEVEL("recv not allowed because of error on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200439 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200440 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200441
Willy Tarreau2febb842020-07-31 09:15:43 +0200442 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
443 TRACE_DEVEL("recv not allowed because of (error|read0|waitl4|waitl6) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletcf56b992018-12-11 16:12:31 +0100444 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200445 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100446
Christopher Faulet7fcbcc02022-10-06 09:24:07 +0200447 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR_MASK)) {
Christopher Faulet119ac872020-09-30 17:33:22 +0200448 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
449 return 0;
450 }
451
Christopher Fauletd17ad822020-09-24 15:14:29 +0200452 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200453 return 1;
454
Christopher Faulet6b81df72019-10-01 22:08:43 +0200455 TRACE_DEVEL("recv not allowed because input is blocked", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456 return 0;
457}
458
459/*
460 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
461 * flags are used to figure what buffer was requested. It returns 1 if the
462 * allocation succeeds, in which case the connection is woken up, or 0 if it's
463 * impossible to wake up and we prefer to be woken up later.
464 */
465static int h1_buf_available(void *target)
466{
467 struct h1c *h1c = target;
468
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100469 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200470 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 +0200471 h1c->flags &= ~H1C_F_IN_ALLOC;
472 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200473 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200474 return 1;
475 }
476
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100477 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200478 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 +0200479 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200480 if (h1c->h1s)
481 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200482 return 1;
483 }
484
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100485 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200486 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
487 h1c->flags &= ~H1C_F_IN_SALLOC;
488 tasklet_wakeup(h1c->wait_event.tasklet);
489 return 1;
490 }
491
Christopher Faulet51dbc942018-09-13 09:05:15 +0200492 return 0;
493}
494
495/*
496 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
497 */
498static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
499{
500 struct buffer *buf = NULL;
501
Willy Tarreau2b718102021-04-21 07:32:39 +0200502 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100503 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200504 h1c->buf_wait.target = h1c;
505 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200506 LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200507 }
508 return buf;
509}
510
511/*
512 * Release a buffer, if any, and try to wake up entities waiting in the buffer
513 * wait queue.
514 */
515static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
516{
517 if (bptr->size) {
518 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100519 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200520 }
521}
522
Christopher Fauletef93be22022-10-04 17:13:32 +0200523/* Returns 1 if the H1 connection is alive (IDLE, EMBRYONIC, RUNNING or
524 * RUNNING). Ortherwise 0 is returned.
525 */
526static inline int h1_is_alive(const struct h1c *h1c)
527{
528 return (h1c->state <= H1_CS_RUNNING);
529}
530
531/* Switch the H1 connection to CLOSING or CLOSED mode, depending on the output
532 * buffer state and if there is still a H1 stream or not. If there are sill
533 * pending outgoing data or if there is still a H1 stream, it is set to CLOSING
534 * state. Otherwise it is set to CLOSED mode. */
535static inline void h1_close(struct h1c *h1c)
536{
537 h1c->state = ((h1c->h1s || b_data(&h1c->obuf)) ? H1_CS_CLOSING : H1_CS_CLOSED);
538}
539
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100540/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet4e72b172022-10-04 17:35:19 +0200541 * or not. We rely on H1C state to know if the connection is in-use or not. It
542 * is IDLE only when no H1 stream is attached and when the previous stream, if
543 * any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100544 */
545static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200546{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100547 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200548
Christopher Faulet4e72b172022-10-04 17:35:19 +0200549 return ((h1c->state == H1_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200550}
551
Willy Tarreau00f18a32019-01-26 12:19:01 +0100552/* returns the number of streams still available on a connection */
553static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100554{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100555 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100556}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200557
Christopher Faulet7a145d62020-08-05 11:31:16 +0200558/* Refresh the h1c task timeout if necessary */
559static void h1_refresh_timeout(struct h1c *h1c)
560{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200561 int is_idle_conn = 0;
562
Christopher Faulet7a145d62020-08-05 11:31:16 +0200563 if (h1c->task) {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200564 if (!h1_is_alive(h1c)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200565 /* half-closed or dead connections : switch to clientfin/serverfin
566 * timeouts so that we don't hang too long on clients that have
567 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200568 */
569 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200570 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 +0200571 is_idle_conn = 1;
Christopher Fauletadcd7892020-10-05 17:13:05 +0200572 }
573 else if (b_data(&h1c->obuf)) {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200574 /* alive connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200575 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200576 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
577 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200578 else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->state != H1_CS_RUNNING)) {
579 /* alive front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200580 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100581 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 +0200582 /* A frontend connection not yet ready could be treated the same way as an idle
583 * one in case of soft-close.
584 */
585 is_idle_conn = 1;
Christopher Faulet7a145d62020-08-05 11:31:16 +0200586 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200587 else {
Willy Tarreau4596fe22022-05-17 19:07:51 +0200588 /* alive back connections of front connections with a stream connector attached */
Christopher Fauletadcd7892020-10-05 17:13:05 +0200589 h1c->task->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200590 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 +0200591 }
592
Christopher Fauletdbe57792020-10-05 17:50:58 +0200593 /* Finally set the idle expiration date if shorter */
594 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200595
596 if ((h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
597 is_idle_conn && tick_isset(global.close_spread_end)) {
598 /* If a soft-stop is in progress and a close-spread-time
599 * is set, we want to spread idle connection closing roughly
600 * evenly across the defined window. This should only
601 * act on idle frontend connections.
602 * If the window end is already in the past, we wake the
603 * timeout task up immediately so that it can be closed.
604 */
605 int remaining_window = tick_remain(now_ms, global.close_spread_end);
606 if (remaining_window) {
607 /* We don't need to reset the expire if it would
608 * already happen before the close window end.
609 */
610 if (tick_is_le(global.close_spread_end, h1c->task->expire)) {
611 /* Set an expire value shorter than the current value
612 * because the close spread window end comes earlier.
613 */
614 h1c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
615 TRACE_DEVEL("connection timeout set to value before close-spread window end", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
616 }
617 }
618 else {
619 /* We are past the soft close window end, wake the timeout
620 * task up immediately.
621 */
622 task_wakeup(h1c->task, TASK_WOKEN_TIMER);
623 }
624 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200625 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
626 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200627 }
628}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200629
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200630static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200631{
632 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
633 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
634 h1c->idle_exp = TICK_ETERNITY;
635 return;
636 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200637 if (h1c->state == H1_CS_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200638 if (!tick_isset(h1c->idle_exp)) {
639 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
640 !b_data(&h1c->ibuf) && /* No input data */
641 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
642 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
643 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
644 }
645 else {
646 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
647 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
648 }
649 }
650 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200651 else if (h1c->state < H1_CS_RUNNING) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200652 if (!tick_isset(h1c->idle_exp)) {
653 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
654 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
655 }
656 }
Christopher Faulet4e72b172022-10-04 17:35:19 +0200657 else {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200658 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200659 TRACE_DEVEL("unset idle expiration (running or closing)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletdbe57792020-10-05 17:50:58 +0200660 }
661}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200662/*****************************************************************/
663/* functions below are dedicated to the mux setup and management */
664/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200665
666/* returns non-zero if there are input data pending for stream h1s. */
667static inline size_t h1s_data_pending(const struct h1s *h1s)
668{
669 const struct h1m *h1m;
670
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200671 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100672 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200673}
674
Willy Tarreau4596fe22022-05-17 19:07:51 +0200675/* Creates a new stream connector and the associate stream. <input> is used as input
Christopher Faulet16df1782020-12-04 16:47:41 +0100676 * buffer for the stream. On success, it is transferred to the stream and the
677 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
678 * mux must still take care of it. However, there is nothing special to do
679 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
Willy Tarreau4596fe22022-05-17 19:07:51 +0200680 * b_free() on it is always safe. This function returns the stream connector on
Christopher Faulet16df1782020-12-04 16:47:41 +0100681 * success or NULL on error. */
Willy Tarreau000d63c2022-05-27 10:39:17 +0200682static struct stconn *h1s_new_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100683{
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100684 struct h1c *h1c = h1s->h1c;
Christopher Faulet47365272018-10-31 17:40:50 +0100685
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100686 TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100687
Christopher Fauletb669d682022-03-22 18:37:19 +0100688 if (h1s->flags & H1S_F_NOT_FIRST)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200689 se_fl_set(h1s->sd, SE_FL_NOT_FIRST);
Christopher Fauletb669d682022-03-22 18:37:19 +0100690 if (h1s->req.flags & H1_MF_UPG_WEBSOCKET)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200691 se_fl_set(h1s->sd, SE_FL_WEBSOCKET);
Christopher Fauletb669d682022-03-22 18:37:19 +0100692
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200693 if (!sc_new_from_endp(h1s->sd, h1c->conn->owner, input)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +0200694 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 +0100695 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200696 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200697
Christopher Faulet4e72b172022-10-04 17:35:19 +0200698 h1c->state = H1_CS_RUNNING;
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100699 TRACE_LEAVE(H1_EV_STRM_NEW, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200700 return h1s_sc(h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100701
Christopher Faulet91449b02022-03-22 18:45:55 +0100702 err:
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100703 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100704 return NULL;
705}
706
Willy Tarreau000d63c2022-05-27 10:39:17 +0200707static struct stconn *h1s_upgrade_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100708{
709 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
710
Willy Tarreaudf1a2fc2022-05-27 11:11:15 +0200711 if (stream_upgrade_from_sc(h1s_sc(h1s), input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100712 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 +0100713 goto err;
714 }
715
Christopher Faulet4e72b172022-10-04 17:35:19 +0200716 h1s->h1c->state = H1_CS_RUNNING;
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100717 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200718 return h1s_sc(h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100719
720 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100721 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100722 return NULL;
723}
724
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200725static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200726{
727 struct h1s *h1s;
728
Christopher Faulet6b81df72019-10-01 22:08:43 +0200729 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
730
Christopher Faulet51dbc942018-09-13 09:05:15 +0200731 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100732 if (!h1s) {
733 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 +0100734 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100735 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200736 h1s->h1c = h1c;
737 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200738 h1s->sess = NULL;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200739 h1s->sd = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100740 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100741 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200742 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100743 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200744
745 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100746 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200747
Christopher Faulet129817b2018-09-20 16:14:40 +0200748 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100749 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200750
751 h1s->status = 0;
752 h1s->meth = HTTP_METH_OTHER;
753
Christopher Faulet47365272018-10-31 17:40:50 +0100754 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
755 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200756 h1s->h1c->state = H1_CS_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100757
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200758 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
759 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200760
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200761 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100762 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200763 return NULL;
764}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100765
Willy Tarreau000d63c2022-05-27 10:39:17 +0200766static struct h1s *h1c_frt_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200767{
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200768 struct h1s *h1s;
769
770 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
771
772 h1s = h1s_new(h1c);
773 if (!h1s)
774 goto fail;
775
Willy Tarreau000d63c2022-05-27 10:39:17 +0200776 if (sc) {
777 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200778 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200779 h1s->sd = sc->sedesc;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100780 }
781 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200782 h1s->sd = sedesc_new();
783 if (!h1s->sd)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100784 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200785 h1s->sd->se = h1s;
786 h1s->sd->conn = h1c->conn;
787 se_fl_set(h1s->sd, SE_FL_T_MUX | SE_FL_ORPHAN);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100788 }
789
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200790 h1s->sess = sess;
791
792 if (h1c->px->options2 & PR_O2_REQBUG_OK)
793 h1s->req.err_pos = -1;
794
Christopher Fauletaf5336f2022-09-12 07:46:11 +0200795 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
796 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
797
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200798 h1c->idle_exp = TICK_ETERNITY;
799 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200800 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200801 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100802
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200803 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100804 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100805 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200806 return NULL;
807}
808
Willy Tarreau000d63c2022-05-27 10:39:17 +0200809static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200810{
811 struct h1s *h1s;
812
813 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
814
815 h1s = h1s_new(h1c);
816 if (!h1s)
817 goto fail;
818
Willy Tarreau000d63c2022-05-27 10:39:17 +0200819 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200820 goto fail;
821
Christopher Faulet10a86702021-04-07 14:18:11 +0200822 h1s->flags |= H1S_F_RX_BLK;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200823 h1s->sd = sc->sedesc;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200824 h1s->sess = sess;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200825
Christopher Faulet4e72b172022-10-04 17:35:19 +0200826 h1c->state = H1_CS_RUNNING;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200827
828 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
829 h1s->res.err_pos = -1;
830
Christopher Faulet60fa0512021-11-26 18:10:39 +0100831 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100832 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100833
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200834 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
835 return h1s;
836
837 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100838 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +0200839 pool_free(pool_head_h1s, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100840 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200841}
842
843static void h1s_destroy(struct h1s *h1s)
844{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200845 if (h1s) {
846 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200847
Christopher Faulet6b81df72019-10-01 22:08:43 +0200848 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200849 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200850
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100851 if (h1s->subs)
852 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200853
Christopher Fauletd17ad822020-09-24 15:14:29 +0200854 h1_release_buf(h1c, &h1s->rxbuf);
855
Christopher Faulet10a86702021-04-07 14:18:11 +0200856 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200857 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
858 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet7fcbcc02022-10-06 09:24:07 +0200859 if (h1s->flags & H1S_F_ERROR_MASK) {
Christopher Faulet71abc0c2022-10-04 17:06:52 +0200860 h1c->flags |= H1C_F_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +0100861 TRACE_ERROR("h1s on error, set error on h1c", H1_EV_H1S_END|H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200862 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100863
Christopher Faulet4e72b172022-10-04 17:35:19 +0200864 if (!(h1c->flags & H1C_F_ERROR) && /* No error */
865 h1_is_alive(h1c) && /* still alive */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100866 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100867 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100868 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200869 h1c->state = H1_CS_IDLE;
870 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100871 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
872 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200873 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +0200874 h1_close(h1c);
875 TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200876 }
Christopher Faulet60fa0512021-11-26 18:10:39 +0100877
878 HA_ATOMIC_DEC(&h1c->px_counters->open_streams);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200879 BUG_ON(h1s->sd && !se_fl_test(h1s->sd, SE_FL_ORPHAN));
880 sedesc_free(h1s->sd);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200881 pool_free(pool_head_h1s, h1s);
882 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200883}
884
885/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200886 * Initialize the mux once it's attached. It is expected that conn->ctx points
Willy Tarreau4596fe22022-05-17 19:07:51 +0200887 * to the existing stream connector (for outgoing connections or for incoming
888 * ones during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200889 * establishment). <input> is always used as Input buffer and may contain
890 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
891 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200892 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200893static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
894 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200895{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200896 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100897 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200898 void *conn_ctx = conn->ctx;
899
900 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200901
902 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100903 if (!h1c) {
904 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200905 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100906 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200907 h1c->conn = conn;
908 h1c->px = proxy;
909
Christopher Fauletef93be22022-10-04 17:13:32 +0200910 h1c->state = H1_CS_IDLE;
Christopher Faulet4e72b172022-10-04 17:35:19 +0200911 h1c->flags = H1C_F_NONE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200912 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200913 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200914 h1c->obuf = BUF_NULL;
915 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200916 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200917
Willy Tarreau90f366b2021-02-20 11:49:49 +0100918 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200919 h1c->wait_event.tasklet = tasklet_new();
920 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200921 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200922 h1c->wait_event.tasklet->process = h1_io_cb;
923 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100924 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200925 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200926
Christopher Faulete9b70722019-04-08 10:46:02 +0200927 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200928 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100929 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
930 if (tick_isset(proxy->timeout.serverfin))
931 h1c->shut_timeout = proxy->timeout.serverfin;
Christopher Faulet563c3452021-11-26 17:37:51 +0100932
933 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be,
934 &h1_stats_module);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100935 } else {
936 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
937 if (tick_isset(proxy->timeout.clientfin))
938 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200939
Christopher Faulet563c3452021-11-26 17:37:51 +0100940 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe,
941 &h1_stats_module);
942
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200943 LIST_APPEND(&mux_stopping_data[tid].list,
944 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100945 }
946 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200947 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100948 if (!t) {
949 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 +0100950 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100951 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100952
953 h1c->task = t;
954 t->process = h1_timeout_task;
955 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200956
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100957 t->expire = tick_add(now_ms, h1c->timeout);
958 }
959
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100960 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200961
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200962 if (h1c->flags & H1C_F_IS_BACK) {
963 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200964 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
965 goto fail;
966 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100967 else if (conn_ctx) {
968 /* Upgraded frontend connection (from TCP) */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100969 if (!h1c_frt_stream_new(h1c, conn_ctx, h1c->conn->owner))
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100970 goto fail;
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100971
Willy Tarreaue68bc612022-05-27 11:23:05 +0200972 /* Attach the SC but Not ready yet */
Christopher Faulet4e72b172022-10-04 17:35:19 +0200973 h1c->state = H1_CS_UPGRADING;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200974 TRACE_DEVEL("Inherit the SC from TCP connection to perform an upgrade",
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100975 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
976 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100977
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200978 if (t) {
979 h1_set_idle_expiration(h1c);
980 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100981 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200982 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100983
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200984 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100985 if (b_data(&h1c->ibuf))
986 tasklet_wakeup(h1c->wait_event.tasklet);
987 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200988 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200989
Christopher Faulet60fa0512021-11-26 18:10:39 +0100990 HA_ATOMIC_INC(&h1c->px_counters->open_conns);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100991 HA_ATOMIC_INC(&h1c->px_counters->total_conns);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100992
Christopher Faulet51dbc942018-09-13 09:05:15 +0200993 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200994 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200995 return 0;
996
997 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200998 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200999 if (h1c->wait_event.tasklet)
1000 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001001 pool_free(pool_head_h1c, h1c);
1002 fail_h1c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +01001003 if (!conn_is_back(conn))
1004 LIST_DEL_INIT(&conn->stopping_list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001005 conn->ctx = conn_ctx; // restore saved context
1006 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001007 return -1;
1008}
1009
Christopher Faulet73c12072019-04-08 11:23:22 +02001010/* release function. This one should be called to free all resources allocated
1011 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +02001012 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001013static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001014{
Christopher Faulet61840e72019-04-15 09:33:32 +02001015 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001016
Christopher Faulet6b81df72019-10-01 22:08:43 +02001017 TRACE_POINT(H1_EV_H1C_END);
1018
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001019 /* The connection must be aattached to this mux to be released */
1020 if (h1c->conn && h1c->conn->ctx == h1c)
1021 conn = h1c->conn;
Christopher Faulet61840e72019-04-15 09:33:32 +02001022
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001023 if (conn && h1c->flags & H1C_F_UPG_H2C) {
1024 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
1025 /* Make sure we're no longer subscribed to anything */
1026 if (h1c->wait_event.events)
1027 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1028 h1c->wait_event.events, &h1c->wait_event);
1029 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
1030 /* connection successfully upgraded to H2, this
1031 * mux was already released */
1032 return;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001033 }
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001034 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
1035 sess_log(conn->owner); /* Log if the upgrade failed */
1036 }
Olivier Houchard45c44372019-06-11 14:06:23 +02001037
Christopher Faulet51dbc942018-09-13 09:05:15 +02001038
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001039 if (LIST_INLIST(&h1c->buf_wait.list))
1040 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001041
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001042 h1_release_buf(h1c, &h1c->ibuf);
1043 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001044
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001045 if (h1c->task) {
1046 h1c->task->context = NULL;
1047 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
1048 h1c->task = NULL;
1049 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001050
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001051 if (h1c->wait_event.tasklet)
1052 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet60fa0512021-11-26 18:10:39 +01001053
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001054 h1s_destroy(h1c->h1s);
1055 if (conn) {
1056 if (h1c->wait_event.events != 0)
1057 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
1058 &h1c->wait_event);
1059 h1_shutw_conn(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001060 }
1061
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001062 HA_ATOMIC_DEC(&h1c->px_counters->open_conns);
1063 pool_free(pool_head_h1c, h1c);
1064
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001065 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001066 if (!conn_is_back(conn))
1067 LIST_DEL_INIT(&conn->stopping_list);
1068
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001069 conn->mux = NULL;
1070 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001071 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001072
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001073 conn_stop_tracking(conn);
1074 conn_full_close(conn);
1075 if (conn->destroy_cb)
1076 conn->destroy_cb(conn);
1077 conn_free(conn);
1078 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001079}
1080
1081/******************************************************/
1082/* functions below are for the H1 protocol processing */
1083/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +02001084/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
1085 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +02001086 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001087static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001088{
Christopher Faulet570d1612018-11-26 11:13:57 +01001089 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001090
Christopher Faulet570d1612018-11-26 11:13:57 +01001091 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 (*(p + 5) > '1' ||
1093 (*(p + 5) == '1' && *(p + 7) >= '1')))
1094 h1m->flags |= H1_MF_VER_11;
1095}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001096
Christopher Faulet9768c262018-10-22 09:34:31 +02001097/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
1098 * greater or equal to 1.1
1099 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001100static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +02001101{
Christopher Faulet570d1612018-11-26 11:13:57 +01001102 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001103
Christopher Faulet570d1612018-11-26 11:13:57 +01001104 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001105 (*(p + 5) > '1' ||
1106 (*(p + 5) == '1' && *(p + 7) >= '1')))
1107 h1m->flags |= H1_MF_VER_11;
1108}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001109
Christopher Fauletf2824e62018-10-01 12:12:37 +02001110/* Deduce the connection mode of the client connection, depending on the
1111 * configuration and the H1 message flags. This function is called twice, the
1112 * first time when the request is parsed and the second time when the response
1113 * is parsed.
1114 */
1115static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
1116{
1117 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001118
1119 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001120 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001121 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001122 h1s->status == 101) {
1123 /* Either we've established an explicit tunnel, or we're
1124 * switching the protocol. In both cases, we're very unlikely to
1125 * understand the next protocols. We have to switch to tunnel
1126 * mode, so that we transfer the request and responses then let
1127 * this protocol pass unmodified. When we later implement
1128 * specific parsers for such protocols, we'll want to check the
1129 * Upgrade header which contains information about that protocol
1130 * for responses with status 101 (eg: see RFC2817 about TLS).
1131 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001132 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001133 TRACE_STATE("set tunnel mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001134 }
1135 else if (h1s->flags & H1S_F_WANT_KAL) {
1136 /* By default the client is in KAL mode. CLOSE mode mean
1137 * it is imposed by the client itself. So only change
1138 * KAL mode here. */
1139 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1140 /* no length known or explicit close => close */
1141 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001142 TRACE_STATE("detect close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001143 }
1144 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1145 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001146 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001147 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001148 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001149 }
1150 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001151 }
1152 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001153 /* Input direction: first pass */
1154 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1155 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001156 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001157 TRACE_STATE("detect close mode (req)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001158 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001159 }
1160
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001161 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001162 * unless a 'close-spread-time' option is set (either to define a
1163 * soft-close window or to disable active closing (close-spread-time
1164 * option set to 0).
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001165 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001166 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001167 int want_clo = 1;
1168 /* If a close-spread-time option is set, we want to avoid
1169 * closing all the active HTTP connections at once so we add a
1170 * random factor that will spread the closing.
1171 */
1172 if (tick_isset(global.close_spread_end)) {
1173 int remaining_window = tick_remain(now_ms, global.close_spread_end);
1174 if (remaining_window) {
1175 /* This should increase the closing rate the further along
1176 * the window we are.
1177 */
1178 want_clo = (remaining_window <= statistical_prng_range(global.close_spread_time));
1179 }
1180 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001181 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
1182 want_clo = 0;
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001183
1184 if (want_clo) {
1185 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1186 TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1187 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001188 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001189}
1190
1191/* Deduce the connection mode of the client connection, depending on the
1192 * configuration and the H1 message flags. This function is called twice, the
1193 * first time when the request is parsed and the second time when the response
1194 * is parsed.
1195 */
1196static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1197{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001198 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001199 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001200 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001201
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001203 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001204 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001205 h1s->status == 101) {
1206 /* Either we've established an explicit tunnel, or we're
1207 * switching the protocol. In both cases, we're very unlikely to
1208 * understand the next protocols. We have to switch to tunnel
1209 * mode, so that we transfer the request and responses then let
1210 * this protocol pass unmodified. When we later implement
1211 * specific parsers for such protocols, we'll want to check the
1212 * Upgrade header which contains information about that protocol
1213 * for responses with status 101 (eg: see RFC2817 about TLS).
1214 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001215 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001216 TRACE_STATE("set tunnel mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001217 }
1218 else if (h1s->flags & H1S_F_WANT_KAL) {
1219 /* By default the server is in KAL mode. CLOSE mode mean
1220 * it is imposed by haproxy itself. So only change KAL
1221 * mode here. */
1222 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1223 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1224 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1225 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001226 TRACE_STATE("detect close mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001227 }
1228 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001229 }
Christopher Faulet70033782018-12-05 13:50:11 +01001230 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001231 /* Output direction: first pass */
1232 if (h1m->flags & H1_MF_CONN_CLO) {
1233 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001234 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001235 TRACE_STATE("detect close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001236 }
1237 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1238 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1239 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1240 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1241 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1242 /* no explicit keep-alive option httpclose/server-close => close */
1243 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001244 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 +01001245 }
Christopher Faulet70033782018-12-05 13:50:11 +01001246 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001247
1248 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001249 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001250 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001251 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);
1252 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001253}
1254
Christopher Fauletb992af02019-03-28 15:42:24 +01001255static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001256{
1257 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001258
1259 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1260 * token is found
1261 */
1262 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001263 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001264
1265 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001266 if (!(h1m->flags & H1_MF_VER_11)) {
1267 TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001268 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001269 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001270 }
1271 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001272 if (h1m->flags & H1_MF_VER_11) {
1273 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001274 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001275 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001276 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001277}
1278
Christopher Fauletb992af02019-03-28 15:42:24 +01001279static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001280{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001281 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1282 * token is found
1283 */
1284 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001285 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001286
1287 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001288 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001289 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1290 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 +01001291 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001292 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001293 }
1294 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001295 if (h1m->flags & H1_MF_VER_11) {
1296 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001297 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001298 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001299 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001300}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001301
Christopher Fauletb992af02019-03-28 15:42:24 +01001302static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001303{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001304 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001305 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001306 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001307 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001308}
1309
Christopher Fauletb992af02019-03-28 15:42:24 +01001310static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1311{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001312 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001313 h1_set_cli_conn_mode(h1s, h1m);
1314 else
1315 h1_set_srv_conn_mode(h1s, h1m);
1316
1317 if (!(h1m->flags & H1_MF_RESP))
1318 h1_update_req_conn_value(h1s, h1m, conn_val);
1319 else
1320 h1_update_res_conn_value(h1s, h1m, conn_val);
1321}
Christopher Faulete44769b2018-11-29 23:01:45 +01001322
Christopher Faulet98fbe952019-07-22 16:18:24 +02001323/* Try to adjust the case of the message header name using the global map
1324 * <hdrs_map>.
1325 */
1326static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1327{
1328 struct ebpt_node *node;
1329 struct h1_hdr_entry *entry;
1330
1331 /* No entry in the map, do nothing */
1332 if (eb_is_empty(&hdrs_map.map))
1333 return;
1334
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001335 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001336 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1337 return;
1338
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001339 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001340 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1341 return;
1342
1343 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1344 if (!node)
1345 return;
1346 entry = container_of(node, struct h1_hdr_entry, node);
1347 name->ptr = entry->name.ptr;
1348 name->len = entry->name.len;
1349}
1350
Christopher Faulete44769b2018-11-29 23:01:45 +01001351/* Append the description of what is present in error snapshot <es> into <out>.
1352 * The description must be small enough to always fit in a buffer. The output
1353 * buffer may be the trash so the trash must not be used inside this function.
1354 */
1355static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1356{
1357 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001358 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1359 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1360 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1361 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1362 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1363 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001364}
1365/*
1366 * Capture a bad request or response and archive it in the proxy's structure.
1367 * By default it tries to report the error position as h1m->err_pos. However if
1368 * this one is not set, it will then report h1m->next, which is the last known
1369 * parsing point. The function is able to deal with wrapping buffers. It always
1370 * displays buffers as a contiguous area starting at buf->p. The direction is
1371 * determined thanks to the h1m's flags.
1372 */
1373static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1374 struct h1m *h1m, struct buffer *buf)
1375{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001376 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001377 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001378 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001379 union error_snapshot_ctx ctx;
1380
Christopher Faulet4e72b172022-10-04 17:35:19 +02001381 if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001382 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02001383 sess = __sc_strm(h1s_sc(h1s))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001384 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02001385 other_end = __sc_strm(h1s_sc(h1s))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001386 else
1387 other_end = sess->fe;
1388 } else
1389 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001390
1391 /* http-specific part now */
1392 ctx.h1.state = h1m->state;
1393 ctx.h1.c_flags = h1c->flags;
1394 ctx.h1.s_flags = h1s->flags;
1395 ctx.h1.m_flags = h1m->flags;
1396 ctx.h1.m_clen = h1m->curr_len;
1397 ctx.h1.m_blen = h1m->body_len;
1398
1399 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1400 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001401 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1402 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001403}
1404
Christopher Fauletadb22202018-12-12 10:32:09 +01001405/* Emit the chunksize followed by a CRLF in front of data of the buffer
1406 * <buf>. It goes backwards and starts with the byte before the buffer's
1407 * head. The caller is responsible for ensuring there is enough room left before
1408 * the buffer's head for the string.
1409 */
1410static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1411{
1412 char *beg, *end;
1413
1414 beg = end = b_head(buf);
1415 *--beg = '\n';
1416 *--beg = '\r';
1417 do {
1418 *--beg = hextab[chksz & 0xF];
1419 } while (chksz >>= 4);
1420 buf->head -= (end - beg);
1421 b_add(buf, end - beg);
1422}
1423
1424/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1425 * ensuring there is enough room left in the buffer for the string. */
1426static void h1_emit_chunk_crlf(struct buffer *buf)
1427{
1428 *(b_peek(buf, b_data(buf))) = '\r';
1429 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1430 b_add(buf, 2);
1431}
1432
Christopher Faulet129817b2018-09-20 16:14:40 +02001433/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001434 * Switch the stream to tunnel mode. This function must only be called on 2xx
1435 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001436 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001437static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001438{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001439 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001440
Christopher Faulet5be651d2021-01-22 15:28:03 +01001441 h1s->req.state = H1_MSG_TUNNEL;
1442 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001443
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001444 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001445 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001446
Christopher Faulet5be651d2021-01-22 15:28:03 +01001447 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 +01001448
Christopher Faulet10a86702021-04-07 14:18:11 +02001449 if (h1s->flags & H1S_F_RX_BLK) {
1450 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001451 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001452 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 +02001453 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001454 if (h1s->flags & H1S_F_TX_BLK) {
1455 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001456 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001457 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 +01001458 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001459}
1460
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001461/* Search for a websocket key header. The message should have been identified
1462 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001463 *
1464 * On the request side, if found the key is stored in the session. It might be
1465 * needed to calculate response key if the server side is using http/2.
1466 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001467 * On the response side, the key might be verified if haproxy has been
1468 * responsible for the generation of a key. This happens when a h2 client is
1469 * interfaced with a h1 server.
1470 *
1471 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001472 */
1473static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1474{
1475 struct htx_blk *blk;
1476 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001477 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001478 int ws_key_found = 0, idx;
1479
1480 idx = htx_get_head(htx); // returns the SL that we skip
1481 while ((idx = htx_get_next(htx, idx)) != -1) {
1482 blk = htx_get_blk(htx, idx);
1483 type = htx_get_blk_type(blk);
1484
1485 if (type == HTX_BLK_UNUSED)
1486 continue;
1487
1488 if (type != HTX_BLK_HDR)
1489 break;
1490
1491 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001492 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001493
Amaury Denoyellec1938232020-12-11 17:53:03 +01001494 /* Websocket key is base64 encoded of 16 bytes */
1495 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001496 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001497 /* Copy the key on request side
1498 * we might need it if the server is using h2 and does
1499 * not provide the response
1500 */
1501 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001502 ws_key_found = 1;
1503 break;
1504 }
1505 else if (isteqi(n, ist("sec-websocket-accept")) &&
1506 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001507 /* Need to verify the response key if the input was
1508 * generated by haproxy
1509 */
1510 if (h1s->ws_key[0]) {
1511 char key[29];
1512 h1_calculate_ws_output_key(h1s->ws_key, key);
1513 if (!isteqi(ist(key), v))
1514 break;
1515 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001516 ws_key_found = 1;
1517 break;
1518 }
1519 }
1520
1521 /* missing websocket key, reject the message */
1522 if (!ws_key_found) {
1523 htx->flags |= HTX_FL_PARSING_ERROR;
1524 return 0;
1525 }
1526
1527 return 1;
1528}
1529
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001530/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001531 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001532 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001533 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1534 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001535 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001536static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1537 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001538{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001539 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001540 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001541
Willy Tarreau022e5e52020-09-10 09:33:15 +02001542 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 +02001543
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001544 if (h1s->meth == HTTP_METH_CONNECT)
1545 h1m->flags |= H1_MF_METH_CONNECT;
1546 if (h1s->meth == HTTP_METH_HEAD)
1547 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001548
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001549 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001550 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001551 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 +02001552 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001553 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001554 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 +02001555 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1556 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001557 else if (ret == -2) {
1558 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);
1559 h1s->flags |= H1S_F_RX_CONGESTED;
1560 }
1561 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001562 goto end;
1563 }
1564
Christopher Faulete136bd12021-09-21 18:44:55 +02001565
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001566 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload except if
1567 * accept_payload_with_any_method global option is set.
1568 *There is a payload if the c-l is not null or the the payload is
1569 * chunk-encoded. A parsing error is reported but a A
1570 * 413-Payload-Too-Large is returned instead of a 400-Bad-Request.
Christopher Faulete136bd12021-09-21 18:44:55 +02001571 */
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001572 if (!accept_payload_with_any_method &&
1573 !(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
Christopher Faulete136bd12021-09-21 18:44:55 +02001574 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1575 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1576 h1s->flags |= H1S_F_PARSING_ERROR;
1577 htx->flags |= HTX_FL_PARSING_ERROR;
1578 h1s->h1c->errcode = 413;
1579 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);
1580 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1581 ret = 0;
1582 goto end;
1583 }
1584
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001585 /* Reject any message with an unknown transfer-encoding. In fact if any
1586 * encoding other than "chunked". A 422-Unprocessable-Content is
1587 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1588 * response.
1589 */
1590 if (h1m->flags & H1_MF_TE_OTHER) {
1591 h1s->flags |= H1S_F_PARSING_ERROR;
1592 htx->flags |= HTX_FL_PARSING_ERROR;
1593 if (!(h1m->flags & H1_MF_RESP))
1594 h1s->h1c->errcode = 422;
1595 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1596 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1597 ret = 0;
1598 goto end;
1599 }
1600
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001601 /* If websocket handshake, search for the websocket key */
1602 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1603 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1604 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1605 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001606 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001607 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 +01001608 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1609
1610 ret = 0;
1611 goto end;
1612 }
1613 }
1614
Christopher Faulet486498c2019-10-11 14:22:00 +02001615 if (h1m->err_pos >= 0) {
1616 /* Maybe we found an error during the parsing while we were
1617 * configured not to block on that, so we have to capture it
1618 * now.
1619 */
1620 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1621 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1622 }
1623
Christopher Faulet5696f542020-12-02 16:08:38 +01001624 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001625 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001626 if (h1s->meth == HTTP_METH_HEAD)
1627 h1s->flags |= H1S_F_BODYLESS_RESP;
1628 }
1629 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001630 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001631 if (h1s->status == 204 || h1s->status == 304)
1632 h1s->flags |= H1S_F_BODYLESS_RESP;
1633 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001634 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001635 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001636
Christopher Faulet129817b2018-09-20 16:14:40 +02001637 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001638 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 +02001639 return ret;
1640}
1641
1642/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001643 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001644 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1645 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001646 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001647static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1648 struct buffer *buf, size_t *ofs, size_t max,
1649 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001650{
Christopher Fauletde471a42021-02-01 16:37:28 +01001651 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001652
Willy Tarreau022e5e52020-09-10 09:33:15 +02001653 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 +02001654 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001655 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001656 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 +01001657 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001658 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001659 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 +02001660 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001661 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001662 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001663 }
1664
Christopher Faulet02a02532019-11-15 09:36:28 +01001665 *ofs += ret;
1666
1667 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001668 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1669 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);
1670 h1s->flags |= H1S_F_RX_CONGESTED;
1671 }
1672
Willy Tarreau022e5e52020-09-10 09:33:15 +02001673 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 +02001674 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001675}
1676
1677/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001678 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1679 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1680 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001681 * responsible to update the parser state <h1m>. If more room is requested,
1682 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001683 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001684static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1685 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001686{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001687 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001688
Willy Tarreau022e5e52020-09-10 09:33:15 +02001689 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 +02001690 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001691 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001692 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 +02001693 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001694 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001695 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 +02001696 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1697 }
Christopher Fauletae660be2022-04-13 17:48:54 +02001698 else if (ret == -2) {
Christopher Faulet46e058d2021-09-20 07:47:27 +02001699 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);
1700 h1s->flags |= H1S_F_RX_CONGESTED;
1701 }
1702 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001703 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001704 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001705
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001706 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001707
1708 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001709 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 +02001710 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001711}
1712
1713/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001714 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001715 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1716 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001717 *
1718 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001719 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001720static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001721{
Christopher Faulet539e0292018-11-19 10:40:09 +01001722 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001723 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001724 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001725 size_t data;
1726 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001727 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001728
Christopher Faulet539e0292018-11-19 10:40:09 +01001729 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001730 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001731
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001732 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001733 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001734
Christopher Faulet2eed8002020-12-07 11:26:13 +01001735 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001736 goto end;
1737
Christopher Faulet10a86702021-04-07 14:18:11 +02001738 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001739 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001740
Christopher Faulet46e058d2021-09-20 07:47:27 +02001741 /* Always remove congestion flags and try to process more input data */
1742 h1s->flags &= ~H1S_F_RX_CONGESTED;
1743
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001744 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001745 size_t used = htx_used_space(htx);
1746
Christopher Faulet129817b2018-09-20 16:14:40 +02001747 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001748 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001749 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001750 if (!ret)
1751 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001752
1753 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1754 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1755
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001756 if ((h1m->flags & H1_MF_RESP) &&
1757 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1758 h1m_init_res(&h1s->res);
1759 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001760 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001761 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001762 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001763 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001764 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001765 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001766 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001767 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001768
1769 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1770 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001771 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001772 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001773 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001774 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001775 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001776 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001777
Christopher Faulet76014fd2019-12-10 11:47:22 +01001778 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1779 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001780 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001781 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001782 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1783 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001784
Christopher Faulet5be651d2021-01-22 15:28:03 +01001785 if ((h1m->flags & H1_MF_RESP) &&
1786 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1787 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001788 else {
1789 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1790 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001791 h1s->flags |= H1S_F_RX_BLK;
1792 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001793 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001794 if (h1s->flags & H1S_F_TX_BLK) {
1795 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001796 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001797 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 +01001798 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001799 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001800 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001801 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001802 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001803 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001804 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001805 if (!ret)
1806 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001807
1808 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1809 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001810 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001811 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001812 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001813 break;
1814 }
1815
Christopher Faulet30db3d72019-05-17 15:35:33 +02001816 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001817 } 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 +01001818
Christopher Faulet129817b2018-09-20 16:14:40 +02001819
Christopher Faulet2eed8002020-12-07 11:26:13 +01001820 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001821 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 +02001822 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001823 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001824
Christopher Faulet539e0292018-11-19 10:40:09 +01001825 b_del(&h1c->ibuf, total);
1826
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001827 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1828
Christopher Faulet30db3d72019-05-17 15:35:33 +02001829 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001830 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001831 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001832 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 +01001833 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001834 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001835
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001836 if (!b_data(&h1c->ibuf))
1837 h1_release_buf(h1c, &h1c->ibuf);
1838
Christopher Faulet2177d962022-10-05 08:39:14 +02001839 if (h1m->state <= H1_MSG_LAST_LF)
1840 goto out;
1841
Christopher Faulet4e72b172022-10-04 17:35:19 +02001842 if (h1c->state < H1_CS_RUNNING) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001843 /* The H1 connection is not ready. Most of time, there is no SC
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001844 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1845 * cases, it is only possible on the client side.
1846 */
1847 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1848
Christopher Faulet4e72b172022-10-04 17:35:19 +02001849 if (h1c->state == H1_CS_EMBRYONIC) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001850 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 +02001851 BUG_ON(h1s_sc(h1s));
Willy Tarreau000d63c2022-05-27 10:39:17 +02001852 if (!h1s_new_sc(h1s, buf)) {
Christopher Faulet71abc0c2022-10-04 17:06:52 +02001853 h1c->flags |= H1C_F_ERROR;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001854 goto err;
1855 }
1856 }
1857 else {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001858 TRACE_DEVEL("request headers fully parsed, upgrade the inherited SC", H1_EV_RX_DATA, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001859 BUG_ON(h1s_sc(h1s) == NULL);
Willy Tarreau000d63c2022-05-27 10:39:17 +02001860 if (!h1s_upgrade_sc(h1s, buf)) {
Christopher Faulet71abc0c2022-10-04 17:06:52 +02001861 h1c->flags |= H1C_F_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001862 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001863 goto err;
1864 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001865 }
1866 }
1867
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001868 /* Here h1s_sc(h1s) is always defined */
Christopher Fauletf5ce3202021-11-26 17:26:19 +01001869 if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) {
Christopher Fauleta583af62020-09-24 15:35:37 +02001870 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 +02001871 se_fl_set(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001872 }
1873 else {
1874 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 +02001875 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001876 }
1877
Willy Tarreau4596fe22022-05-17 19:07:51 +02001878 /* Set EOI on stream connector in DONE state iff:
Christopher Fauleta22782b2021-02-08 17:18:01 +01001879 * - it is a response
1880 * - it is a request but no a protocol upgrade nor a CONNECT
1881 *
1882 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001883 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001884 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001885 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1886 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001887 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauleta583af62020-09-24 15:35:37 +02001888
Christopher Fauletec4207c2021-04-08 18:34:30 +02001889 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001890 /* When Input data are pending for this message, notify upper layer that
1891 * the mux need more space in the HTX buffer to continue if :
1892 *
1893 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1894 * - Headers or trailers are pending to be copied.
1895 */
1896 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001897 se_fl_set(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001898 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1899 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001900 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001901 se_fl_clr(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001902 if (h1s->flags & H1S_F_REOS) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001903 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Faulet10498562022-10-10 18:05:25 +02001904 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 +01001905 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
Willy Tarreau4596fe22022-05-17 19:07:51 +02001906 * EOI on the stream connector */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001907 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001908 }
Christopher Faulet10498562022-10-10 18:05:25 +02001909 else if (h1m->state < H1_MSG_DONE) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001910 se_fl_set(h1s->sd, SE_FL_ERROR);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001911 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 +01001912 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001913
Christopher Faulet10a86702021-04-07 14:18:11 +02001914 if (h1s->flags & H1S_F_TX_BLK) {
1915 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001916 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001917 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 +01001918 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001919 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001920 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001921
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001922 end:
Christopher Faulet5966e402022-07-08 09:02:59 +02001923 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001924 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001925 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001926
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001927 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001928 htx_to_buf(htx, buf);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001929 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001930 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001931 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001932}
1933
Christopher Faulet129817b2018-09-20 16:14:40 +02001934/*
1935 * Process outgoing data. It parses data and transfer them from the channel buffer into
1936 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1937 * 0 if it couldn't proceed.
1938 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001939static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001940{
Christopher Faulet129817b2018-09-20 16:14:40 +02001941 struct h1s *h1s = h1c->h1s;
1942 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001943 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001944 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001945 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001946 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001947 int last_data = 0;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001948 int ws_key_found = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001949
Christopher Faulet94b2c762019-05-24 15:28:57 +02001950 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001951 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1952
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001953 if (htx_is_empty(chn_htx))
1954 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001955
Christopher Faulet10a86702021-04-07 14:18:11 +02001956 if (h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01001957 goto end;
1958
Christopher Faulet51dbc942018-09-13 09:05:15 +02001959 if (!h1_get_buf(h1c, &h1c->obuf)) {
1960 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001961 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 +02001962 goto end;
1963 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001964
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001965 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001966
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001967 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001968 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001969
Willy Tarreau3815b222018-12-11 19:50:43 +01001970 /* Perform some optimizations to reduce the number of buffer copies.
1971 * First, if the mux's buffer is empty and the htx area contains
1972 * exactly one data block of the same size as the requested count,
1973 * then it's possible to simply swap the caller's buffer with the
1974 * mux's output buffer and adjust offsets and length to match the
1975 * entire DATA HTX block in the middle. In this case we perform a
1976 * true zero-copy operation from end-to-end. This is the situation
1977 * that happens all the time with large files. Second, if this is not
1978 * possible, but the mux's output buffer is empty, we still have an
1979 * opportunity to avoid the copy to the intermediary buffer, by making
1980 * the intermediary buffer's area point to the output buffer's area.
1981 * In this case we want to skip the HTX header to make sure that copies
1982 * remain aligned and that this operation remains possible all the
1983 * time. This goes for headers, data blocks and any data extracted from
1984 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001985 */
1986 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001987 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01001988 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01001989 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001990 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001991 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001992 void *old_area;
1993
Christopher Faulet6b81df72019-10-01 22:08:43 +02001994 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 +01001995 if (h1m->state == H1_MSG_DATA) {
1996 if (h1m->flags & H1_MF_CLEN) {
1997 if (count > h1m->curr_len) {
1998 TRACE_ERROR("too much payload, more than announced",
1999 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2000 goto error;
2001 }
2002 h1m->curr_len -= count;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002003 if (!h1m->curr_len)
2004 last_data = 1;
Christopher Faulet140f1a52021-11-26 16:37:55 +01002005 }
2006 if (chn_htx->flags & HTX_FL_EOM) {
2007 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2008 last_data = 1;
2009 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002010 }
2011
Christopher Faulete5596bf2020-12-02 16:13:22 +01002012 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01002013 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02002014 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01002015 h1c->obuf.data = count;
2016
2017 buf->area = old_area;
2018 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01002019
Christopher Faulet6b81df72019-10-01 22:08:43 +02002020 chn_htx = (struct htx *)buf->area;
2021 htx_reset(chn_htx);
2022
Christopher Fauletadb22202018-12-12 10:32:09 +01002023 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002024 * size and eventually the last chunk. We have at least
2025 * the size of the struct htx to write the chunk
2026 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01002027 */
2028 if (h1m->flags & H1_MF_CHNK) {
2029 h1_emit_chunk_size(&h1c->obuf, count);
2030 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002031 if (last_data) {
2032 /* Emit the last chunk too at the buffer's end */
2033 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
2034 }
Christopher Fauletadb22202018-12-12 10:32:09 +01002035 }
2036
Christopher Faulet6b81df72019-10-01 22:08:43 +02002037 if (h1m->state == H1_MSG_DATA)
2038 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002039 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002040 else
2041 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002042 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002043
Christopher Faulete5596bf2020-12-02 16:13:22 +01002044 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002045 if (last_data) {
2046 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02002047 if (h1s->flags & H1S_F_RX_BLK) {
2048 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002049 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002050 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 +01002051 }
2052
2053 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2054 H1_EV_TX_DATA, h1c->conn, h1s);
2055 }
Willy Tarreau3815b222018-12-11 19:50:43 +01002056 goto out;
2057 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002058 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002059 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002060 else
2061 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02002062
Christopher Fauletc2518a52019-06-25 21:41:02 +02002063 tmp.data = 0;
2064 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02002065 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01002066 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02002067 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01002068 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02002069 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02002070 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02002071
Christopher Fauletb2e84162018-12-06 11:39:49 +01002072 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002073 if (type != HTX_BLK_DATA && vlen > count)
2074 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002075
Christopher Faulet94b2c762019-05-24 15:28:57 +02002076 if (type == HTX_BLK_UNUSED)
2077 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02002078
Christopher Faulet94b2c762019-05-24 15:28:57 +02002079 switch (h1m->state) {
2080 case H1_MSG_RQBEFORE:
2081 if (type != HTX_BLK_REQ_SL)
2082 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002083 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 +02002084 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002085 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02002086 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002087 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002088 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002089 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002090 if (sl->flags & HTX_SL_F_BODYLESS)
2091 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02002092 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01002093 if (h1s->meth == HTTP_METH_HEAD)
2094 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02002095 if (h1s->flags & H1S_F_RX_BLK) {
2096 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002097 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002098 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 +02002099 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002100 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02002101
Christopher Faulet94b2c762019-05-24 15:28:57 +02002102 case H1_MSG_RPBEFORE:
2103 if (type != HTX_BLK_RES_SL)
2104 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002105 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 +02002106 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002107 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02002108 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002109 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002110 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01002111 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002112 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01002113 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02002114 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01002115 else if (h1s->status == 204 || h1s->status == 304)
2116 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02002117 h1m->state = H1_MSG_HDR_FIRST;
2118 break;
2119
Christopher Faulet94b2c762019-05-24 15:28:57 +02002120 case H1_MSG_HDR_FIRST:
2121 case H1_MSG_HDR_NAME:
2122 case H1_MSG_HDR_L2_LWS:
2123 if (type == HTX_BLK_EOH)
2124 goto last_lf;
2125 if (type != HTX_BLK_HDR)
2126 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002127
Christopher Faulet9768c262018-10-22 09:34:31 +02002128 h1m->state = H1_MSG_HDR_NAME;
2129 n = htx_get_blk_name(chn_htx, blk);
2130 v = htx_get_blk_value(chn_htx, blk);
2131
Christopher Faulet86d144c2019-08-14 16:32:25 +02002132 /* Skip all pseudo-headers */
2133 if (*(n.ptr) == ':')
2134 goto skip_hdr;
2135
Christopher Faulet91fcf212020-12-02 16:17:15 +01002136 if (isteq(n, ist("transfer-encoding"))) {
2137 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2138 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02002139 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01002140 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002141 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01002142 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2143 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01002144 /* Only skip C-L header with invalid value. */
2145 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01002146 goto skip_hdr;
2147 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002148 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01002149 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02002150 if (!v.len)
2151 goto skip_hdr;
2152 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01002153 else if (isteq(n, ist("upgrade"))) {
2154 h1_parse_upgrade_header(h1m, v);
2155 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002156 else if ((isteq(n, ist("sec-websocket-accept")) &&
2157 h1m->flags & H1_MF_RESP) ||
2158 (isteq(n, ist("sec-websocket-key")) &&
2159 !(h1m->flags & H1_MF_RESP))) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002160 ws_key_found = 1;
2161 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02002162 else if (isteq(n, ist("te"))) {
2163 /* "te" may only be sent with "trailers" if this value
2164 * is present, otherwise it must be deleted.
2165 */
2166 v = istist(v, ist("trailers"));
2167 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2168 goto skip_hdr;
2169 v = ist("trailers");
2170 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002171
Christopher Faulet67d58092019-10-02 10:51:38 +02002172 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002173 if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) &&
2174 isteqi(n, h1c->px->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02002175 goto skip_hdr;
2176
Christopher Faulet98fbe952019-07-22 16:18:24 +02002177 /* Try to adjust the case of the header name */
2178 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2179 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002180 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002181 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002182 skip_hdr:
2183 h1m->state = H1_MSG_HDR_L2_LWS;
2184 break;
2185
Christopher Faulet94b2c762019-05-24 15:28:57 +02002186 case H1_MSG_LAST_LF:
2187 if (type != HTX_BLK_EOH)
2188 goto error;
2189 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002190 h1m->state = H1_MSG_LAST_LF;
2191 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002192 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002193 /* If the reply comes from haproxy while the request is
2194 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002195 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2196 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2197 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002198 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2199 /* T-E + C-L: force close */
2200 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2201 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2202 }
2203 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2204 /* T-E + HTTP/1.0: force close */
2205 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2206 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2207 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002208
2209 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002210 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002211 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002212 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002213 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002214 /* Try to adjust the case of the header name */
2215 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2216 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002217 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002218 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002219 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002220 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002221 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002222
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002223 if ((h1s->meth != HTTP_METH_CONNECT &&
2224 (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 +01002225 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002226 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002227 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002228 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2229 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002230 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002231 n = ist("transfer-encoding");
2232 v = ist("chunked");
2233 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2234 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002235 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002236 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002237 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002238 h1m->flags |= H1_MF_CHNK;
2239 }
2240
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002241 /* Now add the server name to a header (if requested) */
2242 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002243 !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002244 struct server *srv = objt_server(h1c->conn->target);
2245
2246 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002247 n = h1c->px->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002248 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002249
2250 /* Try to adjust the case of the header name */
2251 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2252 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002253 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002254 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002255 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002256 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002257 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2258 }
2259
Amaury Denoyellec1938232020-12-11 17:53:03 +01002260 /* Add websocket handshake key if needed */
2261 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) &&
2262 !ws_key_found) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002263 if (!(h1m->flags & H1_MF_RESP)) {
2264 /* generate a random websocket key
2265 * stored in the session to
2266 * verify it on the response side
2267 */
2268 h1_generate_random_ws_input_key(h1s->ws_key);
2269
2270 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2271 ist(h1s->ws_key),
2272 &tmp)) {
2273 goto full;
2274 }
2275 }
2276 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002277 /* add the response header key */
2278 char key[29];
2279 h1_calculate_ws_output_key(h1s->ws_key, key);
2280 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2281 ist(key),
2282 &tmp)) {
2283 goto full;
2284 }
2285 }
2286 }
2287
Christopher Faulet6b81df72019-10-01 22:08:43 +02002288 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2289 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2290
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002291 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002292 if (!chunk_memcat(&tmp, "\r\n", 2))
2293 goto full;
2294 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002295 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002296 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002297 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002298 if (!chunk_memcat(&tmp, "\r\n", 2))
2299 goto full;
2300 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002301 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002302 else if ((h1m->flags & H1_MF_RESP) &&
2303 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002304 if (!chunk_memcat(&tmp, "\r\n", 2))
2305 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002306 h1m_init_res(&h1s->res);
2307 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002308 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002309 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002310 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002311 else {
Christopher Faulet2eb52432022-04-07 10:29:38 +02002312 /* EOM flag is set or empty payload (C-L to 0) and it is the last block */
2313 if (htx_is_unique_blk(chn_htx, blk) &&
2314 ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) {
Mickael Torres226082d2022-11-16 14:29:37 +01002315 if ((h1m->flags & H1_MF_CHNK) && !(h1s->flags & H1S_F_BODYLESS_RESP)) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002316 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2317 goto full;
2318 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002319 else if (!chunk_memcat(&tmp, "\r\n", 2))
2320 goto full;
2321 goto done;
2322 }
2323 else if (!chunk_memcat(&tmp, "\r\n", 2))
2324 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002325 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002326 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002327 break;
2328
Christopher Faulet94b2c762019-05-24 15:28:57 +02002329 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002330 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002331 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002332 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2333 goto trailers;
2334
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002335 /* If the message is not chunked, never
2336 * add the last chunk. */
2337 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002338 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002339 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 +02002340 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002341 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002342 else if (type != HTX_BLK_DATA)
2343 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002344
2345 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 +02002346
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002347 /* It is the last block of this message. After this one,
2348 * only tunneled data may be forwarded. */
2349 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2350 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2351 last_data = 1;
2352 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002353
2354 if (vlen > count) {
2355 /* Get the maximum amount of data we can xferred */
2356 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002357 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002358 }
2359
Christopher Faulet140f1a52021-11-26 16:37:55 +01002360 if (h1m->state == H1_MSG_DATA) {
2361 if (h1m->flags & H1_MF_CLEN) {
2362 if (vlen > h1m->curr_len) {
2363 TRACE_ERROR("too much payload, more than announced",
2364 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2365 goto error;
2366 }
Christopher Faulet140f1a52021-11-26 16:37:55 +01002367 }
2368 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2369 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2370 goto skip_data;
2371 }
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002372 }
2373
Christopher Fauletd9233f02019-10-14 14:01:24 +02002374 chklen = 0;
2375 if (h1m->flags & H1_MF_CHNK) {
2376 chklen = b_room(&tmp);
2377 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2378 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2379 (chklen < 1048576) ? 5 : 8);
2380 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002381
2382 /* If it is the end of the chunked message (without EOT), reserve the
2383 * last chunk size */
2384 if (last_data)
2385 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002386 }
2387
2388 if (vlen + chklen > b_room(&tmp)) {
2389 /* too large for the buffer */
2390 if (chklen >= b_room(&tmp))
2391 goto full;
2392 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002393 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002394 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002395 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002396 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002397 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002398 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002399
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002400 /* Space already reserved, so it must succeed */
2401 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2402 goto error;
2403
Christopher Faulet6b81df72019-10-01 22:08:43 +02002404 if (h1m->state == H1_MSG_DATA)
2405 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002406 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002407 else
2408 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002409 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002410
2411 skip_data:
Christopher Faulet2eb52432022-04-07 10:29:38 +02002412 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Fauletb4eca0e2022-01-10 17:27:51 +01002413 h1m->curr_len -= vlen;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002414 if (!h1m->curr_len)
2415 last_data = 1;
2416 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002417 if (last_data)
2418 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002419 break;
2420
Christopher Faulet94b2c762019-05-24 15:28:57 +02002421 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002422 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002423 goto error;
2424 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002425 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002426
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002427 if (!(h1m->flags & H1_MF_CHNK))
2428 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002429
Christopher Faulete5596bf2020-12-02 16:13:22 +01002430 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2431 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 +01002432 if (type == HTX_BLK_EOT)
2433 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002434 break;
2435 }
2436
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002437 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002438 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002439 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002440 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2441 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002442 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002443 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002444 else { // HTX_BLK_TLR
2445 n = htx_get_blk_name(chn_htx, blk);
2446 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002447
2448 /* Try to adjust the case of the header name */
2449 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2450 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002451 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002452 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002453 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002454 break;
2455
Christopher Faulet94b2c762019-05-24 15:28:57 +02002456 case H1_MSG_DONE:
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002457 /* If the message is not chunked, ignore
2458 * trailers. It may happen with H2 messages. */
2459 if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && !(h1m->flags & H1_MF_CHNK))
2460 break;
2461
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002462 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2463 goto error; /* For now return an error */
2464
Christopher Faulet94b2c762019-05-24 15:28:57 +02002465 done:
2466 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002467 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002468 h1s->flags |= H1S_F_TX_BLK;
2469 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002470 }
2471 else if ((h1m->flags & H1_MF_RESP) &&
2472 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2473 /* a successful reply to a CONNECT or a protocol switching is sent
2474 * to the client. Switch the response to tunnel mode.
2475 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002476 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002477 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletf3158e92019-11-15 11:14:23 +01002478 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002479
Christopher Faulet10a86702021-04-07 14:18:11 +02002480 if (h1s->flags & H1S_F_RX_BLK) {
2481 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002482 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002483 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 +02002484 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002485
2486 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2487 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002488 break;
2489
Christopher Faulet9768c262018-10-22 09:34:31 +02002490 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002491 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002492 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002493 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002494 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Faulet71abc0c2022-10-04 17:06:52 +02002495 h1c->flags |= H1C_F_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002496 TRACE_ERROR("processing output error, set error on h1c/h1s",
2497 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 +01002498 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02002499 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002500
2501 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002502 total += vlen;
2503 count -= vlen;
2504 if (sz == vlen)
2505 blk = htx_remove_blk(chn_htx, blk);
2506 else {
2507 htx_cut_data_blk(chn_htx, blk, vlen);
2508 break;
2509 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002510 }
2511
Christopher Faulet9768c262018-10-22 09:34:31 +02002512 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002513 /* when the output buffer is empty, tmp shares the same area so that we
2514 * only have to update pointers and lengths.
2515 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002516 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2517 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002518 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002519 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002520
Willy Tarreau3815b222018-12-11 19:50:43 +01002521 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002522 out:
2523 if (!buf_room_for_htx_data(&h1c->obuf)) {
2524 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002525 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002526 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002527 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002528 /* Both the request and the response reached the DONE state. So set EOI
Willy Tarreau4596fe22022-05-17 19:07:51 +02002529 * flag on the stream connector. Most of time, the flag will already be set,
Christopher Fauletdea24742021-01-22 15:12:30 +01002530 * except for protocol upgrades. Report an error if data remains blocked
2531 * in the output buffer.
2532 */
2533 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2534 if (!htx_is_empty(chn_htx)) {
Christopher Faulet71abc0c2022-10-04 17:06:52 +02002535 h1c->flags |= H1C_F_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002536 TRACE_ERROR("txn done but data waiting to be sent, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002537 }
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02002538 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletdea24742021-01-22 15:12:30 +01002539 }
2540
Christopher Faulet6b81df72019-10-01 22:08:43 +02002541 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002542 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002543
2544 full:
2545 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2546 h1c->flags |= H1C_F_OUT_FULL;
2547 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002548}
2549
Christopher Faulet51dbc942018-09-13 09:05:15 +02002550/*********************************************************/
2551/* functions below are I/O callbacks from the connection */
2552/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002553static void h1_wake_stream_for_recv(struct h1s *h1s)
2554{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002555 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002556 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002557 tasklet_wakeup(h1s->subs->tasklet);
2558 h1s->subs->events &= ~SUB_RETRY_RECV;
2559 if (!h1s->subs->events)
2560 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002561 }
2562}
2563static void h1_wake_stream_for_send(struct h1s *h1s)
2564{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002565 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002566 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002567 tasklet_wakeup(h1s->subs->tasklet);
2568 h1s->subs->events &= ~SUB_RETRY_SEND;
2569 if (!h1s->subs->events)
2570 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002571 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002572}
2573
Christopher Fauletad4daf62021-01-21 17:49:01 +01002574/* alerts the data layer following this sequence :
2575 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2576 * - if its subscribed to send, then it's woken up for send
2577 * - if it was subscribed to neither, its ->wake() callback is called
2578 */
2579static void h1_alert(struct h1s *h1s)
2580{
2581 if (h1s->subs) {
2582 h1_wake_stream_for_recv(h1s);
2583 h1_wake_stream_for_send(h1s);
2584 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002585 else if (h1s_sc(h1s) && h1s_sc(h1s)->app_ops->wake != NULL) {
Christopher Fauletad4daf62021-01-21 17:49:01 +01002586 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002587 h1s_sc(h1s)->app_ops->wake(h1s_sc(h1s));
Christopher Fauletad4daf62021-01-21 17:49:01 +01002588 }
2589}
2590
Christopher Fauletc18fc232020-10-06 17:41:36 +02002591/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2592 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2593 * retryable errors (allocation error or buffer full). On success, the error is
2594 * copied in the output buffer.
2595*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002596static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002597{
2598 int rc = http_get_status_idx(h1c->errcode);
2599 int ret = 0;
2600
2601 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2602
2603 /* Verify if the error is mapped on /dev/null or any empty file */
2604 /// XXX: do a function !
2605 if (h1c->px->replies[rc] &&
2606 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2607 h1c->px->replies[rc]->body.errmsg &&
2608 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2609 /* Empty error, so claim a success */
2610 ret = 1;
2611 goto out;
2612 }
2613
2614 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2615 h1c->flags |= H1C_F_ERR_PENDING;
2616 goto out;
2617 }
2618
2619 if (!h1_get_buf(h1c, &h1c->obuf)) {
2620 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2621 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2622 goto out;
2623 }
Tim Duesterhus77508502022-03-15 13:11:06 +01002624 ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc]));
Christopher Fauletc18fc232020-10-06 17:41:36 +02002625 if (unlikely(ret <= 0)) {
2626 if (!ret) {
2627 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2628 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2629 goto out;
2630 }
2631 else {
2632 /* we cannot report this error, so claim a success */
2633 ret = 1;
2634 }
2635 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02002636
2637 if (h1c->h1s) {
2638 TRACE_DEVEL("Abort embryonic H1S", H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2639 h1s_destroy(h1c->h1s);
2640 }
2641
Christopher Fauletc18fc232020-10-06 17:41:36 +02002642 h1c->flags &= ~H1C_F_ERR_PENDING;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002643 h1c->state = H1_CS_CLOSING;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002644 out:
2645 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2646 return ret;
2647}
2648
2649/* Try to send a 500 internal error. It relies on h1_send_error to send the
2650 * error. This function takes care of incrementing stats and tracked counters.
2651 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002652static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002653{
2654 struct session *sess = h1c->conn->owner;
2655 int ret = 1;
2656
2657 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002658 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002659 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2660 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002661 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002662 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002663
2664 h1c->errcode = 500;
2665 ret = h1_send_error(h1c);
2666 sess_log(sess);
2667 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002668}
2669
Christopher Fauletb3230f72021-09-21 18:38:20 +02002670/* Try to send an error because of a parsing error. By default a 400 bad request
2671 * error is returned. But the status code may be specified by setting
2672 * h1c->errcode. It relies on h1_send_error to send the error. This function
2673 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002674 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002675static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002676{
2677 struct session *sess = h1c->conn->owner;
2678 int ret = 1;
2679
Christopher Faulet4e72b172022-10-04 17:35:19 +02002680 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
2681 h1c->state = H1_CS_CLOSING;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002682 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002683 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002684
2685 session_inc_http_req_ctr(sess);
2686 session_inc_http_err_ctr(sess);
2687 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002688 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2689 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002690 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002691 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002692
Christopher Fauletb3230f72021-09-21 18:38:20 +02002693 if (!h1c->errcode)
2694 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002695 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002696 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2697 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002698
2699 end:
2700 return ret;
2701}
2702
Christopher Faulet2eed8002020-12-07 11:26:13 +01002703/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2704 * the error. This function takes care of incrementing stats and tracked
2705 * counters.
2706 */
2707static int h1_handle_not_impl_err(struct h1c *h1c)
2708{
2709 struct session *sess = h1c->conn->owner;
2710 int ret = 1;
2711
Christopher Faulet4e72b172022-10-04 17:35:19 +02002712 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
2713 h1c->state = H1_CS_CLOSING;
Christopher Faulet2eed8002020-12-07 11:26:13 +01002714 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002715 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002716
2717 session_inc_http_req_ctr(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002718 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002719 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2720 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002721 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002722 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002723
2724 h1c->errcode = 501;
2725 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002726 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2727 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002728
2729 end:
2730 return ret;
2731}
2732
Christopher Fauletc18fc232020-10-06 17:41:36 +02002733/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2734 * error. This function takes care of incrementing stats and tracked counters.
2735 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002736static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002737{
2738 struct session *sess = h1c->conn->owner;
2739 int ret = 1;
2740
Christopher Faulet4e72b172022-10-04 17:35:19 +02002741 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) {
2742 h1c->state = H1_CS_CLOSING;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002743 goto end;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002744 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002745
2746 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002747 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002748 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2749 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002750 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002751 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002752
2753 h1c->errcode = 408;
Christopher Faulet07e10de2021-07-26 09:42:49 +02002754 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2755 ret = h1_send_error(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002756 sess_log(sess);
2757 end:
2758 return ret;
2759}
2760
2761
Christopher Faulet51dbc942018-09-13 09:05:15 +02002762/*
2763 * Attempt to read data, and subscribe if none available
2764 */
2765static int h1_recv(struct h1c *h1c)
2766{
2767 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002768 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002769 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002770
Christopher Faulet6b81df72019-10-01 22:08:43 +02002771 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2772
2773 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2774 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002775 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002776 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002777
Christopher Fauletae635762020-09-21 11:47:16 +02002778 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2779 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002780 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002781 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002782
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002783 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2784 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002785 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002786 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002787 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002788
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002789 /*
2790 * If we only have a small amount of data, realign it,
2791 * it's probably cheaper than doing 2 recv() calls.
2792 */
2793 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002794 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002795
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002796 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002797 if (!h1c->h1s ||
2798 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2799 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002800 flags |= CO_RFL_READ_ONCE;
2801
Willy Tarreau45f2b892018-12-05 07:59:27 +01002802 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002803 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002804 if (h1c->flags & H1C_F_IN_FULL) {
2805 h1c->flags &= ~H1C_F_IN_FULL;
2806 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2807 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002808
2809 if (!b_data(&h1c->ibuf)) {
2810 /* try to pre-align the buffer like the rxbufs will be
2811 * to optimize memory copies.
2812 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002813 h1c->ibuf.head = sizeof(struct htx);
2814 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002815 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet41951ab2021-11-26 18:12:51 +01002816 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002817 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002818 if (max && !ret && h1_recv_allowed(h1c)) {
2819 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2820 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002821 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002822 else {
2823 h1_wake_stream_for_recv(h1c->h1s);
2824 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002825 }
2826
Christopher Faulet51dbc942018-09-13 09:05:15 +02002827 if (!b_data(&h1c->ibuf))
2828 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002829 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002830 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002831 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2832 }
2833
2834 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002835 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002836}
2837
2838
2839/*
2840 * Try to send data if possible
2841 */
2842static int h1_send(struct h1c *h1c)
2843{
2844 struct connection *conn = h1c->conn;
2845 unsigned int flags = 0;
2846 size_t ret;
2847 int sent = 0;
2848
Christopher Faulet6b81df72019-10-01 22:08:43 +02002849 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2850
2851 if (conn->flags & CO_FL_ERROR) {
2852 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002853 b_reset(&h1c->obuf);
2854 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002855 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002856
2857 if (!b_data(&h1c->obuf))
2858 goto end;
2859
Christopher Faulet46230362019-10-17 16:04:20 +02002860 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002861 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002862 if (h1c->flags & H1C_F_CO_STREAMER)
2863 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002864
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002865 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002866 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002867 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002868 if (h1c->flags & H1C_F_OUT_FULL) {
2869 h1c->flags &= ~H1C_F_OUT_FULL;
2870 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2871 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01002872 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002873 b_del(&h1c->obuf, ret);
2874 sent = 1;
2875 }
2876
Christopher Faulet145aa472018-12-06 10:56:20 +01002877 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002878 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002879 /* error or output closed, nothing to send, clear the buffer to release it */
2880 b_reset(&h1c->obuf);
2881 }
2882
Christopher Faulet51dbc942018-09-13 09:05:15 +02002883 end:
Christopher Fauletc17c31c2022-02-01 18:25:06 +01002884 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002885 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002886
Christopher Faulet51dbc942018-09-13 09:05:15 +02002887 /* We're done, no more to send */
2888 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002889 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002890 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002891 if (h1c->state == H1_CS_CLOSING) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002892 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002893 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002894 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002895 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002896 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2897 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002898 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002899 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002900
Christopher Faulet6b81df72019-10-01 22:08:43 +02002901 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002902 return sent || (h1c->state == H1_CS_CLOSED);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002903}
2904
Christopher Faulet51dbc942018-09-13 09:05:15 +02002905/* callback called on any event by the connection handler.
2906 * It applies changes and returns zero, or < 0 if it wants immediate
2907 * destruction of the connection.
2908 */
2909static int h1_process(struct h1c * h1c)
2910{
2911 struct connection *conn = h1c->conn;
2912
Christopher Faulet6b81df72019-10-01 22:08:43 +02002913 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2914
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002915 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002916 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Faulet4e72b172022-10-04 17:35:19 +02002917 (h1c->state < H1_CS_RUNNING) && /* IDLE, EMBRYONIC or UPGRADING */
2918 !(h1c->flags & (H1C_F_IN_SALLOC|H1C_F_ERROR))) { /* No allocation failure on the stream rxbuf and no ERROR on the H1C */
2919 struct h1s *h1s = h1c->h1s;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002920 struct buffer *buf;
2921 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002922
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002923 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2924 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002925 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002926
2927 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002928 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002929 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2930 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002931 /* Try to match H2 preface before parsing the request headers. */
2932 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2933 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet4e72b172022-10-04 17:35:19 +02002934 if (h1c->state == H1_CS_UPGRADING) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02002935 /* Force the REOS here to be sure to release the SC.
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002936 Here ATTACHED implies !READY, and h1s defined
2937 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02002938 BUG_ON(!h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002939 h1s->flags |= H1S_F_REOS;
2940 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002941 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002942 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002943 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002944 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002945
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002946 /* Create the H1 stream if not already there */
2947 if (!h1s) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002948 h1s = h1c_frt_stream_new(h1c, NULL, h1c->conn->owner);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002949 if (!h1s) {
2950 b_reset(&h1c->ibuf);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002951 h1c->flags |= H1C_F_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002952 goto no_parsing;
2953 }
2954 }
2955
2956 if (h1s->sess->t_idle == -1)
2957 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2958
2959 /* Get the stream rxbuf */
2960 buf = h1_get_buf(h1c, &h1s->rxbuf);
2961 if (!buf) {
2962 h1c->flags |= H1C_F_IN_SALLOC;
2963 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2964 return 0;
2965 }
2966
2967 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01002968 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002969 h1_release_buf(h1c, &h1s->rxbuf);
2970 h1_set_idle_expiration(h1c);
2971
2972 no_parsing:
Christopher Faulet71abc0c2022-10-04 17:06:52 +02002973 if (h1c->flags & H1C_F_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002974 h1_handle_internal_err(h1c);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002975 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
Christopher Faulet26a26432021-01-27 11:27:50 +01002976 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002977 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002978 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2979 h1_handle_not_impl_err(h1c);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002980 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002981 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002982 }
Christopher Faulet10498562022-10-10 18:05:25 +02002983 else if (h1s->flags & H1S_F_PARSING_ERROR || se_fl_test(h1s->sd, SE_FL_ERROR)) {
2984 h1_handle_parsing_error(h1c);
Christopher Faulet4e72b172022-10-04 17:35:19 +02002985 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ERROR;
Christopher Faulet10498562022-10-10 18:05:25 +02002986 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
2987 }
Christopher Faulet2177d962022-10-05 08:39:14 +02002988 else if (h1c->state < H1_CS_RUNNING) {
2989 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
2990 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2991 }
Christopher Fauletae635762020-09-21 11:47:16 +02002992 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002993 h1_send(h1c);
2994
Christopher Faulet75308302021-11-15 14:51:37 +01002995 /* H1 connection must be released ASAP if:
2996 * - an error occurred on the connection or the H1C or
2997 * - a read0 was received or
2998 * - a silent shutdown was emitted and all outgoing data sent
2999 */
3000 if ((conn->flags & CO_FL_ERROR) ||
3001 conn_xprt_read0_pending(conn) ||
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003002 (h1c->flags & H1C_F_ERROR) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003003 (h1c->state >= H1_CS_CLOSING && (h1c->flags & H1C_F_SILENT_SHUT) && !b_data(&h1c->obuf))) {
3004 if (h1c->state != H1_CS_RUNNING) {
Willy Tarreau4596fe22022-05-17 19:07:51 +02003005 /* No stream connector or not ready */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003006 if (h1c->state < H1_CS_RUNNING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ERROR))) {
3007 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Fauletb3230f72021-09-21 18:38:20 +02003008 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003009 h1_send(h1c);
Christopher Faulet4e72b172022-10-04 17:35:19 +02003010 h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003011 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02003012 else if (h1c->flags & H1C_F_ERR_PENDING) {
3013 /* Handle pending error, if any (only possible on frontend connection) */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003014 BUG_ON(h1c->flags & H1C_F_IS_BACK);
3015 if (h1_send_error(h1c))
3016 h1_send(h1c);
3017 }
Christopher Faulet4e72b172022-10-04 17:35:19 +02003018 else {
3019 h1_close(h1c);
3020 TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn);
3021 }
Christopher Fauletae635762020-09-21 11:47:16 +02003022
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003023 /* If there is some pending outgoing data or error, just wait */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003024 if (h1c->state == H1_CS_CLOSING || (h1c->flags & H1C_F_ERR_PENDING))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003025 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01003026
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003027 /* Otherwise we can release the H1 connection */
3028 goto release;
3029 }
3030 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003031 struct h1s *h1s = h1c->h1s;
3032
Willy Tarreau4596fe22022-05-17 19:07:51 +02003033 /* Here there is still a H1 stream with a stream connector.
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003034 * Report the connection state at the stream level
3035 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003036 BUG_ON(!h1s);
3037
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003038 if (conn_xprt_read0_pending(conn)) {
3039 h1s->flags |= H1S_F_REOS;
3040 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
3041 }
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003042 if ((h1c->flags & H1C_F_ERROR) || ((conn->flags & CO_FL_ERROR) &&
Willy Tarreaucad42a72022-08-29 10:22:56 +02003043 (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf))))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003044 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003045 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01003046 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003047 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003048 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003049
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003050 if (!b_data(&h1c->ibuf))
3051 h1_release_buf(h1c, &h1c->ibuf);
3052
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003053 /* Check if a soft-stop is in progress.
3054 * Release idling front connection if this is the case.
3055 */
3056 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003057 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
William Dauchya9dd9012022-01-05 22:53:24 +01003058 if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) &&
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003059 h1c->flags & H1C_F_WAIT_NEXT_REQ) {
3060
3061 int send_close = 1;
3062 /* If a close-spread-time option is set, we want to avoid
3063 * closing all the active HTTP2 connections at once so we add a
3064 * random factor that will spread the closing.
3065 */
3066 if (tick_isset(global.close_spread_end)) {
3067 int remaining_window = tick_remain(now_ms, global.close_spread_end);
3068 if (remaining_window) {
3069 /* This should increase the closing rate the
3070 * further along the window we are.
3071 */
3072 send_close = (remaining_window <= statistical_prng_range(global.close_spread_time));
3073 }
3074 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02003075 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
3076 send_close = 0; /* let the client close his connection himself */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003077 if (send_close)
3078 goto release;
3079 }
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003080 }
3081 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003082
Christopher Faulet4e72b172022-10-04 17:35:19 +02003083 if (h1c->state == H1_CS_RUNNING && (h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1c->h1s)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003084 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 +02003085 h1_wake_stream_for_recv(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003086 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003087
Christopher Faulet47365272018-10-31 17:40:50 +01003088 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02003089 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003090 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003091 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01003092
3093 release:
Christopher Faulet4e72b172022-10-04 17:35:19 +02003094 if (h1c->state == H1_CS_UPGRADING) {
3095 struct h1s *h1s = h1c->h1s;
3096
3097 /* Don't release the H1 connection right now, we must destroy
3098 * the attached SC first */
3099 BUG_ON(!h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003100
3101 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003102 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003103 if ((h1c->flags & H1C_F_ERROR) || (conn->flags & CO_FL_ERROR))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003104 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003105 h1_alert(h1s);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003106 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003107 }
3108 else {
3109 h1_release(h1c);
3110 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
3111 }
Christopher Faulet539e0292018-11-19 10:40:09 +01003112 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003113}
3114
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003115struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003116{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003117 struct connection *conn;
3118 struct tasklet *tl = (struct tasklet *)t;
3119 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003120 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003121 int ret = 0;
3122
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003123 if (state & TASK_F_USR1) {
3124 /* the tasklet was idling on an idle connection, it might have
3125 * been stolen, let's be careful!
3126 */
3127 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3128 if (tl->context == NULL) {
3129 /* The connection has been taken over by another thread,
3130 * we're no longer responsible for it, so just free the
3131 * tasklet, and do nothing.
3132 */
3133 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3134 tasklet_free(tl);
3135 return NULL;
3136 }
3137 conn = h1c->conn;
3138 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003139
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003140 /* Remove the connection from the list, to be sure nobody attempts
3141 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003142 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003143 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3144 if (conn_in_list)
3145 conn_delete_from_tree(&conn->hash_node->node);
3146
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003147 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003148 } else {
3149 /* we're certain the connection was not in an idle list */
3150 conn = h1c->conn;
3151 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
3152 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003153 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003154
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003155 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003156 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003157 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003158 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003159 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003160 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01003161
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003162 /* If we were in an idle list, we want to add it back into it,
3163 * unless h1_process() returned -1, which mean it has destroyed
3164 * the connection (testing !ret is enough, if h1_process() wasn't
3165 * called then ret will be 0 anyway.
3166 */
Willy Tarreau74163142021-03-13 11:30:19 +01003167 if (ret < 0)
3168 t = NULL;
3169
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003170 if (!ret && conn_in_list) {
3171 struct server *srv = objt_server(conn->target);
3172
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003173 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003174 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02003175 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003176 else
Willy Tarreau85223482022-09-29 20:32:43 +02003177 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003178 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003179 }
Willy Tarreau74163142021-03-13 11:30:19 +01003180 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003181}
3182
Christopher Faulet51dbc942018-09-13 09:05:15 +02003183static int h1_wake(struct connection *conn)
3184{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003185 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01003186 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003187
Christopher Faulet6b81df72019-10-01 22:08:43 +02003188 TRACE_POINT(H1_EV_H1C_WAKE, conn);
3189
Christopher Faulet539e0292018-11-19 10:40:09 +01003190 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01003191 ret = h1_process(h1c);
3192 if (ret == 0) {
3193 struct h1s *h1s = h1c->h1s;
3194
Christopher Faulet4e72b172022-10-04 17:35:19 +02003195 if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING)
Christopher Fauletad4daf62021-01-21 17:49:01 +01003196 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003197 }
3198 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003199}
3200
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003201/* Connection timeout management. The principle is that if there's no receipt
3202 * nor sending for a certain amount of time, the connection is closed.
3203 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003204struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003205{
3206 struct h1c *h1c = context;
3207 int expired = tick_is_expired(t->expire, now_ms);
3208
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003209 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003210
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003211 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003212 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003213 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003214
3215 /* Somebody already stole the connection from us, so we should not
3216 * free it, we just have to free the task.
3217 */
3218 if (!t->context) {
3219 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003220 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003221 goto do_leave;
3222 }
3223
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003224 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003225 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003226 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003227 return t;
3228 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003229
Willy Tarreau4596fe22022-05-17 19:07:51 +02003230 /* If a stream connector is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003231 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003232 */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003233 if (h1c->state == H1_CS_RUNNING) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003234 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003235 t->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +02003236 TRACE_DEVEL("leaving (SC still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003237 return t;
3238 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003239
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003240 /* Try to send an error to the client */
Christopher Faulet4e72b172022-10-04 17:35:19 +02003241 if (h1c->state != H1_CS_CLOSING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ERROR|H1C_F_ERR_PENDING))) {
3242 h1c->flags |= H1C_F_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003243 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003244 if (h1_handle_req_tout(h1c))
3245 h1_send(h1c);
3246 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
3247 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003248 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003249 return t;
3250 }
3251 }
3252
Christopher Faulet4e72b172022-10-04 17:35:19 +02003253 if (h1c->state == H1_CS_UPGRADING) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003254 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet4e72b172022-10-04 17:35:19 +02003255 * attached SC first. */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003256 se_fl_set(h1c->h1s->sd, SE_FL_EOS | SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003257 h1_alert(h1c->h1s);
3258 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003259 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003260 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003261 return t;
3262 }
3263
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003264 /* We're about to destroy the connection, so make sure nobody attempts
3265 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003266 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003267 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003268 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003269
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003270 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003271 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003272
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003273 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003274 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003275
3276 if (!h1c) {
3277 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003278 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003279 return NULL;
3280 }
3281
3282 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003283 h1_release(h1c);
3284 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003285 return NULL;
3286}
3287
Christopher Faulet51dbc942018-09-13 09:05:15 +02003288/*******************************************/
3289/* functions below are used by the streams */
3290/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003291
Christopher Faulet51dbc942018-09-13 09:05:15 +02003292/*
3293 * Attach a new stream to a connection
3294 * (Used for outgoing connections)
3295 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003296static int h1_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003297{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003298 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003299 struct h1s *h1s;
3300
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003301 /* this connection is no more idle (if it was at all) */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003302 h1c->flags &= ~H1C_F_SILENT_SHUT;
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003303
Christopher Faulet6b81df72019-10-01 22:08:43 +02003304 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003305 if (h1c->flags & H1C_F_ERROR) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003306 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3307 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003308 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003309
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003310 h1s = h1c_bck_stream_new(h1c, sd->sc, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003311 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003312 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3313 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003314 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003315
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003316 /* the connection is not idle anymore, let's mark this */
3317 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003318 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003319
Christopher Faulet6b81df72019-10-01 22:08:43 +02003320 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulete00ad352021-12-16 14:44:31 +01003321 return 0;
Christopher Faulet26a26432021-01-27 11:27:50 +01003322 err:
Christopher Faulet26a26432021-01-27 11:27:50 +01003323 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 +01003324 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003325}
3326
Willy Tarreau4596fe22022-05-17 19:07:51 +02003327/* Retrieves a valid stream connector from this connection, or returns NULL.
3328 * For this mux, it's easy as we can only store a single stream connector.
Christopher Faulet51dbc942018-09-13 09:05:15 +02003329 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003330static struct stconn *h1_get_first_sc(const struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003331{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003332 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003333 struct h1s *h1s = h1c->h1s;
3334
3335 if (h1s)
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02003336 return h1s_sc(h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003337
3338 return NULL;
3339}
3340
Christopher Faulet73c12072019-04-08 11:23:22 +02003341static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003342{
Christopher Faulet73c12072019-04-08 11:23:22 +02003343 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003344
Christopher Faulet6b81df72019-10-01 22:08:43 +02003345 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet7c452cc2022-04-14 11:08:26 +02003346 if (!h1c->h1s || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003347 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003348}
3349
3350/*
3351 * Detach the stream from the connection and possibly release the connection.
3352 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003353static void h1_detach(struct sedesc *sd)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003354{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003355 struct h1s *h1s = sd->se;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003356 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003357 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003358 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003359
Christopher Faulet6b81df72019-10-01 22:08:43 +02003360 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3361
Christopher Faulet6b81df72019-10-01 22:08:43 +02003362 if (!h1s) {
3363 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003364 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003365 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003366
Olivier Houchardf502aca2018-12-14 19:42:40 +01003367 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003368 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003369
Christopher Faulet42849b02020-10-05 11:35:17 +02003370 sess->accept_date = date;
3371 sess->tv_accept = now;
3372 sess->t_handshake = 0;
3373 sess->t_idle = -1;
3374
Olivier Houchard8a786902018-12-15 16:05:40 +01003375 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3376 h1s_destroy(h1s);
3377
Christopher Faulet4e72b172022-10-04 17:35:19 +02003378 if (h1c->state == H1_CS_IDLE && (h1c->flags & H1C_F_IS_BACK)) {
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003379 /* this connection may be killed at any moment, we want it to
3380 * die "cleanly" (i.e. only an RST).
3381 */
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003382 h1c->flags |= H1C_F_SILENT_SHUT;
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003383
Christopher Fauletf1204b82019-07-19 14:51:06 +02003384 /* If there are any excess server data in the input buffer,
3385 * release it and close the connection ASAP (some data may
3386 * remain in the output buffer). This happens if a server sends
3387 * invalid responses. So in such case, we don't want to reuse
3388 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003389 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003390 if (b_data(&h1c->ibuf)) {
3391 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet4e72b172022-10-04 17:35:19 +02003392 h1c->state = H1_CS_CLOSING;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003393 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003394 goto release;
3395 }
Christopher Faulet03627242019-07-19 11:34:08 +02003396
Christopher Faulet08016ab2020-07-01 16:10:06 +02003397 if (h1c->conn->flags & CO_FL_PRIVATE) {
3398 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003399 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3400 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003401 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003402 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003403 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003404 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003405 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003406 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003407 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3408 goto end;
3409 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003410 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003411 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003412 if (h1c->conn->owner == sess)
3413 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003414
3415 /* mark that the tasklet may lose its context to another thread and
3416 * that the handler needs to check it under the idle conns lock.
3417 */
3418 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003419 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003420 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3421
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003422 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003423 /* The server doesn't want it, let's kill the connection right away */
3424 h1c->conn->mux->destroy(h1c);
3425 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3426 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003427 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003428 /* At this point, the connection has been added to the
3429 * server idle list, so another thread may already have
3430 * hijacked it, so we can't do anything with it.
3431 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003432 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003433 }
3434 }
3435
Christopher Fauletf1204b82019-07-19 14:51:06 +02003436 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003437 /* 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 +02003438 if ((h1c->flags & H1C_F_ERROR) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003439 (h1c->state == H1_CS_CLOSED) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02003440 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet4e72b172022-10-04 17:35:19 +02003441 (h1c->state == H1_CS_CLOSING && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003442 !h1c->conn->owner) {
3443 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003444 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003445 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003446 else {
Christopher Faulet4e72b172022-10-04 17:35:19 +02003447 if (h1c->state == H1_CS_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003448 /* If we have a new request, process it immediately or
3449 * subscribe for reads waiting for new data
3450 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003451 if (unlikely(b_data(&h1c->ibuf))) {
3452 if (h1_process(h1c) == -1)
3453 goto end;
3454 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003455 else
3456 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3457 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003458 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003459 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003460 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003461 end:
3462 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003463}
3464
3465
Willy Tarreau000d63c2022-05-27 10:39:17 +02003466static void h1_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003467{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003468 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet7f366362019-04-08 10:51:20 +02003469 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003470
3471 if (!h1s)
3472 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003473 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003474
Christopher Fauletc3fe6f32022-10-05 10:24:11 +02003475 TRACE_POINT(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003476}
3477
Willy Tarreau000d63c2022-05-27 10:39:17 +02003478static void h1_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003479{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003480 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003481 struct h1c *h1c;
3482
3483 if (!h1s)
3484 return;
3485 h1c = h1s->h1c;
3486
Christopher Faulet99293b02021-11-10 10:30:15 +01003487 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003488
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003489 if (se_fl_test(h1s->sd, SE_FL_SHW))
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003490 goto end;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003491 if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003492 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003493 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003494 }
3495 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3496 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3497 goto do_shutw;
3498 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003499
Christopher Faulet4e72b172022-10-04 17:35:19 +02003500 if (h1c->state == H1_CS_UPGRADING && !(h1c->flags & H1C_F_ERROR)) {
3501 TRACE_STATE("keep connection alive (UPGRADING)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003502 goto end;
3503 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003504 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3505 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003506 goto end;
3507 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003508
Christopher Faulet7f366362019-04-08 10:51:20 +02003509 do_shutw:
Christopher Faulet4e72b172022-10-04 17:35:19 +02003510 h1c->state = H1_CS_CLOSING;
Christopher Faulet07976562022-03-31 11:05:05 +02003511 if (mode != CO_SHW_NORMAL)
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003512 h1c->flags |= H1C_F_SILENT_SHUT;
Christopher Fauleta85c5222021-10-27 15:36:38 +02003513
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003514 if (!b_data(&h1c->obuf))
Christopher Faulet897d6122021-12-17 17:28:35 +01003515 h1_shutw_conn(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003516 end:
3517 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003518}
3519
Christopher Fauleta85c5222021-10-27 15:36:38 +02003520static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003521{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003522 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003523
Christopher Faulet4e72b172022-10-04 17:35:19 +02003524 TRACE_ENTER(H1_EV_H1C_END, conn);
3525 h1_close(h1c);
Willy Tarreau62592ad2021-03-26 09:09:42 +01003526 if (conn->flags & CO_FL_SOCK_WR_SH)
3527 return;
3528
Christopher Faulet666a0c42019-01-08 11:12:04 +01003529 conn_xprt_shutw(conn);
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003530 conn_sock_shutw(conn, (h1c && !(h1c->flags & H1C_F_SILENT_SHUT)));
Christopher Fauleta85c5222021-10-27 15:36:38 +02003531 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003532}
3533
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003534/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3535 * The <es> pointer is not allowed to differ from the one passed to the
3536 * subscribe() call. It always returns zero.
3537 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003538static int h1_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003539{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003540 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003541
3542 if (!h1s)
3543 return 0;
3544
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003545 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003546 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003547
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003548 es->events &= ~event_type;
3549 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003550 h1s->subs = NULL;
3551
3552 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003553 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003554
3555 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003556 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003557
Christopher Faulet51dbc942018-09-13 09:05:15 +02003558 return 0;
3559}
3560
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003561/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3562 * event subscriber <es> is not allowed to change from a previous call as long
3563 * as at least one event is still subscribed. The <event_type> must only be a
3564 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
Willy Tarreau000d63c2022-05-27 10:39:17 +02003565 * the stream connector <sc> was already detached, in which case it will return
Willy Tarreau4596fe22022-05-17 19:07:51 +02003566 * -1.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003567 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003568static int h1_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003569{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003570 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51bb1852019-09-04 10:22:34 +02003571 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003572
3573 if (!h1s)
3574 return -1;
3575
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003576 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003577 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003578
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003579 es->events |= event_type;
3580 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003581
3582 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003583 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003584
3585
Christopher Faulet6b81df72019-10-01 22:08:43 +02003586 if (event_type & SUB_RETRY_SEND) {
3587 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003588 /*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003589 * If the stconn attempts to subscribe, and the
Christopher Faulet6b81df72019-10-01 22:08:43 +02003590 * mux isn't subscribed to the connection, then it
3591 * probably means the connection wasn't established
3592 * yet, so we have to subscribe.
3593 */
3594 h1c = h1s->h1c;
3595 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3596 h1c->conn->xprt->subscribe(h1c->conn,
3597 h1c->conn->xprt_ctx,
3598 SUB_RETRY_SEND,
3599 &h1c->wait_event);
3600 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003601 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003602}
3603
Christopher Faulet564e39c2021-09-21 15:50:55 +02003604/* Called from the upper layer, to receive data.
3605 *
3606 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3607 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3608 * means the caller wants to flush input data (from the mux buffer and the
3609 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3610 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3611 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3612 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3613 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3614 * copy as much data as possible.
3615 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003616static size_t h1_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003617{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003618 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet539e0292018-11-19 10:40:09 +01003619 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003620 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003621 size_t ret = 0;
3622
Willy Tarreau022e5e52020-09-10 09:33:15 +02003623 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003624
Christopher Faulet4e72b172022-10-04 17:35:19 +02003625 /* Do nothing for now if not RUNNING (implies UPGRADING) */
3626 if (h1c->state < H1_CS_RUNNING) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003627 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3628 goto end;
3629 }
3630
Christopher Faulet539e0292018-11-19 10:40:09 +01003631 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003632 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003633 else
3634 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003635
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003636 if ((flags & CO_RFL_BUF_FLUSH) && se_fl_test(h1s->sd, SE_FL_MAY_SPLICE)) {
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003637 h1c->flags |= H1C_F_WANT_SPLICE;
3638 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 +02003639 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003640 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003641 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 +01003642 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003643 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003644
3645 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003646 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003647 return ret;
3648}
3649
3650
3651/* Called from the upper layer, to send data */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003652static size_t h1_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003653{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003654 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003655 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003656 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003657
3658 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003659 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003660 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003661
Willy Tarreau022e5e52020-09-10 09:33:15 +02003662 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003663
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003664 /* If we're not connected yet, or we're waiting for a handshake, stop
3665 * now, as we don't want to remove everything from the channel buffer
3666 * before we're sure we can send it.
3667 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003668 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003669 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003670 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003671 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003672
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003673 if (h1c->flags & H1C_F_ERROR) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003674 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet26a26432021-01-27 11:27:50 +01003675 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 +01003676 return 0;
3677 }
3678
Christopher Faulet46230362019-10-17 16:04:20 +02003679 /* Inherit some flags from the upper layer */
3680 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3681 if (flags & CO_SFL_MSG_MORE)
3682 h1c->flags |= H1C_F_CO_MSG_MORE;
3683 if (flags & CO_SFL_STREAMER)
3684 h1c->flags |= H1C_F_CO_STREAMER;
3685
Christopher Fauletc31872f2019-06-04 22:09:36 +02003686 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003687 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003688
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003689 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003690 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003691 else
3692 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003693
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003694 if (!ret)
Christopher Faulet372b38f2022-07-08 15:20:31 +02003695 break;
3696
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003697 if ((count - ret) > 0)
3698 h1c->flags |= H1C_F_CO_MSG_MORE;
3699
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003700 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003701 count -= ret;
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003702
Olivier Houchard68787ef2020-01-15 19:13:32 +01003703 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003704 break;
Christopher Fauletb0b8e9b2022-09-15 16:21:55 +02003705
3706 if ((h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)))
3707 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003708 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003709
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003710 if ((h1c->flags & H1C_F_ERROR) || ((h1c->conn->flags & CO_FL_ERROR) &&
Willy Tarreaucad42a72022-08-29 10:22:56 +02003711 (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf)))) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003712 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet26a26432021-01-27 11:27:50 +01003713 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 +01003714 }
3715
Christopher Faulet7a145d62020-08-05 11:31:16 +02003716 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003717 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003718 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003719}
3720
Willy Tarreaue5733232019-05-22 19:24:06 +02003721#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003722/* Send and get, using splicing */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003723static int h1_rcv_pipe(struct stconn *sc, struct pipe *pipe, unsigned int count)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003724{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003725 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003726 struct h1c *h1c = h1s->h1c;
3727 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003728 int ret = 0;
3729
Christopher Faulet897d6122021-12-17 17:28:35 +01003730 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003731
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003732 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003733 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003734 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 +02003735 goto end;
3736 }
3737
Christopher Fauletcf307562021-07-26 10:49:39 +02003738 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003739 if (h1s_data_pending(h1s)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003740 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003741 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003742 }
3743
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003744 if (!h1_recv_allowed(h1c)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003745 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet0060be92020-07-03 15:02:25 +02003746 goto end;
3747 }
3748
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003749 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003750 count = h1m->curr_len;
Christopher Faulet897d6122021-12-17 17:28:35 +01003751 ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003752 if (ret >= 0) {
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003753 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003754 if (ret > h1m->curr_len) {
3755 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003756 h1c->flags |= H1C_F_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003757 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003758 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003759 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 +01003760 goto end;
3761 }
3762 h1m->curr_len -= ret;
3763 if (!h1m->curr_len) {
3764 h1m->state = H1_MSG_DONE;
3765 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003766 TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003767 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003768 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003769 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
3770 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret);
Christopher Faulete18777b2019-04-16 16:46:36 +02003771 }
3772
Christopher Faulet1be55f92018-10-02 15:59:23 +02003773 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003774 if (conn_xprt_read0_pending(h1c->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003775 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003776 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003777 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003778 }
Christopher Faulet9009c972022-10-05 12:04:56 +02003779 if (h1c->conn->flags & CO_FL_ERROR) {
3780 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003781 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERROR;
Christopher Faulet9009c972022-10-05 12:04:56 +02003782 TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
3783 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003784
Christopher Faulet94d35102021-04-09 11:58:49 +02003785 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003786 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003787 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Faulet94d35102021-04-09 11:58:49 +02003788 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003789 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s);
3790 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet94d35102021-04-09 11:58:49 +02003791 }
Christopher Faulet27182292020-07-03 15:08:49 +02003792 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003793
Christopher Faulet897d6122021-12-17 17:28:35 +01003794 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003795 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003796}
3797
Willy Tarreau000d63c2022-05-27 10:39:17 +02003798static int h1_snd_pipe(struct stconn *sc, struct pipe *pipe)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003799{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003800 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003801 struct h1c *h1c = h1s->h1c;
3802 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003803 int ret = 0;
3804
Christopher Faulet897d6122021-12-17 17:28:35 +01003805 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003806
Christopher Faulet140f1a52021-11-26 16:37:55 +01003807 if (b_data(&h1c->obuf)) {
3808 if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003809 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s);
3810 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003811 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003812 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003813 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003814
Christopher Faulet897d6122021-12-17 17:28:35 +01003815 ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe);
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003816 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003817 if (ret > h1m->curr_len) {
3818 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003819 h1c->flags |= H1C_F_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003820 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003821 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003822 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 +01003823 goto end;
3824 }
3825 h1m->curr_len -= ret;
3826 if (!h1m->curr_len) {
3827 h1m->state = H1_MSG_DONE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003828 TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003829 }
3830 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003831 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
3832 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret);
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003833
3834 end:
Christopher Faulet9009c972022-10-05 12:04:56 +02003835 if (h1c->conn->flags & CO_FL_ERROR) {
3836 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet71abc0c2022-10-04 17:06:52 +02003837 h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERROR;
Christopher Faulet9009c972022-10-05 12:04:56 +02003838 TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
3839 }
3840
Christopher Faulet897d6122021-12-17 17:28:35 +01003841 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003842 return ret;
3843}
3844#endif
3845
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003846static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3847{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003848 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003849 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003850
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003851 switch (mux_ctl) {
3852 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003853 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003854 ret |= MUX_STATUS_READY;
3855 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003856 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003857 if (output)
3858 *((int *)output) = h1c->errcode;
3859 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3860 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3861 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3862 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003863 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003864 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003865 default:
3866 return -1;
3867 }
3868}
3869
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003870/* appends some info about connection <h1c> to buffer <msg>, or does nothing if
3871 * <h1c> is NULL. Returns non-zero if the connection is considered suspicious.
3872 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
3873 * not NULL, otherwise a single line is used.
3874 */
3875static int h1_dump_h1c_info(struct buffer *msg, struct h1c *h1c, const char *pfx)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003876{
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003877 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003878
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003879 if (!h1c)
3880 return ret;
3881
Christopher Fauletf376a312019-01-04 15:16:06 +01003882 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3883 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003884 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3885 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003886 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003887 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003888 return ret;
3889}
3890
3891/* appends some info about stream <h1s> to buffer <msg>, or does nothing if
3892 * <h1s> is NULL. Returns non-zero if the stream is considered suspicious. May
3893 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
3894 * NULL, otherwise a single line is used.
3895 */
3896static int h1_dump_h1s_info(struct buffer *msg, const struct h1s *h1s, const char *pfx)
3897{
3898 const char *method;
3899 int ret = 0;
3900
3901 if (!h1s)
3902 return ret;
3903
3904 if (h1s->meth < HTTP_METH_OTHER)
3905 method = http_known_methods[h1s->meth].ptr;
3906 else
3907 method = "UNKNOWN";
3908
3909 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .sd.flg=0x%x .req.state=%s .res.state=%s",
3910 h1s, h1s->flags, se_fl_get(h1s->sd),
3911 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003912
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003913 if (pfx)
3914 chunk_appendf(msg, "\n%s", pfx);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003915
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003916 chunk_appendf(msg, " .meth=%s status=%d",
3917 method, h1s->status);
Christopher Faulet186367f2022-05-30 08:45:15 +02003918
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003919 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(h1s->sd));
3920 if (!se_fl_test(h1s->sd, SE_FL_ORPHAN))
3921 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
3922 h1s_sc(h1s)->flags, h1s_sc(h1s)->app);
Christopher Faulet186367f2022-05-30 08:45:15 +02003923
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003924 if (pfx && h1s->subs)
3925 chunk_appendf(msg, "\n%s", pfx);
3926
3927 chunk_appendf(msg, " .subs=%p", h1s->subs);
3928 if (h1s->subs) {
3929 chunk_appendf(msg, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3930 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
3931 h1s->subs->tasklet->calls,
3932 h1s->subs->tasklet->context);
3933 if (h1s->subs->tasklet->calls >= 1000000)
3934 ret = 1;
3935 resolve_sym_name(msg, NULL, h1s->subs->tasklet->process);
3936 chunk_appendf(msg, ")");
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003937 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003938 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003939}
3940
Willy Tarreau7079c0f2022-09-02 16:11:28 +02003941/* for debugging with CLI's "show fd" command */
3942static int h1_show_fd(struct buffer *msg, struct connection *conn)
3943{
3944 struct h1c *h1c = conn->ctx;
3945 struct h1s *h1s = h1c->h1s;
3946 int ret = 0;
3947
3948 ret |= h1_dump_h1c_info(msg, h1c, NULL);
3949
3950 if (h1s)
3951 ret |= h1_dump_h1s_info(msg, h1s, NULL);
3952
3953 return ret;
3954}
3955
Willy Tarreaue6f389d2022-09-02 16:32:31 +02003956/* for debugging with CLI's "show sess" command. May emit multiple lines, each
3957 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
3958 * line is used. Each field starts with a space so it's safe to print it after
3959 * existing fields.
3960 */
3961static int h1_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
3962{
3963 struct h1s *h1s = sd->se;
3964 int ret = 0;
3965
3966 if (!h1s)
3967 return ret;
3968
3969 ret |= h1_dump_h1s_info(msg, h1s, pfx);
3970 if (pfx)
3971 chunk_appendf(msg, "\n%s", pfx);
3972 chunk_appendf(msg, " h1c=%p", h1s->h1c);
3973 ret |= h1_dump_h1c_info(msg, h1s->h1c, pfx);
3974 return ret;
3975}
3976
Christopher Faulet98fbe952019-07-22 16:18:24 +02003977
3978/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3979static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3980{
3981 struct h1_hdr_entry *entry;
3982
3983 /* Be sure there is a non-empty <to> */
3984 if (!strlen(to)) {
3985 memprintf(err, "expect <to>");
3986 return -1;
3987 }
3988
3989 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003990 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003991 memprintf(err, "<from> and <to> must not differ execpt the case");
3992 return -1;
3993 }
3994
3995 /* Be sure <from> does not already existsin the tree */
3996 if (ebis_lookup(&hdrs_map.map, from)) {
3997 memprintf(err, "duplicate entry '%s'", from);
3998 return -1;
3999 }
4000
4001 /* Create the entry and insert it in the tree */
4002 entry = malloc(sizeof(*entry));
4003 if (!entry) {
4004 memprintf(err, "out of memory");
4005 return -1;
4006 }
4007
4008 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004009 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01004010 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02004011 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004012 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004013 free(entry);
4014 memprintf(err, "out of memory");
4015 return -1;
4016 }
4017 ebis_insert(&hdrs_map.map, &entry->node);
4018 return 0;
4019}
4020
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004021/* Migrate the the connection to the current thread.
4022 * Return 0 if successful, non-zero otherwise.
4023 * Expected to be called with the old thread lock held.
4024 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004025static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004026{
4027 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004028 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004029
4030 if (fd_takeover(conn->handle.fd, conn) != 0)
4031 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004032
4033 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4034 /* We failed to takeover the xprt, even if the connection may
4035 * still be valid, flag it as error'd, as we have already
4036 * taken over the fd, and wake the tasklet, so that it will
4037 * destroy it.
4038 */
4039 conn->flags |= CO_FL_ERROR;
4040 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
4041 return -1;
4042 }
4043
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004044 if (h1c->wait_event.events)
4045 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
4046 h1c->wait_event.events, &h1c->wait_event);
4047 /* To let the tasklet know it should free itself, and do nothing else,
4048 * set its context to NULL.
4049 */
4050 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004051 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004052
4053 task = h1c->task;
4054 if (task) {
4055 task->context = NULL;
4056 h1c->task = NULL;
4057 __ha_barrier_store();
4058 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004059
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004060 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004061 if (!h1c->task) {
4062 h1_release(h1c);
4063 return -1;
4064 }
4065 h1c->task->process = h1_timeout_task;
4066 h1c->task->context = h1c;
4067 }
4068 h1c->wait_event.tasklet = tasklet_new();
4069 if (!h1c->wait_event.tasklet) {
4070 h1_release(h1c);
4071 return -1;
4072 }
4073 h1c->wait_event.tasklet->process = h1_io_cb;
4074 h1c->wait_event.tasklet->context = h1c;
4075 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
4076 SUB_RETRY_RECV, &h1c->wait_event);
4077
4078 return 0;
4079}
4080
4081
Christopher Faulet98fbe952019-07-22 16:18:24 +02004082static void h1_hdeaders_case_adjust_deinit()
4083{
4084 struct ebpt_node *node, *next;
4085 struct h1_hdr_entry *entry;
4086
4087 node = ebpt_first(&hdrs_map.map);
4088 while (node) {
4089 next = ebpt_next(node);
4090 ebpt_delete(node);
4091 entry = container_of(node, struct h1_hdr_entry, node);
4092 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004093 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004094 free(entry);
4095 node = next;
4096 }
4097 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004098}
4099
Christopher Faulet98fbe952019-07-22 16:18:24 +02004100static int cfg_h1_headers_case_adjust_postparser()
4101{
4102 FILE *file = NULL;
4103 char *c, *key_beg, *key_end, *value_beg, *value_end;
4104 char *err;
4105 int rc, line = 0, err_code = 0;
4106
4107 if (!hdrs_map.name)
4108 goto end;
4109
4110 file = fopen(hdrs_map.name, "r");
4111 if (!file) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004112 ha_alert("h1-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004113 hdrs_map.name);
4114 err_code |= ERR_ALERT | ERR_FATAL;
4115 goto end;
4116 }
4117
4118 /* now parse all lines. The file may contain only two header name per
4119 * line, separated by spaces. All heading and trailing spaces will be
4120 * ignored. Lines starting with a # are ignored.
4121 */
4122 while (fgets(trash.area, trash.size, file) != NULL) {
4123 line++;
4124 c = trash.area;
4125
4126 /* strip leading spaces and tabs */
4127 while (*c == ' ' || *c == '\t')
4128 c++;
4129
4130 /* ignore emptu lines, or lines beginning with a dash */
4131 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
4132 continue;
4133
4134 /* look for the end of the key */
4135 key_beg = c;
4136 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
4137 c++;
4138 key_end = c;
4139
4140 /* strip middle spaces and tabs */
4141 while (*c == ' ' || *c == '\t')
4142 c++;
4143
4144 /* look for the end of the value, it is the end of the line */
4145 value_beg = c;
4146 while (*c && *c != '\n' && *c != '\r')
4147 c++;
4148 value_end = c;
4149
4150 /* trim possibly trailing spaces and tabs */
4151 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
4152 value_end--;
4153
4154 /* set final \0 and check entries */
4155 *key_end = '\0';
4156 *value_end = '\0';
4157
4158 err = NULL;
4159 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
4160 if (rc < 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004161 ha_alert("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004162 hdrs_map.name, err, line);
4163 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004164 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004165 goto end;
4166 }
4167 if (rc > 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004168 ha_warning("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004169 hdrs_map.name, err, line);
4170 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004171 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004172 }
4173 }
4174
4175 end:
4176 if (file)
4177 fclose(file);
4178 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
4179 return err_code;
4180}
4181
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004182/* config parser for global "h1-accept-payload_=-with-any-method" */
4183static int cfg_parse_h1_accept_payload_with_any_method(char **args, int section_type, struct proxy *curpx,
4184 const struct proxy *defpx, const char *file, int line,
4185 char **err)
4186{
4187 if (too_many_args(0, args, err, NULL))
4188 return -1;
4189 accept_payload_with_any_method = 1;
4190 return 0;
4191}
4192
Christopher Faulet98fbe952019-07-22 16:18:24 +02004193
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004194/* config parser for global "h1-header-case-adjust" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004195static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004196 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004197 char **err)
4198{
4199 if (too_many_args(2, args, err, NULL))
4200 return -1;
4201 if (!*(args[1]) || !*(args[2])) {
4202 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
4203 return -1;
4204 }
4205 return add_hdr_case_adjust(args[1], args[2], err);
4206}
4207
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004208/* config parser for global "h1-headers-case-adjust-file" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004209static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004210 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004211 char **err)
4212{
4213 if (too_many_args(1, args, err, NULL))
4214 return -1;
4215 if (!*(args[1])) {
4216 memprintf(err, "'%s' expects <file> as argument.", args[0]);
4217 return -1;
4218 }
4219 free(hdrs_map.name);
4220 hdrs_map.name = strdup(args[1]);
4221 return 0;
4222}
4223
Christopher Faulet98fbe952019-07-22 16:18:24 +02004224/* config keyword parsers */
4225static struct cfg_kw_list cfg_kws = {{ }, {
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004226 { CFG_GLOBAL, "h1-accept-payload-with-any-method", cfg_parse_h1_accept_payload_with_any_method },
Christopher Faulet98fbe952019-07-22 16:18:24 +02004227 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
4228 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
4229 { 0, NULL, NULL },
4230 }
4231};
4232
4233INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4234REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
4235
4236
Christopher Faulet51dbc942018-09-13 09:05:15 +02004237/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05004238/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02004239/****************************************/
4240
4241/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01004242static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02004243 .init = h1_init,
4244 .wake = h1_wake,
4245 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004246 .get_first_sc = h1_get_first_sc,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004247 .detach = h1_detach,
4248 .destroy = h1_destroy,
4249 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01004250 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004251 .rcv_buf = h1_rcv_buf,
4252 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02004253#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02004254 .rcv_pipe = h1_rcv_pipe,
4255 .snd_pipe = h1_snd_pipe,
4256#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02004257 .subscribe = h1_subscribe,
4258 .unsubscribe = h1_unsubscribe,
4259 .shutr = h1_shutr,
4260 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004261 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004262 .show_sd = h1_show_sd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004263 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004264 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02004265 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02004266 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02004267};
4268
Christopher Faulet3f612f72021-02-05 16:44:21 +01004269static const struct mux_ops mux_h1_ops = {
4270 .init = h1_init,
4271 .wake = h1_wake,
4272 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004273 .get_first_sc = h1_get_first_sc,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004274 .detach = h1_detach,
4275 .destroy = h1_destroy,
4276 .avail_streams = h1_avail_streams,
4277 .used_streams = h1_used_streams,
4278 .rcv_buf = h1_rcv_buf,
4279 .snd_buf = h1_snd_buf,
4280#if defined(USE_LINUX_SPLICE)
4281 .rcv_pipe = h1_rcv_pipe,
4282 .snd_pipe = h1_snd_pipe,
4283#endif
4284 .subscribe = h1_subscribe,
4285 .unsubscribe = h1_unsubscribe,
4286 .shutr = h1_shutr,
4287 .shutw = h1_shutw,
4288 .show_fd = h1_show_fd,
Willy Tarreaue6f389d2022-09-02 16:32:31 +02004289 .show_sd = h1_show_sd,
Christopher Faulet3f612f72021-02-05 16:44:21 +01004290 .ctl = h1_ctl,
4291 .takeover = h1_takeover,
4292 .flags = MX_FL_HTX|MX_FL_NO_UPG,
4293 .name = "H1",
4294};
Christopher Faulet51dbc942018-09-13 09:05:15 +02004295
Christopher Faulet3f612f72021-02-05 16:44:21 +01004296/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
4297static struct mux_proto_list mux_proto_h1 =
4298 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
4299static struct mux_proto_list mux_proto_http =
4300 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02004301
Christopher Faulet3f612f72021-02-05 16:44:21 +01004302INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
4303INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01004304
Christopher Faulet51dbc942018-09-13 09:05:15 +02004305/*
4306 * Local variables:
4307 * c-indent-level: 8
4308 * c-basic-offset: 8
4309 * End:
4310 */