blob: ddce75f8f53d4a8e086a67f9ed54a4f8a8611996 [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>
Willy Tarreau551271d2020-06-04 08:32:23 +020026#include <haproxy/pipe-t.h>
Willy Tarreauadc02402021-05-08 20:28:54 +020027#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020028#include <haproxy/session-t.h>
Christopher Fauletb4c584e2021-11-26 17:37:07 +010029#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020030#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020031#include <haproxy/stream.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020032#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020033
34/*
35 * H1 Connection flags (32 bits)
36 */
37#define H1C_F_NONE 0x00000000
38
39/* Flags indicating why writing output data are blocked */
40#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
41#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
42/* 0x00000004 - 0x00000008 unused */
43
44/* Flags indicating why reading input data are blocked. */
45#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
46#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletd17ad822020-09-24 15:14:29 +020047#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
Christopher Faulet51dbc942018-09-13 09:05:15 +020048
Christopher Faulet7b109f22019-12-05 11:18:31 +010049/* Flags indicating the connection state */
Willy Tarreau4596fe22022-05-17 19:07:51 +020050#define H1C_F_ST_EMBRYONIC 0x00000100 /* Set when a H1 stream with no stream connector is attached to the connection */
51#define H1C_F_ST_ATTACHED 0x00000200 /* Set when a H1 stream with a stream connector is attached to the connection (may be not READY) */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010052#define H1C_F_ST_IDLE 0x00000400 /* connection is idle and may be reused
53 * (exclusive to all H1C_F_ST flags and never set when an h1s is attached) */
Willy Tarreau4596fe22022-05-17 19:07:51 +020054#define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (stream connector may still be attached) */
55#define H1C_F_ST_SHUTDOWN 0x00001000 /* connection must be shut down ASAP flushing output first (stream connector may still be attached) */
56#define H1C_F_ST_READY 0x00002000 /* Set in ATTACHED state with a READY stream connector. A stream connector is not ready when
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010057 * a TCP>H1 upgrade is in progress Thus this flag is only set if ATTACHED is also set */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010058#define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED)
Christopher Faulet75308302021-11-15 14:51:37 +010059#define H1C_F_ST_SILENT_SHUT 0x00004000 /* silent (or dirty) shutdown must be performed (implied ST_SHUTDOWN) */
Christopher Fauleta85c5222021-10-27 15:36:38 +020060/* 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020061
Christopher Fauletb385b502021-01-13 18:47:57 +010062#define H1C_F_WANT_SPLICE 0x00010000 /* Don't read into a buffer because we want to use or we are using splicing */
63#define H1C_F_ERR_PENDING 0x00020000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
64#define H1C_F_WAIT_NEXT_REQ 0x00040000 /* waiting for the next request to start, use keep-alive timeout */
65#define H1C_F_UPG_H2C 0x00080000 /* set if an upgrade to h2 should be done */
66#define H1C_F_CO_MSG_MORE 0x00100000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
67#define H1C_F_CO_STREAMER 0x00200000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
Christopher Faulet129817b2018-09-20 16:14:40 +020068
Christopher Faulet10a86702021-04-07 14:18:11 +020069/* 0x00400000 - 0x40000000 unusued*/
Christopher Faulet3ced1d12020-11-27 16:44:01 +010070#define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */
Christopher Faulet46230362019-10-17 16:04:20 +020071
Christopher Faulet51dbc942018-09-13 09:05:15 +020072/*
73 * H1 Stream flags (32 bits)
74 */
Christopher Faulet129817b2018-09-20 16:14:40 +020075#define H1S_F_NONE 0x00000000
Christopher Faulet10a86702021-04-07 14:18:11 +020076
77#define H1S_F_RX_BLK 0x00100000 /* Don't process more input data, waiting sync with output side */
78#define H1S_F_TX_BLK 0x00200000 /* Don't process more output data, waiting sync with input side */
Christopher Faulet46e058d2021-09-20 07:47:27 +020079#define H1S_F_RX_CONGESTED 0x00000004 /* Cannot process input data RX path is congested (waiting for more space in channel's buffer) */
80
Willy Tarreauc493c9c2019-06-03 14:18:22 +020081#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020082#define H1S_F_WANT_KAL 0x00000010
83#define H1S_F_WANT_TUN 0x00000020
84#define H1S_F_WANT_CLO 0x00000040
85#define H1S_F_WANT_MSK 0x00000070
86#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet5696f542020-12-02 16:08:38 +010087#define H1S_F_BODYLESS_RESP 0x00000100 /* Bodyless response message */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020088
Ilya Shipitsinacf84592021-02-06 22:29:08 +050089/* 0x00000200 unused */
Christopher Faulet2eed8002020-12-07 11:26:13 +010090#define H1S_F_NOT_IMPL_ERROR 0x00000400 /* Set when a feature is not implemented during the message parsing */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020091#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
92#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
93#define H1S_F_ERROR 0x00001800 /* stream error mask */
94
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020095#define H1S_F_HAVE_SRV_NAME 0x00002000 /* Set during output process if the server name header was added to the request */
Christopher Fauletada34b62019-05-24 16:36:43 +020096#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020097
98/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020099struct h1c {
100 struct connection *conn;
101 struct proxy *px;
102 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +0200103 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200104 struct buffer ibuf; /* Input buffer to store data before parsing */
105 struct buffer obuf; /* Output buffer to store data after reformatting */
106
107 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
108 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
109
110 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100111 struct task *task; /* timeout management task */
Christopher Faulet563c3452021-11-26 17:37:51 +0100112 struct h1_counters *px_counters; /* h1 counters attached to proxy */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200113 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
114 int timeout; /* client/server timeout duration */
115 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200116};
117
118/* H1 stream descriptor */
119struct h1s {
120 struct h1c *h1c;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200121 struct sedesc *sd;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100122 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200123
Willy Tarreau4596fe22022-05-17 19:07:51 +0200124 struct wait_event *subs; /* Address of the wait_event the stream connector associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200125
Olivier Houchardf502aca2018-12-14 19:42:40 +0100126 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200127 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200128 struct h1m req;
129 struct h1m res;
130
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500131 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200132 uint16_t status; /* HTTP response status */
Amaury Denoyellec1938232020-12-11 17:53:03 +0100133
134 char ws_key[25]; /* websocket handshake key */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200135};
136
Christopher Faulet98fbe952019-07-22 16:18:24 +0200137/* Map of headers used to convert outgoing headers */
138struct h1_hdrs_map {
139 char *name;
140 struct eb_root map;
141};
142
143/* An entry in a headers map */
144struct h1_hdr_entry {
145 struct ist name;
146 struct ebpt_node node;
147};
148
149/* Declare the headers map */
150static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
Christopher Faulet0f9c0f52022-05-13 09:20:13 +0200151static int accept_payload_with_any_method = 0;
Christopher Faulet98fbe952019-07-22 16:18:24 +0200152
Christopher Faulet6b81df72019-10-01 22:08:43 +0200153/* trace source and events */
154static void h1_trace(enum trace_level level, uint64_t mask,
155 const struct trace_source *src,
156 const struct ist where, const struct ist func,
157 const void *a1, const void *a2, const void *a3, const void *a4);
158
159/* The event representation is split like this :
160 * h1c - internal H1 connection
161 * h1s - internal H1 stream
162 * strm - application layer
163 * rx - data receipt
164 * tx - data transmission
165 *
166 */
167static const struct trace_event h1_trace_events[] = {
168#define H1_EV_H1C_NEW (1ULL << 0)
169 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
170#define H1_EV_H1C_RECV (1ULL << 1)
171 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
172#define H1_EV_H1C_SEND (1ULL << 2)
173 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
174#define H1_EV_H1C_BLK (1ULL << 3)
175 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
176#define H1_EV_H1C_WAKE (1ULL << 4)
177 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
178#define H1_EV_H1C_END (1ULL << 5)
179 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
180#define H1_EV_H1C_ERR (1ULL << 6)
181 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
182
183#define H1_EV_RX_DATA (1ULL << 7)
184 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
185#define H1_EV_RX_EOI (1ULL << 8)
186 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
187#define H1_EV_RX_HDRS (1ULL << 9)
188 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
189#define H1_EV_RX_BODY (1ULL << 10)
190 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
191#define H1_EV_RX_TLRS (1ULL << 11)
192 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
193
194#define H1_EV_TX_DATA (1ULL << 12)
195 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
196#define H1_EV_TX_EOI (1ULL << 13)
197 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
198#define H1_EV_TX_HDRS (1ULL << 14)
199 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
200#define H1_EV_TX_BODY (1ULL << 15)
201 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
202#define H1_EV_TX_TLRS (1ULL << 16)
203 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
204
205#define H1_EV_H1S_NEW (1ULL << 17)
206 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
207#define H1_EV_H1S_BLK (1ULL << 18)
208 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
209#define H1_EV_H1S_END (1ULL << 19)
210 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
211#define H1_EV_H1S_ERR (1ULL << 20)
212 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
213
214#define H1_EV_STRM_NEW (1ULL << 21)
215 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
216#define H1_EV_STRM_RECV (1ULL << 22)
217 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
218#define H1_EV_STRM_SEND (1ULL << 23)
219 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
220#define H1_EV_STRM_WAKE (1ULL << 24)
221 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
222#define H1_EV_STRM_SHUT (1ULL << 25)
223 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
224#define H1_EV_STRM_END (1ULL << 26)
225 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
226#define H1_EV_STRM_ERR (1ULL << 27)
227 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
228
229 { }
230};
231
232static const struct name_desc h1_trace_lockon_args[4] = {
233 /* arg1 */ { /* already used by the connection */ },
234 /* arg2 */ { .name="h1s", .desc="H1 stream" },
235 /* arg3 */ { },
236 /* arg4 */ { }
237};
238
239static const struct name_desc h1_trace_decoding[] = {
240#define H1_VERB_CLEAN 1
241 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
242#define H1_VERB_MINIMAL 2
243 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
244#define H1_VERB_SIMPLE 3
245 { .name="simple", .desc="add request/response status line or htx info when available" },
246#define H1_VERB_ADVANCED 4
247 { .name="advanced", .desc="add header fields or frame decoding when available" },
248#define H1_VERB_COMPLETE 5
249 { .name="complete", .desc="add full data dump when available" },
250 { /* end */ }
251};
252
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200253static struct trace_source trace_h1 __read_mostly = {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200254 .name = IST("h1"),
255 .desc = "HTTP/1 multiplexer",
256 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
257 .default_cb = h1_trace,
258 .known_events = h1_trace_events,
259 .lockon_args = h1_trace_lockon_args,
260 .decoding = h1_trace_decoding,
261 .report_events = ~0, // report everything by default
262};
263
264#define TRACE_SOURCE &trace_h1
265INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
266
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100267
268/* h1 stats module */
269enum {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100270 H1_ST_OPEN_CONN,
271 H1_ST_OPEN_STREAM,
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100272 H1_ST_TOTAL_CONN,
273 H1_ST_TOTAL_STREAM,
274
Christopher Faulet41951ab2021-11-26 18:12:51 +0100275 H1_ST_BYTES_IN,
276 H1_ST_BYTES_OUT,
277#if defined(USE_LINUX_SPLICE)
278 H1_ST_SPLICED_BYTES_IN,
279 H1_ST_SPLICED_BYTES_OUT,
280#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100281 H1_STATS_COUNT /* must be the last member of the enum */
282};
283
284
285static struct name_desc h1_stats[] = {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100286 [H1_ST_OPEN_CONN] = { .name = "h1_open_connections",
287 .desc = "Count of currently open connections" },
288 [H1_ST_OPEN_STREAM] = { .name = "h1_open_streams",
289 .desc = "Count of currently open streams" },
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100290 [H1_ST_TOTAL_CONN] = { .name = "h1_total_connections",
291 .desc = "Total number of connections" },
292 [H1_ST_TOTAL_STREAM] = { .name = "h1_total_streams",
Christopher Faulet41951ab2021-11-26 18:12:51 +0100293 .desc = "Total number of streams" },
294
295 [H1_ST_BYTES_IN] = { .name = "h1_bytes_in",
296 .desc = "Total number of bytes received" },
297 [H1_ST_BYTES_OUT] = { .name = "h1_bytes_out",
298 .desc = "Total number of bytes send" },
299#if defined(USE_LINUX_SPLICE)
300 [H1_ST_SPLICED_BYTES_IN] = { .name = "h1_spliced_bytes_in",
301 .desc = "Total number of bytes received using kernel splicing" },
302 [H1_ST_SPLICED_BYTES_OUT] = { .name = "h1_spliced_bytes_out",
303 .desc = "Total number of bytes sendusing kernel splicing" },
304#endif
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100305
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100306};
307
308static struct h1_counters {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100309 long long open_conns; /* count of currently open connections */
310 long long open_streams; /* count of currently open streams */
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100311 long long total_conns; /* total number of connections */
312 long long total_streams; /* total number of streams */
313
Christopher Faulet41951ab2021-11-26 18:12:51 +0100314 long long bytes_in; /* number of bytes received */
315 long long bytes_out; /* number of bytes sent */
316#if defined(USE_LINUX_SPLICE)
317 long long spliced_bytes_in; /* number of bytes received using kernel splicing */
318 long long spliced_bytes_out; /* number of bytes sent using kernel splicing */
319#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100320} h1_counters;
321
322static void h1_fill_stats(void *data, struct field *stats)
323{
Christopher Faulet60fa0512021-11-26 18:10:39 +0100324 struct h1_counters *counters = data;
325
326 stats[H1_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
327 stats[H1_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100328 stats[H1_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
329 stats[H1_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
Christopher Faulet41951ab2021-11-26 18:12:51 +0100330
331 stats[H1_ST_BYTES_IN] = mkf_u64(FN_COUNTER, counters->bytes_in);
332 stats[H1_ST_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->bytes_out);
333#if defined(USE_LINUX_SPLICE)
334 stats[H1_ST_SPLICED_BYTES_IN] = mkf_u64(FN_COUNTER, counters->spliced_bytes_in);
335 stats[H1_ST_SPLICED_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->spliced_bytes_out);
336#endif
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100337}
338
339static struct stats_module h1_stats_module = {
340 .name = "h1",
341 .fill_stats = h1_fill_stats,
342 .stats = h1_stats,
343 .stats_count = H1_STATS_COUNT,
344 .counters = &h1_counters,
345 .counters_size = sizeof(h1_counters),
346 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
347 .clearable = 1,
348};
349
350INITCALL1(STG_REGISTER, stats_register_module, &h1_stats_module);
351
352
Christopher Faulet51dbc942018-09-13 09:05:15 +0200353/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100354DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
355DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200356
Christopher Faulet51dbc942018-09-13 09:05:15 +0200357static int h1_recv(struct h1c *h1c);
358static int h1_send(struct h1c *h1c);
359static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100360/* h1_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100361struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state);
362struct task *h1_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Fauleta85c5222021-10-27 15:36:38 +0200363static void h1_shutw_conn(struct connection *conn);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200364static void h1_wake_stream_for_recv(struct h1s *h1s);
365static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200367/* returns the stconn associated to the H1 stream */
368static forceinline struct stconn *h1s_sc(const struct h1s *h1s)
369{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200370 return h1s->sd->sc;
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200371}
372
Christopher Faulet6b81df72019-10-01 22:08:43 +0200373/* the H1 traces always expect that arg1, if non-null, is of type connection
374 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
375 * that arg3, if non-null, is a htx for rx/tx headers.
376 */
377static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
378 const struct ist where, const struct ist func,
379 const void *a1, const void *a2, const void *a3, const void *a4)
380{
381 const struct connection *conn = a1;
382 const struct h1c *h1c = conn ? conn->ctx : NULL;
383 const struct h1s *h1s = a2;
384 const struct htx *htx = a3;
385 const size_t *val = a4;
386
387 if (!h1c)
388 h1c = (h1s ? h1s->h1c : NULL);
389
390 if (!h1c || src->verbosity < H1_VERB_CLEAN)
391 return;
392
393 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200394 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200395
396 /* Display request and response states if h1s is defined */
Christopher Faulet6580f282021-11-26 17:31:35 +0100397 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200398 chunk_appendf(&trace_buf, " [%s, %s]",
399 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
400
Christopher Faulet6580f282021-11-26 17:31:35 +0100401 if (src->verbosity > H1_VERB_SIMPLE) {
402 chunk_appendf(&trace_buf, " - req=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
403 h1s->req.flags, (unsigned long)h1s->req.curr_len, (unsigned long)h1s->req.body_len);
404 chunk_appendf(&trace_buf, " res=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
405 h1s->res.flags, (unsigned long)h1s->res.curr_len, (unsigned long)h1s->res.body_len);
406 }
407
408 }
409
Christopher Faulet6b81df72019-10-01 22:08:43 +0200410 if (src->verbosity == H1_VERB_CLEAN)
411 return;
412
413 /* Display the value to the 4th argument (level > STATE) */
414 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100415 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200416
417 /* Display status-line if possible (verbosity > MINIMAL) */
418 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
419 const struct htx_blk *blk = htx_get_head_blk(htx);
420 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
421 enum htx_blk_type type = htx_get_blk_type(blk);
422
423 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
424 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
425 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
426 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
427 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
428 }
429
430 /* Display h1c info and, if defined, h1s info (pointer + flags) */
431 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100432 if (h1c->conn)
433 chunk_appendf(&trace_buf, " conn=%p(0x%08x)", h1c->conn, h1c->conn->flags);
434 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200435 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200436 if (h1s->sd)
437 chunk_appendf(&trace_buf, " sd=%p(0x%08x)", h1s->sd, se_fl_get(h1s->sd));
438 if (h1s->sd && h1s_sc(h1s))
Willy Tarreau000d63c2022-05-27 10:39:17 +0200439 chunk_appendf(&trace_buf, " sc=%p(0x%08x)", h1s_sc(h1s), h1s_sc(h1s)->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100440 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200441
442 if (src->verbosity == H1_VERB_MINIMAL)
443 return;
444
445 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
446 if (src->level > TRACE_LEVEL_USER) {
447 if (src->verbosity == H1_VERB_COMPLETE ||
448 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
449 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
450 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
451 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
452 if (src->verbosity == H1_VERB_COMPLETE ||
453 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
454 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
455 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
456 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
457 }
458
459 /* Display htx info if defined (level > USER) */
460 if (src->level > TRACE_LEVEL_USER && htx) {
461 int full = 0;
462
463 /* Full htx info (level > STATE && verbosity > SIMPLE) */
464 if (src->level > TRACE_LEVEL_STATE) {
465 if (src->verbosity == H1_VERB_COMPLETE)
466 full = 1;
467 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
468 full = 1;
469 }
470
471 chunk_memcat(&trace_buf, "\n\t", 2);
472 htx_dump(&trace_buf, htx, full);
473 }
474}
475
476
Christopher Faulet51dbc942018-09-13 09:05:15 +0200477/*****************************************************/
478/* functions below are for dynamic buffer management */
479/*****************************************************/
480/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100481 * Indicates whether or not we may receive data. The rules are the following :
482 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200483 * must not attempt to receive
484 * - if we are waiting for the connection establishment, we must not attempt
485 * to receive
486 * - if an error was detected on the stream we must not attempt to receive
487 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100488 * - if the input buffer failed to be allocated or is full , we must not try
489 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200490 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200491 * - otherwise must may not attempt to receive
492 */
493static inline int h1_recv_allowed(const struct h1c *h1c)
494{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100495 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100496 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 +0200497 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200498 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200499
Willy Tarreau2febb842020-07-31 09:15:43 +0200500 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
501 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 +0100502 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200503 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100504
Christopher Faulet119ac872020-09-30 17:33:22 +0200505 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
506 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
507 return 0;
508 }
509
Christopher Fauletd17ad822020-09-24 15:14:29 +0200510 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200511 return 1;
512
Christopher Faulet6b81df72019-10-01 22:08:43 +0200513 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 +0200514 return 0;
515}
516
517/*
518 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
519 * flags are used to figure what buffer was requested. It returns 1 if the
520 * allocation succeeds, in which case the connection is woken up, or 0 if it's
521 * impossible to wake up and we prefer to be woken up later.
522 */
523static int h1_buf_available(void *target)
524{
525 struct h1c *h1c = target;
526
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100527 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200528 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 +0200529 h1c->flags &= ~H1C_F_IN_ALLOC;
530 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200531 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200532 return 1;
533 }
534
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100535 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200536 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 +0200537 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200538 if (h1c->h1s)
539 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200540 return 1;
541 }
542
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100543 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200544 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
545 h1c->flags &= ~H1C_F_IN_SALLOC;
546 tasklet_wakeup(h1c->wait_event.tasklet);
547 return 1;
548 }
549
Christopher Faulet51dbc942018-09-13 09:05:15 +0200550 return 0;
551}
552
553/*
554 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
555 */
556static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
557{
558 struct buffer *buf = NULL;
559
Willy Tarreau2b718102021-04-21 07:32:39 +0200560 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100561 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200562 h1c->buf_wait.target = h1c;
563 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200564 LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200565 }
566 return buf;
567}
568
569/*
570 * Release a buffer, if any, and try to wake up entities waiting in the buffer
571 * wait queue.
572 */
573static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
574{
575 if (bptr->size) {
576 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100577 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200578 }
579}
580
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100581/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100582 * or not. We rely on H1C_F_ST_IDLE to know if the connection is in-use or
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100583 * not. This flag is only set when no H1S is attached and when the previous
584 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100585 */
586static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200587{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100588 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200589
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100590 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200591}
592
Willy Tarreau00f18a32019-01-26 12:19:01 +0100593/* returns the number of streams still available on a connection */
594static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100595{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100596 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100597}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200598
Christopher Faulet7a145d62020-08-05 11:31:16 +0200599/* Refresh the h1c task timeout if necessary */
600static void h1_refresh_timeout(struct h1c *h1c)
601{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200602 int is_idle_conn = 0;
603
Christopher Faulet7a145d62020-08-05 11:31:16 +0200604 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100605 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200606 /* half-closed or dead connections : switch to clientfin/serverfin
607 * timeouts so that we don't hang too long on clients that have
608 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200609 */
610 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200611 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 +0200612 is_idle_conn = 1;
Christopher Fauletadcd7892020-10-05 17:13:05 +0200613 }
614 else if (b_data(&h1c->obuf)) {
615 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200616 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200617 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
618 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100619 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
620 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200621 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100622 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 +0200623 /* A frontend connection not yet ready could be treated the same way as an idle
624 * one in case of soft-close.
625 */
626 is_idle_conn = 1;
Christopher Faulet7a145d62020-08-05 11:31:16 +0200627 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200628 else {
Willy Tarreau4596fe22022-05-17 19:07:51 +0200629 /* alive back connections of front connections with a stream connector attached */
Christopher Fauletadcd7892020-10-05 17:13:05 +0200630 h1c->task->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200631 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 +0200632 }
633
Christopher Fauletdbe57792020-10-05 17:50:58 +0200634 /* Finally set the idle expiration date if shorter */
635 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200636
637 if ((h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
638 is_idle_conn && tick_isset(global.close_spread_end)) {
639 /* If a soft-stop is in progress and a close-spread-time
640 * is set, we want to spread idle connection closing roughly
641 * evenly across the defined window. This should only
642 * act on idle frontend connections.
643 * If the window end is already in the past, we wake the
644 * timeout task up immediately so that it can be closed.
645 */
646 int remaining_window = tick_remain(now_ms, global.close_spread_end);
647 if (remaining_window) {
648 /* We don't need to reset the expire if it would
649 * already happen before the close window end.
650 */
651 if (tick_is_le(global.close_spread_end, h1c->task->expire)) {
652 /* Set an expire value shorter than the current value
653 * because the close spread window end comes earlier.
654 */
655 h1c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
656 TRACE_DEVEL("connection timeout set to value before close-spread window end", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
657 }
658 }
659 else {
660 /* We are past the soft close window end, wake the timeout
661 * task up immediately.
662 */
663 task_wakeup(h1c->task, TASK_WOKEN_TIMER);
664 }
665 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200666 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
667 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200668 }
669}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200670
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200671static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200672{
673 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
674 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
675 h1c->idle_exp = TICK_ETERNITY;
676 return;
677 }
678
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100679 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200680 if (!tick_isset(h1c->idle_exp)) {
681 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
682 !b_data(&h1c->ibuf) && /* No input data */
683 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
684 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
685 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
686 }
687 else {
688 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
689 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
690 }
691 }
692 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100693 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200694 if (!tick_isset(h1c->idle_exp)) {
695 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
696 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
697 }
698 }
Willy Tarreaue68bc612022-05-27 11:23:05 +0200699 else { // ST_ATTACHED or SHUTDOWN
Christopher Fauletdbe57792020-10-05 17:50:58 +0200700 h1c->idle_exp = TICK_ETERNITY;
701 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
702 }
703}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200704/*****************************************************************/
705/* functions below are dedicated to the mux setup and management */
706/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200707
708/* returns non-zero if there are input data pending for stream h1s. */
709static inline size_t h1s_data_pending(const struct h1s *h1s)
710{
711 const struct h1m *h1m;
712
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200713 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100714 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200715}
716
Willy Tarreau4596fe22022-05-17 19:07:51 +0200717/* Creates a new stream connector and the associate stream. <input> is used as input
Christopher Faulet16df1782020-12-04 16:47:41 +0100718 * buffer for the stream. On success, it is transferred to the stream and the
719 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
720 * mux must still take care of it. However, there is nothing special to do
721 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
Willy Tarreau4596fe22022-05-17 19:07:51 +0200722 * b_free() on it is always safe. This function returns the stream connector on
Christopher Faulet16df1782020-12-04 16:47:41 +0100723 * success or NULL on error. */
Willy Tarreau000d63c2022-05-27 10:39:17 +0200724static struct stconn *h1s_new_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100725{
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100726 struct h1c *h1c = h1s->h1c;
Christopher Faulet47365272018-10-31 17:40:50 +0100727
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100728 TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100729
Christopher Fauletb669d682022-03-22 18:37:19 +0100730 if (h1s->flags & H1S_F_NOT_FIRST)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200731 se_fl_set(h1s->sd, SE_FL_NOT_FIRST);
Christopher Fauletb669d682022-03-22 18:37:19 +0100732 if (h1s->req.flags & H1_MF_UPG_WEBSOCKET)
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200733 se_fl_set(h1s->sd, SE_FL_WEBSOCKET);
Christopher Fauletb669d682022-03-22 18:37:19 +0100734
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200735 if (!sc_new_from_endp(h1s->sd, h1c->conn->owner, input)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +0200736 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 +0100737 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200738 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200739
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100740 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
741 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100742
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100743 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
744 TRACE_LEAVE(H1_EV_STRM_NEW, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200745 return h1s_sc(h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100746
Christopher Faulet91449b02022-03-22 18:45:55 +0100747 err:
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100748 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100749 return NULL;
750}
751
Willy Tarreau000d63c2022-05-27 10:39:17 +0200752static struct stconn *h1s_upgrade_sc(struct h1s *h1s, struct buffer *input)
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100753{
754 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
755
Willy Tarreaudf1a2fc2022-05-27 11:11:15 +0200756 if (stream_upgrade_from_sc(h1s_sc(h1s), input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100757 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 +0100758 goto err;
759 }
760
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100761 h1s->h1c->flags |= H1C_F_ST_READY;
762 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +0200763 return h1s_sc(h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100764
765 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100766 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100767 return NULL;
768}
769
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200770static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200771{
772 struct h1s *h1s;
773
Christopher Faulet6b81df72019-10-01 22:08:43 +0200774 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
775
Christopher Faulet51dbc942018-09-13 09:05:15 +0200776 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100777 if (!h1s) {
778 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 +0100779 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100780 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200781 h1s->h1c = h1c;
782 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200783 h1s->sess = NULL;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200784 h1s->sd = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100785 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100786 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200787 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100788 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200789
790 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100791 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200792
Christopher Faulet129817b2018-09-20 16:14:40 +0200793 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100794 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200795
796 h1s->status = 0;
797 h1s->meth = HTTP_METH_OTHER;
798
Christopher Faulet47365272018-10-31 17:40:50 +0100799 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
800 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100801 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100802
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200803 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
804 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200805
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200806 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100807 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200808 return NULL;
809}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100810
Willy Tarreau000d63c2022-05-27 10:39:17 +0200811static struct h1s *h1c_frt_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200812{
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200813 struct h1s *h1s;
814
815 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
816
817 h1s = h1s_new(h1c);
818 if (!h1s)
819 goto fail;
820
Willy Tarreau000d63c2022-05-27 10:39:17 +0200821 if (sc) {
822 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200823 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200824 h1s->sd = sc->sedesc;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100825 }
826 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200827 h1s->sd = sedesc_new();
828 if (!h1s->sd)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100829 goto fail;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200830 h1s->sd->se = h1s;
831 h1s->sd->conn = h1c->conn;
832 se_fl_set(h1s->sd, SE_FL_T_MUX | SE_FL_ORPHAN);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100833 }
834
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200835 h1s->sess = sess;
836
837 if (h1c->px->options2 & PR_O2_REQBUG_OK)
838 h1s->req.err_pos = -1;
839
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200840 h1c->idle_exp = TICK_ETERNITY;
841 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200842 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200843 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100844
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200845 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100846 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100847 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200848 return NULL;
849}
850
Willy Tarreau000d63c2022-05-27 10:39:17 +0200851static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess)
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200852{
853 struct h1s *h1s;
854
855 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
856
857 h1s = h1s_new(h1c);
858 if (!h1s)
859 goto fail;
860
Willy Tarreau000d63c2022-05-27 10:39:17 +0200861 if (sc_attach_mux(sc, h1s, h1c->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200862 goto fail;
863
Christopher Faulet10a86702021-04-07 14:18:11 +0200864 h1s->flags |= H1S_F_RX_BLK;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200865 h1s->sd = sc->sedesc;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200866 h1s->sess = sess;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200867
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100868 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200869
870 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
871 h1s->res.err_pos = -1;
872
Christopher Faulet60fa0512021-11-26 18:10:39 +0100873 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100874 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100875
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200876 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
877 return h1s;
878
879 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100880 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +0200881 pool_free(pool_head_h1s, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100882 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200883}
884
885static void h1s_destroy(struct h1s *h1s)
886{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200887 if (h1s) {
888 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200889
Christopher Faulet6b81df72019-10-01 22:08:43 +0200890 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200891 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200892
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100893 if (h1s->subs)
894 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200895
Christopher Fauletd17ad822020-09-24 15:14:29 +0200896 h1_release_buf(h1c, &h1s->rxbuf);
897
Christopher Faulet10a86702021-04-07 14:18:11 +0200898 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Fauletb385b502021-01-13 18:47:57 +0100899 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200900 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
901 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200902 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100903 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +0100904 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 +0200905 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100906
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100907 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100908 !(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 +0100909 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100910 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100911 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100912 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
913 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200914 else {
Christopher Faulet26a26432021-01-27 11:27:50 +0100915 TRACE_STATE("set shudown on h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100916 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200917 }
Christopher Faulet60fa0512021-11-26 18:10:39 +0100918
919 HA_ATOMIC_DEC(&h1c->px_counters->open_streams);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +0200920 BUG_ON(h1s->sd && !se_fl_test(h1s->sd, SE_FL_ORPHAN));
921 sedesc_free(h1s->sd);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200922 pool_free(pool_head_h1s, h1s);
923 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200924}
925
926/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200927 * Initialize the mux once it's attached. It is expected that conn->ctx points
Willy Tarreau4596fe22022-05-17 19:07:51 +0200928 * to the existing stream connector (for outgoing connections or for incoming
929 * ones during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200930 * establishment). <input> is always used as Input buffer and may contain
931 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
932 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200933 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200934static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
935 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200936{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200937 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100938 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200939 void *conn_ctx = conn->ctx;
940
941 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200942
943 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100944 if (!h1c) {
945 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200946 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100947 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200948 h1c->conn = conn;
949 h1c->px = proxy;
950
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100951 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200952 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200953 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200954 h1c->obuf = BUF_NULL;
955 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200956 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200957
Willy Tarreau90f366b2021-02-20 11:49:49 +0100958 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200959 h1c->wait_event.tasklet = tasklet_new();
960 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200961 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200962 h1c->wait_event.tasklet->process = h1_io_cb;
963 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100964 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200965 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200966
Christopher Faulete9b70722019-04-08 10:46:02 +0200967 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200968 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100969 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
970 if (tick_isset(proxy->timeout.serverfin))
971 h1c->shut_timeout = proxy->timeout.serverfin;
Christopher Faulet563c3452021-11-26 17:37:51 +0100972
973 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be,
974 &h1_stats_module);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100975 } else {
976 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
977 if (tick_isset(proxy->timeout.clientfin))
978 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200979
Christopher Faulet563c3452021-11-26 17:37:51 +0100980 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe,
981 &h1_stats_module);
982
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200983 LIST_APPEND(&mux_stopping_data[tid].list,
984 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100985 }
986 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200987 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100988 if (!t) {
989 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 +0100990 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100991 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100992
993 h1c->task = t;
994 t->process = h1_timeout_task;
995 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200996
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100997 t->expire = tick_add(now_ms, h1c->timeout);
998 }
999
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001000 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +02001001
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001002 if (h1c->flags & H1C_F_IS_BACK) {
1003 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +02001004 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
1005 goto fail;
1006 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001007 else if (conn_ctx) {
1008 /* Upgraded frontend connection (from TCP) */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001009 if (!h1c_frt_stream_new(h1c, conn_ctx, h1c->conn->owner))
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001010 goto fail;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001011
Willy Tarreaue68bc612022-05-27 11:23:05 +02001012 /* Attach the SC but Not ready yet */
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001013 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
Willy Tarreaue68bc612022-05-27 11:23:05 +02001014 TRACE_DEVEL("Inherit the SC from TCP connection to perform an upgrade",
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001015 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
1016 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001017
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001018 if (t) {
1019 h1_set_idle_expiration(h1c);
1020 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001021 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001022 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001023
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001024 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +01001025 if (b_data(&h1c->ibuf))
1026 tasklet_wakeup(h1c->wait_event.tasklet);
1027 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +02001028 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001029
Christopher Faulet60fa0512021-11-26 18:10:39 +01001030 HA_ATOMIC_INC(&h1c->px_counters->open_conns);
Christopher Faulet3bca28c2021-11-26 18:12:04 +01001031 HA_ATOMIC_INC(&h1c->px_counters->total_conns);
Christopher Faulet60fa0512021-11-26 18:10:39 +01001032
Christopher Faulet51dbc942018-09-13 09:05:15 +02001033 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001034 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001035 return 0;
1036
1037 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +02001038 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001039 if (h1c->wait_event.tasklet)
1040 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001041 pool_free(pool_head_h1c, h1c);
1042 fail_h1c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +01001043 if (!conn_is_back(conn))
1044 LIST_DEL_INIT(&conn->stopping_list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001045 conn->ctx = conn_ctx; // restore saved context
1046 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001047 return -1;
1048}
1049
Christopher Faulet73c12072019-04-08 11:23:22 +02001050/* release function. This one should be called to free all resources allocated
1051 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +02001052 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001053static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001054{
Christopher Faulet61840e72019-04-15 09:33:32 +02001055 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001056
Christopher Faulet6b81df72019-10-01 22:08:43 +02001057 TRACE_POINT(H1_EV_H1C_END);
1058
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001059 /* The connection must be aattached to this mux to be released */
1060 if (h1c->conn && h1c->conn->ctx == h1c)
1061 conn = h1c->conn;
Christopher Faulet61840e72019-04-15 09:33:32 +02001062
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001063 if (conn && h1c->flags & H1C_F_UPG_H2C) {
1064 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
1065 /* Make sure we're no longer subscribed to anything */
1066 if (h1c->wait_event.events)
1067 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1068 h1c->wait_event.events, &h1c->wait_event);
1069 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
1070 /* connection successfully upgraded to H2, this
1071 * mux was already released */
1072 return;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001073 }
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001074 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
1075 sess_log(conn->owner); /* Log if the upgrade failed */
1076 }
Olivier Houchard45c44372019-06-11 14:06:23 +02001077
Christopher Faulet51dbc942018-09-13 09:05:15 +02001078
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001079 if (LIST_INLIST(&h1c->buf_wait.list))
1080 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001081
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001082 h1_release_buf(h1c, &h1c->ibuf);
1083 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001084
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001085 if (h1c->task) {
1086 h1c->task->context = NULL;
1087 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
1088 h1c->task = NULL;
1089 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001090
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001091 if (h1c->wait_event.tasklet)
1092 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet60fa0512021-11-26 18:10:39 +01001093
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001094 h1s_destroy(h1c->h1s);
1095 if (conn) {
1096 if (h1c->wait_event.events != 0)
1097 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
1098 &h1c->wait_event);
1099 h1_shutw_conn(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001100 }
1101
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001102 HA_ATOMIC_DEC(&h1c->px_counters->open_conns);
1103 pool_free(pool_head_h1c, h1c);
1104
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001105 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001106 if (!conn_is_back(conn))
1107 LIST_DEL_INIT(&conn->stopping_list);
1108
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001109 conn->mux = NULL;
1110 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001111 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001112
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001113 conn_stop_tracking(conn);
1114 conn_full_close(conn);
1115 if (conn->destroy_cb)
1116 conn->destroy_cb(conn);
1117 conn_free(conn);
1118 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001119}
1120
1121/******************************************************/
1122/* functions below are for the H1 protocol processing */
1123/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +02001124/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
1125 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +02001126 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001127static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001128{
Christopher Faulet570d1612018-11-26 11:13:57 +01001129 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001130
Christopher Faulet570d1612018-11-26 11:13:57 +01001131 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001132 (*(p + 5) > '1' ||
1133 (*(p + 5) == '1' && *(p + 7) >= '1')))
1134 h1m->flags |= H1_MF_VER_11;
1135}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001136
Christopher Faulet9768c262018-10-22 09:34:31 +02001137/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
1138 * greater or equal to 1.1
1139 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001140static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +02001141{
Christopher Faulet570d1612018-11-26 11:13:57 +01001142 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001143
Christopher Faulet570d1612018-11-26 11:13:57 +01001144 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001145 (*(p + 5) > '1' ||
1146 (*(p + 5) == '1' && *(p + 7) >= '1')))
1147 h1m->flags |= H1_MF_VER_11;
1148}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001149
Christopher Fauletf2824e62018-10-01 12:12:37 +02001150/* Deduce the connection mode of the client connection, depending on the
1151 * configuration and the H1 message flags. This function is called twice, the
1152 * first time when the request is parsed and the second time when the response
1153 * is parsed.
1154 */
1155static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
1156{
1157 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001158
1159 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001160 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001161 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001162 h1s->status == 101) {
1163 /* Either we've established an explicit tunnel, or we're
1164 * switching the protocol. In both cases, we're very unlikely to
1165 * understand the next protocols. We have to switch to tunnel
1166 * mode, so that we transfer the request and responses then let
1167 * this protocol pass unmodified. When we later implement
1168 * specific parsers for such protocols, we'll want to check the
1169 * Upgrade header which contains information about that protocol
1170 * for responses with status 101 (eg: see RFC2817 about TLS).
1171 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001172 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001173 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 +01001174 }
1175 else if (h1s->flags & H1S_F_WANT_KAL) {
1176 /* By default the client is in KAL mode. CLOSE mode mean
1177 * it is imposed by the client itself. So only change
1178 * KAL mode here. */
1179 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1180 /* no length known or explicit close => close */
1181 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001182 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 +01001183 }
1184 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1185 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001186 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001187 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001188 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 +01001189 }
1190 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001191 }
1192 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001193 /* Input direction: first pass */
1194 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1195 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001196 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001197 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 +01001198 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001199 }
1200
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001201 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001202 * unless a 'close-spread-time' option is set (either to define a
1203 * soft-close window or to disable active closing (close-spread-time
1204 * option set to 0).
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001205 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001206 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001207 int want_clo = 1;
1208 /* If a close-spread-time option is set, we want to avoid
1209 * closing all the active HTTP connections at once so we add a
1210 * random factor that will spread the closing.
1211 */
1212 if (tick_isset(global.close_spread_end)) {
1213 int remaining_window = tick_remain(now_ms, global.close_spread_end);
1214 if (remaining_window) {
1215 /* This should increase the closing rate the further along
1216 * the window we are.
1217 */
1218 want_clo = (remaining_window <= statistical_prng_range(global.close_spread_time));
1219 }
1220 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02001221 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
1222 want_clo = 0;
Remi Tricot-Le Bretonb4f5fac2022-04-25 17:50:48 +02001223
1224 if (want_clo) {
1225 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1226 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);
1227 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001228 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001229}
1230
1231/* Deduce the connection mode of the client connection, depending on the
1232 * configuration and the H1 message flags. This function is called twice, the
1233 * first time when the request is parsed and the second time when the response
1234 * is parsed.
1235 */
1236static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1237{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001238 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001239 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001240 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001241
Christopher Fauletf2824e62018-10-01 12:12:37 +02001242 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001243 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001244 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001245 h1s->status == 101) {
1246 /* Either we've established an explicit tunnel, or we're
1247 * switching the protocol. In both cases, we're very unlikely to
1248 * understand the next protocols. We have to switch to tunnel
1249 * mode, so that we transfer the request and responses then let
1250 * this protocol pass unmodified. When we later implement
1251 * specific parsers for such protocols, we'll want to check the
1252 * Upgrade header which contains information about that protocol
1253 * for responses with status 101 (eg: see RFC2817 about TLS).
1254 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001255 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001256 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 +01001257 }
1258 else if (h1s->flags & H1S_F_WANT_KAL) {
1259 /* By default the server is in KAL mode. CLOSE mode mean
1260 * it is imposed by haproxy itself. So only change KAL
1261 * mode here. */
1262 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1263 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1264 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1265 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001266 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 +01001267 }
1268 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001269 }
Christopher Faulet70033782018-12-05 13:50:11 +01001270 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001271 /* Output direction: first pass */
1272 if (h1m->flags & H1_MF_CONN_CLO) {
1273 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001274 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001275 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 +01001276 }
1277 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1278 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1279 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1280 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1281 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1282 /* no explicit keep-alive option httpclose/server-close => close */
1283 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001284 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 +01001285 }
Christopher Faulet70033782018-12-05 13:50:11 +01001286 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001287
1288 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001289 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001290 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001291 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);
1292 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001293}
1294
Christopher Fauletb992af02019-03-28 15:42:24 +01001295static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001296{
1297 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001298
1299 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1300 * token is found
1301 */
1302 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001303 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001304
1305 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001306 if (!(h1m->flags & H1_MF_VER_11)) {
1307 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 +01001308 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001309 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001310 }
1311 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001312 if (h1m->flags & H1_MF_VER_11) {
1313 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001314 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001315 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001316 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001317}
1318
Christopher Fauletb992af02019-03-28 15:42:24 +01001319static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001320{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001321 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1322 * token is found
1323 */
1324 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001325 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001326
1327 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001328 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001329 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1330 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 +01001331 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001332 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001333 }
1334 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001335 if (h1m->flags & H1_MF_VER_11) {
1336 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001337 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001338 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001339 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001340}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001341
Christopher Fauletb992af02019-03-28 15:42:24 +01001342static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001343{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001344 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001345 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001346 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001347 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001348}
1349
Christopher Fauletb992af02019-03-28 15:42:24 +01001350static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1351{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001352 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001353 h1_set_cli_conn_mode(h1s, h1m);
1354 else
1355 h1_set_srv_conn_mode(h1s, h1m);
1356
1357 if (!(h1m->flags & H1_MF_RESP))
1358 h1_update_req_conn_value(h1s, h1m, conn_val);
1359 else
1360 h1_update_res_conn_value(h1s, h1m, conn_val);
1361}
Christopher Faulete44769b2018-11-29 23:01:45 +01001362
Christopher Faulet98fbe952019-07-22 16:18:24 +02001363/* Try to adjust the case of the message header name using the global map
1364 * <hdrs_map>.
1365 */
1366static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1367{
1368 struct ebpt_node *node;
1369 struct h1_hdr_entry *entry;
1370
1371 /* No entry in the map, do nothing */
1372 if (eb_is_empty(&hdrs_map.map))
1373 return;
1374
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001375 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001376 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1377 return;
1378
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001379 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001380 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1381 return;
1382
1383 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1384 if (!node)
1385 return;
1386 entry = container_of(node, struct h1_hdr_entry, node);
1387 name->ptr = entry->name.ptr;
1388 name->len = entry->name.len;
1389}
1390
Christopher Faulete44769b2018-11-29 23:01:45 +01001391/* Append the description of what is present in error snapshot <es> into <out>.
1392 * The description must be small enough to always fit in a buffer. The output
1393 * buffer may be the trash so the trash must not be used inside this function.
1394 */
1395static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1396{
1397 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001398 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1399 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1400 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1401 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1402 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1403 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001404}
1405/*
1406 * Capture a bad request or response and archive it in the proxy's structure.
1407 * By default it tries to report the error position as h1m->err_pos. However if
1408 * this one is not set, it will then report h1m->next, which is the last known
1409 * parsing point. The function is able to deal with wrapping buffers. It always
1410 * displays buffers as a contiguous area starting at buf->p. The direction is
1411 * determined thanks to the h1m's flags.
1412 */
1413static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1414 struct h1m *h1m, struct buffer *buf)
1415{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001416 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001417 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001418 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001419 union error_snapshot_ctx ctx;
1420
Willy Tarreauea27f482022-05-18 16:10:52 +02001421 if ((h1c->flags & H1C_F_ST_ATTACHED) && sc_strm(h1s_sc(h1s))) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001422 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02001423 sess = __sc_strm(h1s_sc(h1s))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001424 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02001425 other_end = __sc_strm(h1s_sc(h1s))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001426 else
1427 other_end = sess->fe;
1428 } else
1429 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001430
1431 /* http-specific part now */
1432 ctx.h1.state = h1m->state;
1433 ctx.h1.c_flags = h1c->flags;
1434 ctx.h1.s_flags = h1s->flags;
1435 ctx.h1.m_flags = h1m->flags;
1436 ctx.h1.m_clen = h1m->curr_len;
1437 ctx.h1.m_blen = h1m->body_len;
1438
1439 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1440 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001441 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1442 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001443}
1444
Christopher Fauletadb22202018-12-12 10:32:09 +01001445/* Emit the chunksize followed by a CRLF in front of data of the buffer
1446 * <buf>. It goes backwards and starts with the byte before the buffer's
1447 * head. The caller is responsible for ensuring there is enough room left before
1448 * the buffer's head for the string.
1449 */
1450static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1451{
1452 char *beg, *end;
1453
1454 beg = end = b_head(buf);
1455 *--beg = '\n';
1456 *--beg = '\r';
1457 do {
1458 *--beg = hextab[chksz & 0xF];
1459 } while (chksz >>= 4);
1460 buf->head -= (end - beg);
1461 b_add(buf, end - beg);
1462}
1463
1464/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1465 * ensuring there is enough room left in the buffer for the string. */
1466static void h1_emit_chunk_crlf(struct buffer *buf)
1467{
1468 *(b_peek(buf, b_data(buf))) = '\r';
1469 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1470 b_add(buf, 2);
1471}
1472
Christopher Faulet129817b2018-09-20 16:14:40 +02001473/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001474 * Switch the stream to tunnel mode. This function must only be called on 2xx
1475 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001476 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001477static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001478{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001479 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001480
Christopher Faulet5be651d2021-01-22 15:28:03 +01001481 h1s->req.state = H1_MSG_TUNNEL;
1482 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001483
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001484 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001485 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001486
Christopher Faulet5be651d2021-01-22 15:28:03 +01001487 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 +01001488
Christopher Faulet10a86702021-04-07 14:18:11 +02001489 if (h1s->flags & H1S_F_RX_BLK) {
1490 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001491 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001492 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 +02001493 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001494 if (h1s->flags & H1S_F_TX_BLK) {
1495 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001496 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001497 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 +01001498 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001499}
1500
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001501/* Search for a websocket key header. The message should have been identified
1502 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001503 *
1504 * On the request side, if found the key is stored in the session. It might be
1505 * needed to calculate response key if the server side is using http/2.
1506 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001507 * On the response side, the key might be verified if haproxy has been
1508 * responsible for the generation of a key. This happens when a h2 client is
1509 * interfaced with a h1 server.
1510 *
1511 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001512 */
1513static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1514{
1515 struct htx_blk *blk;
1516 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001517 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001518 int ws_key_found = 0, idx;
1519
1520 idx = htx_get_head(htx); // returns the SL that we skip
1521 while ((idx = htx_get_next(htx, idx)) != -1) {
1522 blk = htx_get_blk(htx, idx);
1523 type = htx_get_blk_type(blk);
1524
1525 if (type == HTX_BLK_UNUSED)
1526 continue;
1527
1528 if (type != HTX_BLK_HDR)
1529 break;
1530
1531 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001532 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001533
Amaury Denoyellec1938232020-12-11 17:53:03 +01001534 /* Websocket key is base64 encoded of 16 bytes */
1535 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001536 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001537 /* Copy the key on request side
1538 * we might need it if the server is using h2 and does
1539 * not provide the response
1540 */
1541 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001542 ws_key_found = 1;
1543 break;
1544 }
1545 else if (isteqi(n, ist("sec-websocket-accept")) &&
1546 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001547 /* Need to verify the response key if the input was
1548 * generated by haproxy
1549 */
1550 if (h1s->ws_key[0]) {
1551 char key[29];
1552 h1_calculate_ws_output_key(h1s->ws_key, key);
1553 if (!isteqi(ist(key), v))
1554 break;
1555 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001556 ws_key_found = 1;
1557 break;
1558 }
1559 }
1560
1561 /* missing websocket key, reject the message */
1562 if (!ws_key_found) {
1563 htx->flags |= HTX_FL_PARSING_ERROR;
1564 return 0;
1565 }
1566
1567 return 1;
1568}
1569
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001570/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001571 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001572 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001573 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1574 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001575 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001576static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1577 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001578{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001579 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001580 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001581
Willy Tarreau022e5e52020-09-10 09:33:15 +02001582 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 +02001583
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001584 if (h1s->meth == HTTP_METH_CONNECT)
1585 h1m->flags |= H1_MF_METH_CONNECT;
1586 if (h1s->meth == HTTP_METH_HEAD)
1587 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001588
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001589 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001590 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001591 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 +02001592 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001593 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001594 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 +02001595 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1596 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001597 else if (ret == -2) {
1598 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);
1599 h1s->flags |= H1S_F_RX_CONGESTED;
1600 }
1601 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001602 goto end;
1603 }
1604
Christopher Faulete136bd12021-09-21 18:44:55 +02001605
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001606 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload except if
1607 * accept_payload_with_any_method global option is set.
1608 *There is a payload if the c-l is not null or the the payload is
1609 * chunk-encoded. A parsing error is reported but a A
1610 * 413-Payload-Too-Large is returned instead of a 400-Bad-Request.
Christopher Faulete136bd12021-09-21 18:44:55 +02001611 */
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02001612 if (!accept_payload_with_any_method &&
1613 !(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
Christopher Faulete136bd12021-09-21 18:44:55 +02001614 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1615 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1616 h1s->flags |= H1S_F_PARSING_ERROR;
1617 htx->flags |= HTX_FL_PARSING_ERROR;
1618 h1s->h1c->errcode = 413;
1619 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);
1620 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1621 ret = 0;
1622 goto end;
1623 }
1624
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001625 /* Reject any message with an unknown transfer-encoding. In fact if any
1626 * encoding other than "chunked". A 422-Unprocessable-Content is
1627 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1628 * response.
1629 */
1630 if (h1m->flags & H1_MF_TE_OTHER) {
1631 h1s->flags |= H1S_F_PARSING_ERROR;
1632 htx->flags |= HTX_FL_PARSING_ERROR;
1633 if (!(h1m->flags & H1_MF_RESP))
1634 h1s->h1c->errcode = 422;
1635 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1636 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1637 ret = 0;
1638 goto end;
1639 }
1640
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001641 /* If websocket handshake, search for the websocket key */
1642 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1643 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1644 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1645 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001646 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001647 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 +01001648 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1649
1650 ret = 0;
1651 goto end;
1652 }
1653 }
1654
Christopher Faulet486498c2019-10-11 14:22:00 +02001655 if (h1m->err_pos >= 0) {
1656 /* Maybe we found an error during the parsing while we were
1657 * configured not to block on that, so we have to capture it
1658 * now.
1659 */
1660 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1661 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1662 }
1663
Christopher Faulet5696f542020-12-02 16:08:38 +01001664 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001665 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001666 if (h1s->meth == HTTP_METH_HEAD)
1667 h1s->flags |= H1S_F_BODYLESS_RESP;
1668 }
1669 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001670 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001671 if (h1s->status == 204 || h1s->status == 304)
1672 h1s->flags |= H1S_F_BODYLESS_RESP;
1673 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001674 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001675 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001676
Christopher Faulet129817b2018-09-20 16:14:40 +02001677 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001678 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 +02001679 return ret;
1680}
1681
1682/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001683 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001684 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1685 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001686 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001687static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1688 struct buffer *buf, size_t *ofs, size_t max,
1689 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001690{
Christopher Fauletde471a42021-02-01 16:37:28 +01001691 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001692
Willy Tarreau022e5e52020-09-10 09:33:15 +02001693 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 +02001694 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001695 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001696 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 +01001697 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001698 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001699 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 +02001700 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001701 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001702 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001703 }
1704
Christopher Faulet02a02532019-11-15 09:36:28 +01001705 *ofs += ret;
1706
1707 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001708 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1709 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);
1710 h1s->flags |= H1S_F_RX_CONGESTED;
1711 }
1712
Willy Tarreau022e5e52020-09-10 09:33:15 +02001713 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 +02001714 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001715}
1716
1717/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001718 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1719 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1720 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001721 * responsible to update the parser state <h1m>. If more room is requested,
1722 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001723 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001724static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1725 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001726{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001727 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001728
Willy Tarreau022e5e52020-09-10 09:33:15 +02001729 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 +02001730 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001731 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001732 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 +02001733 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001734 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001735 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 +02001736 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1737 }
Christopher Fauletae660be2022-04-13 17:48:54 +02001738 else if (ret == -2) {
Christopher Faulet46e058d2021-09-20 07:47:27 +02001739 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);
1740 h1s->flags |= H1S_F_RX_CONGESTED;
1741 }
1742 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001743 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001744 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001745
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001746 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001747
1748 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001749 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 +02001750 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001751}
1752
1753/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001754 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001755 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1756 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001757 *
1758 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001759 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001760static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001761{
Christopher Faulet539e0292018-11-19 10:40:09 +01001762 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001763 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001764 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001765 size_t data;
1766 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001767 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001768
Christopher Faulet539e0292018-11-19 10:40:09 +01001769 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001770 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001771
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001772 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001773 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001774
Christopher Faulet2eed8002020-12-07 11:26:13 +01001775 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001776 goto end;
1777
Christopher Faulet10a86702021-04-07 14:18:11 +02001778 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001779 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001780
Christopher Faulet46e058d2021-09-20 07:47:27 +02001781 /* Always remove congestion flags and try to process more input data */
1782 h1s->flags &= ~H1S_F_RX_CONGESTED;
1783
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001784 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001785 size_t used = htx_used_space(htx);
1786
Christopher Faulet129817b2018-09-20 16:14:40 +02001787 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001788 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001789 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001790 if (!ret)
1791 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001792
1793 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1794 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1795
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001796 if ((h1m->flags & H1_MF_RESP) &&
1797 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1798 h1m_init_res(&h1s->res);
1799 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001800 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001801 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001802 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001803 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001804 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001805 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001806 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001807 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001808
1809 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1810 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001811 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001812 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001813 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001814 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001815 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001816 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001817
Christopher Faulet76014fd2019-12-10 11:47:22 +01001818 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1819 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001820 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001821 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001822 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1823 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001824
Christopher Faulet5be651d2021-01-22 15:28:03 +01001825 if ((h1m->flags & H1_MF_RESP) &&
1826 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1827 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001828 else {
1829 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1830 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001831 h1s->flags |= H1S_F_RX_BLK;
1832 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001833 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001834 if (h1s->flags & H1S_F_TX_BLK) {
1835 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001836 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001837 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 +01001838 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001839 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001840 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001841 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001842 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001843 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001844 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001845 if (!ret)
1846 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001847
1848 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1849 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001850 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001851 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001852 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001853 break;
1854 }
1855
Christopher Faulet30db3d72019-05-17 15:35:33 +02001856 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001857 } 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 +01001858
Christopher Faulet129817b2018-09-20 16:14:40 +02001859
Christopher Faulet2eed8002020-12-07 11:26:13 +01001860 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001861 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 +02001862 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001863 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001864
Christopher Faulet539e0292018-11-19 10:40:09 +01001865 b_del(&h1c->ibuf, total);
1866
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001867 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1868
Christopher Faulet30db3d72019-05-17 15:35:33 +02001869 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001870 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001871 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001872 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 +01001873 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001874 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001875
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001876 if (!b_data(&h1c->ibuf))
1877 h1_release_buf(h1c, &h1c->ibuf);
1878
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001879 if (!(h1c->flags & H1C_F_ST_READY)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001880 /* The H1 connection is not ready. Most of time, there is no SC
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001881 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1882 * cases, it is only possible on the client side.
1883 */
1884 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1885
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001886 if (h1m->state <= H1_MSG_LAST_LF) {
1887 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1888 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1889 goto end;
1890 }
1891
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001892 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001893 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 +02001894 BUG_ON(h1s_sc(h1s));
Willy Tarreau000d63c2022-05-27 10:39:17 +02001895 if (!h1s_new_sc(h1s, buf)) {
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001896 h1c->flags |= H1C_F_ST_ERROR;
1897 goto err;
1898 }
1899 }
1900 else {
Willy Tarreaue68bc612022-05-27 11:23:05 +02001901 TRACE_DEVEL("request headers fully parsed, upgrade the inherited SC", H1_EV_RX_DATA, h1c->conn, h1s);
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001902 BUG_ON(h1s_sc(h1s) == NULL);
Willy Tarreau000d63c2022-05-27 10:39:17 +02001903 if (!h1s_upgrade_sc(h1s, buf)) {
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001904 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001905 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001906 goto err;
1907 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001908 }
1909 }
1910
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02001911 /* Here h1s_sc(h1s) is always defined */
Christopher Fauletf5ce3202021-11-26 17:26:19 +01001912 if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) {
Christopher Fauleta583af62020-09-24 15:35:37 +02001913 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 +02001914 se_fl_set(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001915 }
1916 else {
1917 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 +02001918 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Fauleta583af62020-09-24 15:35:37 +02001919 }
1920
Willy Tarreau4596fe22022-05-17 19:07:51 +02001921 /* Set EOI on stream connector in DONE state iff:
Christopher Fauleta22782b2021-02-08 17:18:01 +01001922 * - it is a response
1923 * - it is a request but no a protocol upgrade nor a CONNECT
1924 *
1925 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001926 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001927 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001928 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1929 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001930 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauleta583af62020-09-24 15:35:37 +02001931
Christopher Fauletec4207c2021-04-08 18:34:30 +02001932 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001933 /* When Input data are pending for this message, notify upper layer that
1934 * the mux need more space in the HTX buffer to continue if :
1935 *
1936 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1937 * - Headers or trailers are pending to be copied.
1938 */
1939 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001940 se_fl_set(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001941 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1942 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001943 else {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001944 se_fl_clr(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001945 if (h1s->flags & H1S_F_REOS) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001946 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Faulet1e857782020-12-08 10:38:22 +01001947 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1948 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
Willy Tarreau4596fe22022-05-17 19:07:51 +02001949 * EOI on the stream connector */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001950 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001951 }
Christopher Faulet26a26432021-01-27 11:27:50 +01001952 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001953 se_fl_set(h1s->sd, SE_FL_ERROR);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001954 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 +01001955 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001956
Christopher Faulet10a86702021-04-07 14:18:11 +02001957 if (h1s->flags & H1S_F_TX_BLK) {
1958 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001959 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001960 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 +01001961 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001962 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001963 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001964
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001965 end:
Christopher Faulet5966e402022-07-08 09:02:59 +02001966 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001967 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001968 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001969
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001970 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001971 htx_to_buf(htx, buf);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02001972 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001973 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001974 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001975}
1976
Christopher Faulet129817b2018-09-20 16:14:40 +02001977/*
1978 * Process outgoing data. It parses data and transfer them from the channel buffer into
1979 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1980 * 0 if it couldn't proceed.
1981 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001982static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001983{
Christopher Faulet129817b2018-09-20 16:14:40 +02001984 struct h1s *h1s = h1c->h1s;
1985 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001986 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001987 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001988 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001989 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001990 int last_data = 0;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001991 int ws_key_found = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001992
Christopher Faulet94b2c762019-05-24 15:28:57 +02001993 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001994 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1995
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001996 if (htx_is_empty(chn_htx))
1997 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001998
Christopher Faulet10a86702021-04-07 14:18:11 +02001999 if (h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01002000 goto end;
2001
Christopher Faulet51dbc942018-09-13 09:05:15 +02002002 if (!h1_get_buf(h1c, &h1c->obuf)) {
2003 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002004 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 +02002005 goto end;
2006 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002007
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002008 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02002009
Willy Tarreau37dd54d2018-12-15 14:48:31 +01002010 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01002011 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02002012
Willy Tarreau3815b222018-12-11 19:50:43 +01002013 /* Perform some optimizations to reduce the number of buffer copies.
2014 * First, if the mux's buffer is empty and the htx area contains
2015 * exactly one data block of the same size as the requested count,
2016 * then it's possible to simply swap the caller's buffer with the
2017 * mux's output buffer and adjust offsets and length to match the
2018 * entire DATA HTX block in the middle. In this case we perform a
2019 * true zero-copy operation from end-to-end. This is the situation
2020 * that happens all the time with large files. Second, if this is not
2021 * possible, but the mux's output buffer is empty, we still have an
2022 * opportunity to avoid the copy to the intermediary buffer, by making
2023 * the intermediary buffer's area point to the output buffer's area.
2024 * In this case we want to skip the HTX header to make sure that copies
2025 * remain aligned and that this operation remains possible all the
2026 * time. This goes for headers, data blocks and any data extracted from
2027 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01002028 */
2029 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01002030 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002031 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01002032 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01002033 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01002034 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01002035 void *old_area;
2036
Christopher Faulet6b81df72019-10-01 22:08:43 +02002037 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 +01002038 if (h1m->state == H1_MSG_DATA) {
2039 if (h1m->flags & H1_MF_CLEN) {
2040 if (count > h1m->curr_len) {
2041 TRACE_ERROR("too much payload, more than announced",
2042 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2043 goto error;
2044 }
2045 h1m->curr_len -= count;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002046 if (!h1m->curr_len)
2047 last_data = 1;
Christopher Faulet140f1a52021-11-26 16:37:55 +01002048 }
2049 if (chn_htx->flags & HTX_FL_EOM) {
2050 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2051 last_data = 1;
2052 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002053 }
2054
Christopher Faulete5596bf2020-12-02 16:13:22 +01002055 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01002056 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02002057 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01002058 h1c->obuf.data = count;
2059
2060 buf->area = old_area;
2061 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01002062
Christopher Faulet6b81df72019-10-01 22:08:43 +02002063 chn_htx = (struct htx *)buf->area;
2064 htx_reset(chn_htx);
2065
Christopher Fauletadb22202018-12-12 10:32:09 +01002066 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002067 * size and eventually the last chunk. We have at least
2068 * the size of the struct htx to write the chunk
2069 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01002070 */
2071 if (h1m->flags & H1_MF_CHNK) {
2072 h1_emit_chunk_size(&h1c->obuf, count);
2073 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002074 if (last_data) {
2075 /* Emit the last chunk too at the buffer's end */
2076 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
2077 }
Christopher Fauletadb22202018-12-12 10:32:09 +01002078 }
2079
Christopher Faulet6b81df72019-10-01 22:08:43 +02002080 if (h1m->state == H1_MSG_DATA)
2081 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002082 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002083 else
2084 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002085 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002086
Christopher Faulete5596bf2020-12-02 16:13:22 +01002087 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002088 if (last_data) {
2089 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02002090 if (h1s->flags & H1S_F_RX_BLK) {
2091 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002092 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002093 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 +01002094 }
2095
2096 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2097 H1_EV_TX_DATA, h1c->conn, h1s);
2098 }
Willy Tarreau3815b222018-12-11 19:50:43 +01002099 goto out;
2100 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002101 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002102 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002103 else
2104 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02002105
Christopher Fauletc2518a52019-06-25 21:41:02 +02002106 tmp.data = 0;
2107 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02002108 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01002109 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02002110 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01002111 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02002112 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02002113 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02002114
Christopher Fauletb2e84162018-12-06 11:39:49 +01002115 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002116 if (type != HTX_BLK_DATA && vlen > count)
2117 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002118
Christopher Faulet94b2c762019-05-24 15:28:57 +02002119 if (type == HTX_BLK_UNUSED)
2120 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02002121
Christopher Faulet94b2c762019-05-24 15:28:57 +02002122 switch (h1m->state) {
2123 case H1_MSG_RQBEFORE:
2124 if (type != HTX_BLK_REQ_SL)
2125 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002126 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 +02002127 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002128 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02002129 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002130 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002131 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002132 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002133 if (sl->flags & HTX_SL_F_BODYLESS)
2134 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02002135 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01002136 if (h1s->meth == HTTP_METH_HEAD)
2137 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02002138 if (h1s->flags & H1S_F_RX_BLK) {
2139 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002140 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002141 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 +02002142 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002143 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02002144
Christopher Faulet94b2c762019-05-24 15:28:57 +02002145 case H1_MSG_RPBEFORE:
2146 if (type != HTX_BLK_RES_SL)
2147 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002148 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 +02002149 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002150 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02002151 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002152 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002153 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01002154 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002155 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01002156 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02002157 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01002158 else if (h1s->status == 204 || h1s->status == 304)
2159 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02002160 h1m->state = H1_MSG_HDR_FIRST;
2161 break;
2162
Christopher Faulet94b2c762019-05-24 15:28:57 +02002163 case H1_MSG_HDR_FIRST:
2164 case H1_MSG_HDR_NAME:
2165 case H1_MSG_HDR_L2_LWS:
2166 if (type == HTX_BLK_EOH)
2167 goto last_lf;
2168 if (type != HTX_BLK_HDR)
2169 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002170
Christopher Faulet9768c262018-10-22 09:34:31 +02002171 h1m->state = H1_MSG_HDR_NAME;
2172 n = htx_get_blk_name(chn_htx, blk);
2173 v = htx_get_blk_value(chn_htx, blk);
2174
Christopher Faulet86d144c2019-08-14 16:32:25 +02002175 /* Skip all pseudo-headers */
2176 if (*(n.ptr) == ':')
2177 goto skip_hdr;
2178
Christopher Faulet91fcf212020-12-02 16:17:15 +01002179 if (isteq(n, ist("transfer-encoding"))) {
2180 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2181 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02002182 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01002183 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002184 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01002185 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2186 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01002187 /* Only skip C-L header with invalid value. */
2188 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01002189 goto skip_hdr;
2190 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002191 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01002192 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02002193 if (!v.len)
2194 goto skip_hdr;
2195 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01002196 else if (isteq(n, ist("upgrade"))) {
2197 h1_parse_upgrade_header(h1m, v);
2198 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002199 else if ((isteq(n, ist("sec-websocket-accept")) &&
2200 h1m->flags & H1_MF_RESP) ||
2201 (isteq(n, ist("sec-websocket-key")) &&
2202 !(h1m->flags & H1_MF_RESP))) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002203 ws_key_found = 1;
2204 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02002205 else if (isteq(n, ist("te"))) {
2206 /* "te" may only be sent with "trailers" if this value
2207 * is present, otherwise it must be deleted.
2208 */
2209 v = istist(v, ist("trailers"));
2210 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2211 goto skip_hdr;
2212 v = ist("trailers");
2213 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002214
Christopher Faulet67d58092019-10-02 10:51:38 +02002215 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002216 if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) &&
2217 isteqi(n, h1c->px->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02002218 goto skip_hdr;
2219
Christopher Faulet98fbe952019-07-22 16:18:24 +02002220 /* Try to adjust the case of the header name */
2221 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2222 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002223 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002224 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002225 skip_hdr:
2226 h1m->state = H1_MSG_HDR_L2_LWS;
2227 break;
2228
Christopher Faulet94b2c762019-05-24 15:28:57 +02002229 case H1_MSG_LAST_LF:
2230 if (type != HTX_BLK_EOH)
2231 goto error;
2232 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002233 h1m->state = H1_MSG_LAST_LF;
2234 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002235 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002236 /* If the reply comes from haproxy while the request is
2237 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002238 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2239 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2240 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002241 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2242 /* T-E + C-L: force close */
2243 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2244 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2245 }
2246 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2247 /* T-E + HTTP/1.0: force close */
2248 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2249 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2250 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002251
2252 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002253 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002254 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002255 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002256 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002257 /* Try to adjust the case of the header name */
2258 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2259 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002260 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002261 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002262 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002263 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002264 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002265
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002266 if ((h1s->meth != HTTP_METH_CONNECT &&
2267 (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 +01002268 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002269 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002270 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002271 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2272 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002273 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002274 n = ist("transfer-encoding");
2275 v = ist("chunked");
2276 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2277 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002278 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002279 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002280 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002281 h1m->flags |= H1_MF_CHNK;
2282 }
2283
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002284 /* Now add the server name to a header (if requested) */
2285 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002286 !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002287 struct server *srv = objt_server(h1c->conn->target);
2288
2289 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002290 n = h1c->px->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002291 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002292
2293 /* Try to adjust the case of the header name */
2294 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2295 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002296 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002297 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002298 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002299 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002300 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2301 }
2302
Amaury Denoyellec1938232020-12-11 17:53:03 +01002303 /* Add websocket handshake key if needed */
2304 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) &&
2305 !ws_key_found) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002306 if (!(h1m->flags & H1_MF_RESP)) {
2307 /* generate a random websocket key
2308 * stored in the session to
2309 * verify it on the response side
2310 */
2311 h1_generate_random_ws_input_key(h1s->ws_key);
2312
2313 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2314 ist(h1s->ws_key),
2315 &tmp)) {
2316 goto full;
2317 }
2318 }
2319 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002320 /* add the response header key */
2321 char key[29];
2322 h1_calculate_ws_output_key(h1s->ws_key, key);
2323 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2324 ist(key),
2325 &tmp)) {
2326 goto full;
2327 }
2328 }
2329 }
2330
Christopher Faulet6b81df72019-10-01 22:08:43 +02002331 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2332 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2333
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002334 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002335 if (!chunk_memcat(&tmp, "\r\n", 2))
2336 goto full;
2337 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002338 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002339 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002340 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002341 if (!chunk_memcat(&tmp, "\r\n", 2))
2342 goto full;
2343 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002344 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002345 else if ((h1m->flags & H1_MF_RESP) &&
2346 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002347 if (!chunk_memcat(&tmp, "\r\n", 2))
2348 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002349 h1m_init_res(&h1s->res);
2350 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002351 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002352 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002353 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002354 else {
Christopher Faulet2eb52432022-04-07 10:29:38 +02002355 /* EOM flag is set or empty payload (C-L to 0) and it is the last block */
2356 if (htx_is_unique_blk(chn_htx, blk) &&
2357 ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002358 if (h1m->flags & H1_MF_CHNK) {
2359 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2360 goto full;
2361 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002362 else if (!chunk_memcat(&tmp, "\r\n", 2))
2363 goto full;
2364 goto done;
2365 }
2366 else if (!chunk_memcat(&tmp, "\r\n", 2))
2367 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002368 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002369 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002370 break;
2371
Christopher Faulet94b2c762019-05-24 15:28:57 +02002372 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002373 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002374 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002375 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2376 goto trailers;
2377
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002378 /* If the message is not chunked, never
2379 * add the last chunk. */
2380 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002381 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002382 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 +02002383 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002384 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002385 else if (type != HTX_BLK_DATA)
2386 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002387
2388 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 +02002389
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002390 /* It is the last block of this message. After this one,
2391 * only tunneled data may be forwarded. */
2392 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2393 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2394 last_data = 1;
2395 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002396
2397 if (vlen > count) {
2398 /* Get the maximum amount of data we can xferred */
2399 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002400 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002401 }
2402
Christopher Faulet140f1a52021-11-26 16:37:55 +01002403 if (h1m->state == H1_MSG_DATA) {
2404 if (h1m->flags & H1_MF_CLEN) {
2405 if (vlen > h1m->curr_len) {
2406 TRACE_ERROR("too much payload, more than announced",
2407 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2408 goto error;
2409 }
Christopher Faulet140f1a52021-11-26 16:37:55 +01002410 }
2411 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2412 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2413 goto skip_data;
2414 }
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002415 }
2416
Christopher Fauletd9233f02019-10-14 14:01:24 +02002417 chklen = 0;
2418 if (h1m->flags & H1_MF_CHNK) {
2419 chklen = b_room(&tmp);
2420 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2421 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2422 (chklen < 1048576) ? 5 : 8);
2423 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002424
2425 /* If it is the end of the chunked message (without EOT), reserve the
2426 * last chunk size */
2427 if (last_data)
2428 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002429 }
2430
2431 if (vlen + chklen > b_room(&tmp)) {
2432 /* too large for the buffer */
2433 if (chklen >= b_room(&tmp))
2434 goto full;
2435 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002436 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002437 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002438 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002439 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002440 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002441 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002442
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002443 /* Space already reserved, so it must succeed */
2444 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2445 goto error;
2446
Christopher Faulet6b81df72019-10-01 22:08:43 +02002447 if (h1m->state == H1_MSG_DATA)
2448 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002449 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002450 else
2451 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002452 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002453
2454 skip_data:
Christopher Faulet2eb52432022-04-07 10:29:38 +02002455 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Fauletb4eca0e2022-01-10 17:27:51 +01002456 h1m->curr_len -= vlen;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002457 if (!h1m->curr_len)
2458 last_data = 1;
2459 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002460 if (last_data)
2461 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002462 break;
2463
Christopher Faulet94b2c762019-05-24 15:28:57 +02002464 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002465 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002466 goto error;
2467 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002468 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002469
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002470 if (!(h1m->flags & H1_MF_CHNK))
2471 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002472
Christopher Faulete5596bf2020-12-02 16:13:22 +01002473 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2474 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 +01002475 if (type == HTX_BLK_EOT)
2476 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002477 break;
2478 }
2479
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002480 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002481 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002482 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002483 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2484 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002485 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002486 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002487 else { // HTX_BLK_TLR
2488 n = htx_get_blk_name(chn_htx, blk);
2489 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002490
2491 /* Try to adjust the case of the header name */
2492 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2493 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002494 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002495 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002496 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002497 break;
2498
Christopher Faulet94b2c762019-05-24 15:28:57 +02002499 case H1_MSG_DONE:
Christopher Fauletd934e8d2022-05-05 09:39:42 +02002500 /* If the message is not chunked, ignore
2501 * trailers. It may happen with H2 messages. */
2502 if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && !(h1m->flags & H1_MF_CHNK))
2503 break;
2504
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002505 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2506 goto error; /* For now return an error */
2507
Christopher Faulet94b2c762019-05-24 15:28:57 +02002508 done:
2509 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002510 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002511 h1s->flags |= H1S_F_TX_BLK;
2512 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002513 }
2514 else if ((h1m->flags & H1_MF_RESP) &&
2515 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2516 /* a successful reply to a CONNECT or a protocol switching is sent
2517 * to the client. Switch the response to tunnel mode.
2518 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002519 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002520 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 +01002521 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002522
Christopher Faulet10a86702021-04-07 14:18:11 +02002523 if (h1s->flags & H1S_F_RX_BLK) {
2524 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002525 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002526 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 +02002527 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002528
2529 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2530 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002531 break;
2532
Christopher Faulet9768c262018-10-22 09:34:31 +02002533 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002534 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002535 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002536 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002537 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002538 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002539 TRACE_ERROR("processing output error, set error on h1c/h1s",
2540 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 +01002541 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02002542 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002543
2544 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002545 total += vlen;
2546 count -= vlen;
2547 if (sz == vlen)
2548 blk = htx_remove_blk(chn_htx, blk);
2549 else {
2550 htx_cut_data_blk(chn_htx, blk, vlen);
2551 break;
2552 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002553 }
2554
Christopher Faulet9768c262018-10-22 09:34:31 +02002555 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002556 /* when the output buffer is empty, tmp shares the same area so that we
2557 * only have to update pointers and lengths.
2558 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002559 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2560 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002561 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002562 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002563
Willy Tarreau3815b222018-12-11 19:50:43 +01002564 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002565 out:
2566 if (!buf_room_for_htx_data(&h1c->obuf)) {
2567 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002568 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002569 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002570 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002571 /* Both the request and the response reached the DONE state. So set EOI
Willy Tarreau4596fe22022-05-17 19:07:51 +02002572 * flag on the stream connector. Most of time, the flag will already be set,
Christopher Fauletdea24742021-01-22 15:12:30 +01002573 * except for protocol upgrades. Report an error if data remains blocked
2574 * in the output buffer.
2575 */
2576 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2577 if (!htx_is_empty(chn_htx)) {
2578 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002579 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 +01002580 }
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02002581 se_fl_set(h1s->sd, SE_FL_EOI);
Christopher Fauletdea24742021-01-22 15:12:30 +01002582 }
2583
Christopher Faulet6b81df72019-10-01 22:08:43 +02002584 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002585 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002586
2587 full:
2588 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2589 h1c->flags |= H1C_F_OUT_FULL;
2590 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002591}
2592
Christopher Faulet51dbc942018-09-13 09:05:15 +02002593/*********************************************************/
2594/* functions below are I/O callbacks from the connection */
2595/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002596static void h1_wake_stream_for_recv(struct h1s *h1s)
2597{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002598 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002599 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002600 tasklet_wakeup(h1s->subs->tasklet);
2601 h1s->subs->events &= ~SUB_RETRY_RECV;
2602 if (!h1s->subs->events)
2603 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002604 }
2605}
2606static void h1_wake_stream_for_send(struct h1s *h1s)
2607{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002608 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002609 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002610 tasklet_wakeup(h1s->subs->tasklet);
2611 h1s->subs->events &= ~SUB_RETRY_SEND;
2612 if (!h1s->subs->events)
2613 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002614 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002615}
2616
Christopher Fauletad4daf62021-01-21 17:49:01 +01002617/* alerts the data layer following this sequence :
2618 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2619 * - if its subscribed to send, then it's woken up for send
2620 * - if it was subscribed to neither, its ->wake() callback is called
2621 */
2622static void h1_alert(struct h1s *h1s)
2623{
2624 if (h1s->subs) {
2625 h1_wake_stream_for_recv(h1s);
2626 h1_wake_stream_for_send(h1s);
2627 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002628 else if (h1s_sc(h1s) && h1s_sc(h1s)->app_ops->wake != NULL) {
Christopher Fauletad4daf62021-01-21 17:49:01 +01002629 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02002630 h1s_sc(h1s)->app_ops->wake(h1s_sc(h1s));
Christopher Fauletad4daf62021-01-21 17:49:01 +01002631 }
2632}
2633
Christopher Fauletc18fc232020-10-06 17:41:36 +02002634/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2635 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2636 * retryable errors (allocation error or buffer full). On success, the error is
2637 * copied in the output buffer.
2638*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002639static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002640{
2641 int rc = http_get_status_idx(h1c->errcode);
2642 int ret = 0;
2643
2644 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2645
2646 /* Verify if the error is mapped on /dev/null or any empty file */
2647 /// XXX: do a function !
2648 if (h1c->px->replies[rc] &&
2649 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2650 h1c->px->replies[rc]->body.errmsg &&
2651 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2652 /* Empty error, so claim a success */
2653 ret = 1;
2654 goto out;
2655 }
2656
2657 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2658 h1c->flags |= H1C_F_ERR_PENDING;
2659 goto out;
2660 }
2661
2662 if (!h1_get_buf(h1c, &h1c->obuf)) {
2663 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2664 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2665 goto out;
2666 }
Tim Duesterhus77508502022-03-15 13:11:06 +01002667 ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc]));
Christopher Fauletc18fc232020-10-06 17:41:36 +02002668 if (unlikely(ret <= 0)) {
2669 if (!ret) {
2670 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2671 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2672 goto out;
2673 }
2674 else {
2675 /* we cannot report this error, so claim a success */
2676 ret = 1;
2677 }
2678 }
2679 h1c->flags &= ~H1C_F_ERR_PENDING;
2680 out:
2681 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2682 return ret;
2683}
2684
2685/* Try to send a 500 internal error. It relies on h1_send_error to send the
2686 * error. This function takes care of incrementing stats and tracked counters.
2687 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002688static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002689{
2690 struct session *sess = h1c->conn->owner;
2691 int ret = 1;
2692
2693 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002694 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002695 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2696 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002697 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002698 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002699
2700 h1c->errcode = 500;
2701 ret = h1_send_error(h1c);
2702 sess_log(sess);
2703 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002704}
2705
Christopher Fauletb3230f72021-09-21 18:38:20 +02002706/* Try to send an error because of a parsing error. By default a 400 bad request
2707 * error is returned. But the status code may be specified by setting
2708 * h1c->errcode. It relies on h1_send_error to send the error. This function
2709 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002710 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002711static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002712{
2713 struct session *sess = h1c->conn->owner;
2714 int ret = 1;
2715
2716 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2717 goto end;
2718
2719 session_inc_http_req_ctr(sess);
2720 session_inc_http_err_ctr(sess);
2721 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002722 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2723 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002724 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002725 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002726
Christopher Fauletb3230f72021-09-21 18:38:20 +02002727 if (!h1c->errcode)
2728 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002729 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002730 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2731 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002732
2733 end:
2734 return ret;
2735}
2736
Christopher Faulet2eed8002020-12-07 11:26:13 +01002737/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2738 * the error. This function takes care of incrementing stats and tracked
2739 * counters.
2740 */
2741static int h1_handle_not_impl_err(struct h1c *h1c)
2742{
2743 struct session *sess = h1c->conn->owner;
2744 int ret = 1;
2745
2746 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2747 goto end;
2748
2749 session_inc_http_req_ctr(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002750 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002751 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2752 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002753 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002754 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002755
2756 h1c->errcode = 501;
2757 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002758 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2759 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002760
2761 end:
2762 return ret;
2763}
2764
Christopher Fauletc18fc232020-10-06 17:41:36 +02002765/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2766 * error. This function takes care of incrementing stats and tracked counters.
2767 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002768static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002769{
2770 struct session *sess = h1c->conn->owner;
2771 int ret = 1;
2772
2773 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2774 goto end;
2775
2776 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002777 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002778 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2779 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002780 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002781 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002782
2783 h1c->errcode = 408;
Christopher Faulet07e10de2021-07-26 09:42:49 +02002784 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2785 ret = h1_send_error(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002786 sess_log(sess);
2787 end:
2788 return ret;
2789}
2790
2791
Christopher Faulet51dbc942018-09-13 09:05:15 +02002792/*
2793 * Attempt to read data, and subscribe if none available
2794 */
2795static int h1_recv(struct h1c *h1c)
2796{
2797 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002798 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002799 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002800
Christopher Faulet6b81df72019-10-01 22:08:43 +02002801 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2802
2803 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2804 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002805 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002806 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002807
Christopher Fauletae635762020-09-21 11:47:16 +02002808 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2809 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002810 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002811 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002812
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002813 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2814 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002815 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002816 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002817 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002818
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002819 /*
2820 * If we only have a small amount of data, realign it,
2821 * it's probably cheaper than doing 2 recv() calls.
2822 */
2823 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002824 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002825
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002826 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002827 if (!h1c->h1s ||
2828 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2829 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002830 flags |= CO_RFL_READ_ONCE;
2831
Willy Tarreau45f2b892018-12-05 07:59:27 +01002832 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002833 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002834 if (h1c->flags & H1C_F_IN_FULL) {
2835 h1c->flags &= ~H1C_F_IN_FULL;
2836 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2837 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002838
2839 if (!b_data(&h1c->ibuf)) {
2840 /* try to pre-align the buffer like the rxbufs will be
2841 * to optimize memory copies.
2842 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002843 h1c->ibuf.head = sizeof(struct htx);
2844 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002845 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet41951ab2021-11-26 18:12:51 +01002846 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002847 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002848 if (max && !ret && h1_recv_allowed(h1c)) {
2849 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2850 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002851 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002852 else {
2853 h1_wake_stream_for_recv(h1c->h1s);
2854 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002855 }
2856
Christopher Faulet51dbc942018-09-13 09:05:15 +02002857 if (!b_data(&h1c->ibuf))
2858 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002859 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002860 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002861 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2862 }
2863
2864 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002865 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002866}
2867
2868
2869/*
2870 * Try to send data if possible
2871 */
2872static int h1_send(struct h1c *h1c)
2873{
2874 struct connection *conn = h1c->conn;
2875 unsigned int flags = 0;
2876 size_t ret;
2877 int sent = 0;
2878
Christopher Faulet6b81df72019-10-01 22:08:43 +02002879 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2880
2881 if (conn->flags & CO_FL_ERROR) {
2882 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002883 b_reset(&h1c->obuf);
2884 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002885 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002886
2887 if (!b_data(&h1c->obuf))
2888 goto end;
2889
Christopher Faulet46230362019-10-17 16:04:20 +02002890 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002891 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002892 if (h1c->flags & H1C_F_CO_STREAMER)
2893 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002894
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002895 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002896 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002897 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002898 if (h1c->flags & H1C_F_OUT_FULL) {
2899 h1c->flags &= ~H1C_F_OUT_FULL;
2900 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2901 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01002902 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002903 b_del(&h1c->obuf, ret);
2904 sent = 1;
2905 }
2906
Christopher Faulet145aa472018-12-06 10:56:20 +01002907 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002908 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002909 /* error or output closed, nothing to send, clear the buffer to release it */
2910 b_reset(&h1c->obuf);
2911 }
2912
Christopher Faulet51dbc942018-09-13 09:05:15 +02002913 end:
Christopher Fauletc17c31c2022-02-01 18:25:06 +01002914 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002915 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002916
Christopher Faulet51dbc942018-09-13 09:05:15 +02002917 /* We're done, no more to send */
2918 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002919 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002920 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002921 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002922 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002923 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002924 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002925 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002926 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2927 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002928 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002929 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002930
Christopher Faulet6b81df72019-10-01 22:08:43 +02002931 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002932 return sent;
2933}
2934
Christopher Faulet51dbc942018-09-13 09:05:15 +02002935/* callback called on any event by the connection handler.
2936 * It applies changes and returns zero, or < 0 if it wants immediate
2937 * destruction of the connection.
2938 */
2939static int h1_process(struct h1c * h1c)
2940{
2941 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002942 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002943
Christopher Faulet6b81df72019-10-01 22:08:43 +02002944 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2945
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002946 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002947 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Fauletab7389d2021-09-16 08:16:23 +02002948 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2949 !(h1c->flags & (H1C_F_IN_SALLOC|H1C_F_ST_ERROR))) { /* No allocation failure on the stream rxbuf and no ERROR on the H1C */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002950 struct buffer *buf;
2951 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002952
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002953 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2954 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002955 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002956
2957 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002958 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002959 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2960 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002961 /* Try to match H2 preface before parsing the request headers. */
2962 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2963 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002964 if (h1c->flags & H1C_F_ST_ATTACHED) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02002965 /* Force the REOS here to be sure to release the SC.
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002966 Here ATTACHED implies !READY, and h1s defined
2967 */
2968 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2969 h1s->flags |= H1S_F_REOS;
2970 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002971 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002972 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002973 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002974 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002975
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002976 /* Create the H1 stream if not already there */
2977 if (!h1s) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002978 h1s = h1c_frt_stream_new(h1c, NULL, h1c->conn->owner);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002979 if (!h1s) {
2980 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002981 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002982 goto no_parsing;
2983 }
2984 }
2985
2986 if (h1s->sess->t_idle == -1)
2987 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2988
2989 /* Get the stream rxbuf */
2990 buf = h1_get_buf(h1c, &h1s->rxbuf);
2991 if (!buf) {
2992 h1c->flags |= H1C_F_IN_SALLOC;
2993 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2994 return 0;
2995 }
2996
2997 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01002998 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002999 h1_release_buf(h1c, &h1s->rxbuf);
3000 h1_set_idle_expiration(h1c);
3001
3002 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003003 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003004 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003005 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet26a26432021-01-27 11:27:50 +01003006 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003007 }
3008 else if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02003009 h1_handle_parsing_error(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003010 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003011 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003012 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01003013 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
3014 h1_handle_not_impl_err(h1c);
3015 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003016 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01003017 }
Christopher Fauletae635762020-09-21 11:47:16 +02003018 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003019 h1_send(h1c);
3020
Christopher Faulet75308302021-11-15 14:51:37 +01003021 /* H1 connection must be released ASAP if:
3022 * - an error occurred on the connection or the H1C or
3023 * - a read0 was received or
3024 * - a silent shutdown was emitted and all outgoing data sent
3025 */
3026 if ((conn->flags & CO_FL_ERROR) ||
3027 conn_xprt_read0_pending(conn) ||
3028 (h1c->flags & H1C_F_ST_ERROR) ||
3029 ((h1c->flags & H1C_F_ST_SILENT_SHUT) && !b_data(&h1c->obuf))) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003030 if (!(h1c->flags & H1C_F_ST_READY)) {
Willy Tarreau4596fe22022-05-17 19:07:51 +02003031 /* No stream connector or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003032 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet75308302021-11-15 14:51:37 +01003033 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN))) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02003034 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003035 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003036 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003037 }
3038
3039 /* Handle pending error, if any (only possible on frontend connection) */
3040 if (h1c->flags & H1C_F_ERR_PENDING) {
3041 BUG_ON(h1c->flags & H1C_F_IS_BACK);
3042 if (h1_send_error(h1c))
3043 h1_send(h1c);
3044 }
Christopher Fauletae635762020-09-21 11:47:16 +02003045
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003046 /* If there is some pending outgoing data or error, just wait */
3047 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
3048 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01003049
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003050 /* Otherwise we can release the H1 connection */
3051 goto release;
3052 }
3053 else {
Willy Tarreau4596fe22022-05-17 19:07:51 +02003054 /* Here there is still a H1 stream with a stream connector.
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003055 * Report the connection state at the stream level
3056 */
3057 if (conn_xprt_read0_pending(conn)) {
3058 h1s->flags |= H1S_F_REOS;
3059 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
3060 }
Willy Tarreaucad42a72022-08-29 10:22:56 +02003061 if ((h1c->flags & H1C_F_ST_ERROR) || ((conn->flags & CO_FL_ERROR) &&
3062 (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf))))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003063 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003064 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01003065 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003066 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003067 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003068
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003069 if (!b_data(&h1c->ibuf))
3070 h1_release_buf(h1c, &h1c->ibuf);
3071
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003072 /* Check if a soft-stop is in progress.
3073 * Release idling front connection if this is the case.
3074 */
3075 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003076 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
William Dauchya9dd9012022-01-05 22:53:24 +01003077 if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) &&
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003078 h1c->flags & H1C_F_WAIT_NEXT_REQ) {
3079
3080 int send_close = 1;
3081 /* If a close-spread-time option is set, we want to avoid
3082 * closing all the active HTTP2 connections at once so we add a
3083 * random factor that will spread the closing.
3084 */
3085 if (tick_isset(global.close_spread_end)) {
3086 int remaining_window = tick_remain(now_ms, global.close_spread_end);
3087 if (remaining_window) {
3088 /* This should increase the closing rate the
3089 * further along the window we are.
3090 */
3091 send_close = (remaining_window <= statistical_prng_range(global.close_spread_time));
3092 }
3093 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02003094 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
3095 send_close = 0; /* let the client close his connection himself */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003096 if (send_close)
3097 goto release;
3098 }
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003099 }
3100 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003101
3102 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
3103 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
3104 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003105 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003106
Christopher Faulet47365272018-10-31 17:40:50 +01003107 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02003108 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003109 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003110 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01003111
3112 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003113 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003114 /* Don't release the H1 connection right now, we must destroy the
Willy Tarreaue68bc612022-05-27 11:23:05 +02003115 * attached SC first. Here, the H1C must not be READY */
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003116 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
3117
3118 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003119 se_fl_set(h1s->sd, SE_FL_EOS);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003120 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003121 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003122 h1_alert(h1s);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003123 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003124 }
3125 else {
3126 h1_release(h1c);
3127 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
3128 }
Christopher Faulet539e0292018-11-19 10:40:09 +01003129 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003130}
3131
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003132struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003133{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003134 struct connection *conn;
3135 struct tasklet *tl = (struct tasklet *)t;
3136 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003137 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003138 int ret = 0;
3139
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003140 if (state & TASK_F_USR1) {
3141 /* the tasklet was idling on an idle connection, it might have
3142 * been stolen, let's be careful!
3143 */
3144 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3145 if (tl->context == NULL) {
3146 /* The connection has been taken over by another thread,
3147 * we're no longer responsible for it, so just free the
3148 * tasklet, and do nothing.
3149 */
3150 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3151 tasklet_free(tl);
3152 return NULL;
3153 }
3154 conn = h1c->conn;
3155 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003156
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003157 /* Remove the connection from the list, to be sure nobody attempts
3158 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003159 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003160 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3161 if (conn_in_list)
3162 conn_delete_from_tree(&conn->hash_node->node);
3163
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003164 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003165 } else {
3166 /* we're certain the connection was not in an idle list */
3167 conn = h1c->conn;
3168 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
3169 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003170 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003171
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003172 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003173 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003174 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003175 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003176 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003177 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01003178
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003179 /* If we were in an idle list, we want to add it back into it,
3180 * unless h1_process() returned -1, which mean it has destroyed
3181 * the connection (testing !ret is enough, if h1_process() wasn't
3182 * called then ret will be 0 anyway.
3183 */
Willy Tarreau74163142021-03-13 11:30:19 +01003184 if (ret < 0)
3185 t = NULL;
3186
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003187 if (!ret && conn_in_list) {
3188 struct server *srv = objt_server(conn->target);
3189
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003190 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003191 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003192 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003193 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003194 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003195 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003196 }
Willy Tarreau74163142021-03-13 11:30:19 +01003197 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003198}
3199
Christopher Faulet51dbc942018-09-13 09:05:15 +02003200static int h1_wake(struct connection *conn)
3201{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003202 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01003203 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003204
Christopher Faulet6b81df72019-10-01 22:08:43 +02003205 TRACE_POINT(H1_EV_H1C_WAKE, conn);
3206
Christopher Faulet539e0292018-11-19 10:40:09 +01003207 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01003208 ret = h1_process(h1c);
3209 if (ret == 0) {
3210 struct h1s *h1s = h1c->h1s;
3211
Christopher Fauletad4daf62021-01-21 17:49:01 +01003212 if (h1c->flags & H1C_F_ST_ATTACHED)
3213 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003214 }
3215 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003216}
3217
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003218/* Connection timeout management. The principle is that if there's no receipt
3219 * nor sending for a certain amount of time, the connection is closed.
3220 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003221struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003222{
3223 struct h1c *h1c = context;
3224 int expired = tick_is_expired(t->expire, now_ms);
3225
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003226 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003227
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003228 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003229 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003230 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003231
3232 /* Somebody already stole the connection from us, so we should not
3233 * free it, we just have to free the task.
3234 */
3235 if (!t->context) {
3236 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003237 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003238 goto do_leave;
3239 }
3240
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003241 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003242 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003243 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003244 return t;
3245 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003246
Willy Tarreau4596fe22022-05-17 19:07:51 +02003247 /* If a stream connector is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003248 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003249 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003250 if (h1c->flags & H1C_F_ST_READY) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003251 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003252 t->expire = TICK_ETERNITY;
Willy Tarreaue68bc612022-05-27 11:23:05 +02003253 TRACE_DEVEL("leaving (SC still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003254 return t;
3255 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003256
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003257 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003258 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
3259 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003260 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003261 if (h1_handle_req_tout(h1c))
3262 h1_send(h1c);
3263 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
3264 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003265 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003266 return t;
3267 }
3268 }
3269
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003270 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003271 /* Don't release the H1 connection right now, we must destroy the
Willy Tarreaue68bc612022-05-27 11:23:05 +02003272 * attached SC first. Here, the H1C must not be READY */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003273 se_fl_set(h1c->h1s->sd, SE_FL_EOS | SE_FL_ERROR);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003274 h1_alert(h1c->h1s);
3275 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003276 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue68bc612022-05-27 11:23:05 +02003277 TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003278 return t;
3279 }
3280
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003281 /* We're about to destroy the connection, so make sure nobody attempts
3282 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003283 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003284 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003285 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003286
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003287 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003288 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003289
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003290 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003291 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003292
3293 if (!h1c) {
3294 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003295 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003296 return NULL;
3297 }
3298
3299 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003300 h1_release(h1c);
3301 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003302 return NULL;
3303}
3304
Christopher Faulet51dbc942018-09-13 09:05:15 +02003305/*******************************************/
3306/* functions below are used by the streams */
3307/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003308
Christopher Faulet51dbc942018-09-13 09:05:15 +02003309/*
3310 * Attach a new stream to a connection
3311 * (Used for outgoing connections)
3312 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003313static int h1_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003314{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003315 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003316 struct h1s *h1s;
3317
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003318 /* this connection is no more idle (if it was at all) */
3319 h1c->flags &= ~H1C_F_ST_SILENT_SHUT;
3320
Christopher Faulet6b81df72019-10-01 22:08:43 +02003321 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003322 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003323 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3324 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003325 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003326
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003327 h1s = h1c_bck_stream_new(h1c, sd->sc, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003328 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003329 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3330 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003331 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003332
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003333 /* the connection is not idle anymore, let's mark this */
3334 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003335 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003336
Christopher Faulet6b81df72019-10-01 22:08:43 +02003337 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulete00ad352021-12-16 14:44:31 +01003338 return 0;
Christopher Faulet26a26432021-01-27 11:27:50 +01003339 err:
Christopher Faulet26a26432021-01-27 11:27:50 +01003340 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 +01003341 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003342}
3343
Willy Tarreau4596fe22022-05-17 19:07:51 +02003344/* Retrieves a valid stream connector from this connection, or returns NULL.
3345 * For this mux, it's easy as we can only store a single stream connector.
Christopher Faulet51dbc942018-09-13 09:05:15 +02003346 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003347static struct stconn *h1_get_first_sc(const struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003348{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003349 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003350 struct h1s *h1s = h1c->h1s;
3351
3352 if (h1s)
Willy Tarreau97b4d3b2022-05-18 07:27:14 +02003353 return h1s_sc(h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003354
3355 return NULL;
3356}
3357
Christopher Faulet73c12072019-04-08 11:23:22 +02003358static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003359{
Christopher Faulet73c12072019-04-08 11:23:22 +02003360 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003361
Christopher Faulet6b81df72019-10-01 22:08:43 +02003362 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet7c452cc2022-04-14 11:08:26 +02003363 if (!h1c->h1s || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003364 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003365}
3366
3367/*
3368 * Detach the stream from the connection and possibly release the connection.
3369 */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003370static void h1_detach(struct sedesc *sd)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003371{
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003372 struct h1s *h1s = sd->se;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003373 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003374 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003375 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003376
Christopher Faulet6b81df72019-10-01 22:08:43 +02003377 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3378
Christopher Faulet6b81df72019-10-01 22:08:43 +02003379 if (!h1s) {
3380 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003381 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003382 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003383
Olivier Houchardf502aca2018-12-14 19:42:40 +01003384 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003385 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003386
Christopher Faulet42849b02020-10-05 11:35:17 +02003387 sess->accept_date = date;
3388 sess->tv_accept = now;
3389 sess->t_handshake = 0;
3390 sess->t_idle = -1;
3391
Olivier Houchard8a786902018-12-15 16:05:40 +01003392 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3393 h1s_destroy(h1s);
3394
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003395 if ((h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_IDLE)) == (H1C_F_IS_BACK|H1C_F_ST_IDLE)) {
Willy Tarreau4d1ff112022-09-01 15:49:23 +02003396 /* this connection may be killed at any moment, we want it to
3397 * die "cleanly" (i.e. only an RST).
3398 */
3399 h1c->flags |= H1C_F_ST_SILENT_SHUT;
3400
Christopher Fauletf1204b82019-07-19 14:51:06 +02003401 /* If there are any excess server data in the input buffer,
3402 * release it and close the connection ASAP (some data may
3403 * remain in the output buffer). This happens if a server sends
3404 * invalid responses. So in such case, we don't want to reuse
3405 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003406 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003407 if (b_data(&h1c->ibuf)) {
3408 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003409 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003410 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003411 goto release;
3412 }
Christopher Faulet03627242019-07-19 11:34:08 +02003413
Christopher Faulet08016ab2020-07-01 16:10:06 +02003414 if (h1c->conn->flags & CO_FL_PRIVATE) {
3415 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003416 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3417 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003418 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003419 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003420 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003421 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003422 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003423 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003424 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3425 goto end;
3426 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003427 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003428 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003429 if (h1c->conn->owner == sess)
3430 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003431
3432 /* mark that the tasklet may lose its context to another thread and
3433 * that the handler needs to check it under the idle conns lock.
3434 */
3435 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003436 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003437 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3438
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003439 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003440 /* The server doesn't want it, let's kill the connection right away */
3441 h1c->conn->mux->destroy(h1c);
3442 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3443 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003444 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003445 /* At this point, the connection has been added to the
3446 * server idle list, so another thread may already have
3447 * hijacked it, so we can't do anything with it.
3448 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003449 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003450 }
3451 }
3452
Christopher Fauletf1204b82019-07-19 14:51:06 +02003453 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003454 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003455 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02003456 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003457 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003458 !h1c->conn->owner) {
3459 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003460 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003461 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003462 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003463 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003464 /* If we have a new request, process it immediately or
3465 * subscribe for reads waiting for new data
3466 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003467 if (unlikely(b_data(&h1c->ibuf))) {
3468 if (h1_process(h1c) == -1)
3469 goto end;
3470 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003471 else
3472 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3473 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003474 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003475 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003476 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003477 end:
3478 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003479}
3480
3481
Willy Tarreau000d63c2022-05-27 10:39:17 +02003482static void h1_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003483{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003484 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet7f366362019-04-08 10:51:20 +02003485 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003486
3487 if (!h1s)
3488 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003489 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003490
Christopher Faulet99293b02021-11-10 10:30:15 +01003491 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003492
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003493 if (se_fl_test(h1s->sd, SE_FL_SHR))
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003494 goto end;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003495 if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003496 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
3497 goto do_shutr;
3498 }
3499 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3500 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003501 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003502 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003503
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003504 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02003505 /* Here attached is implicit because there is SC */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003506 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3507 goto end;
3508 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003509 if (h1s->flags & H1S_F_WANT_KAL) {
3510 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003511 goto end;
3512 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003513
Christopher Faulet7f366362019-04-08 10:51:20 +02003514 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003515 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003516 if (se_fl_test(h1s->sd, SE_FL_SHR))
Christopher Faulet6b81df72019-10-01 22:08:43 +02003517 goto end;
Christopher Faulet897d6122021-12-17 17:28:35 +01003518
3519 if (conn_xprt_ready(h1c->conn) && h1c->conn->xprt->shutr)
Christopher Faulet07976562022-03-31 11:05:05 +02003520 h1c->conn->xprt->shutr(h1c->conn, h1c->conn->xprt_ctx, (mode == CO_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003521 end:
3522 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003523}
3524
Willy Tarreau000d63c2022-05-27 10:39:17 +02003525static void h1_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003526{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003527 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003528 struct h1c *h1c;
3529
3530 if (!h1s)
3531 return;
3532 h1c = h1s->h1c;
3533
Christopher Faulet99293b02021-11-10 10:30:15 +01003534 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003535
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003536 if (se_fl_test(h1s->sd, SE_FL_SHW))
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003537 goto end;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003538 if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003539 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003540 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003541 }
3542 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3543 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3544 goto do_shutw;
3545 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003546
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003547 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
Willy Tarreaue68bc612022-05-27 11:23:05 +02003548 /* Here attached is implicit because there is SC */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003549 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3550 goto end;
3551 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003552 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3553 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003554 goto end;
3555 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003556
Christopher Faulet7f366362019-04-08 10:51:20 +02003557 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003558 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet07976562022-03-31 11:05:05 +02003559 if (mode != CO_SHW_NORMAL)
Christopher Fauleta85c5222021-10-27 15:36:38 +02003560 h1c->flags |= H1C_F_ST_SILENT_SHUT;
3561
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003562 if (!b_data(&h1c->obuf))
Christopher Faulet897d6122021-12-17 17:28:35 +01003563 h1_shutw_conn(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003564 end:
3565 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003566}
3567
Christopher Fauleta85c5222021-10-27 15:36:38 +02003568static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003569{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003570 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003571
Willy Tarreau62592ad2021-03-26 09:09:42 +01003572 if (conn->flags & CO_FL_SOCK_WR_SH)
3573 return;
3574
Christopher Fauleta85c5222021-10-27 15:36:38 +02003575 TRACE_ENTER(H1_EV_H1C_END, conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003576 conn_xprt_shutw(conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02003577 conn_sock_shutw(conn, (h1c && !(h1c->flags & H1C_F_ST_SILENT_SHUT)));
3578 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003579}
3580
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003581/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3582 * The <es> pointer is not allowed to differ from the one passed to the
3583 * subscribe() call. It always returns zero.
3584 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003585static int h1_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003586{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003587 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003588
3589 if (!h1s)
3590 return 0;
3591
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003592 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003593 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003594
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003595 es->events &= ~event_type;
3596 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003597 h1s->subs = NULL;
3598
3599 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003600 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003601
3602 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003603 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003604
Christopher Faulet51dbc942018-09-13 09:05:15 +02003605 return 0;
3606}
3607
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003608/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3609 * event subscriber <es> is not allowed to change from a previous call as long
3610 * as at least one event is still subscribed. The <event_type> must only be a
3611 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
Willy Tarreau000d63c2022-05-27 10:39:17 +02003612 * the stream connector <sc> was already detached, in which case it will return
Willy Tarreau4596fe22022-05-17 19:07:51 +02003613 * -1.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003614 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003615static int h1_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003616{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003617 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51bb1852019-09-04 10:22:34 +02003618 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003619
3620 if (!h1s)
3621 return -1;
3622
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003623 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003624 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003625
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003626 es->events |= event_type;
3627 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003628
3629 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003630 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003631
3632
Christopher Faulet6b81df72019-10-01 22:08:43 +02003633 if (event_type & SUB_RETRY_SEND) {
3634 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003635 /*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003636 * If the stconn attempts to subscribe, and the
Christopher Faulet6b81df72019-10-01 22:08:43 +02003637 * mux isn't subscribed to the connection, then it
3638 * probably means the connection wasn't established
3639 * yet, so we have to subscribe.
3640 */
3641 h1c = h1s->h1c;
3642 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3643 h1c->conn->xprt->subscribe(h1c->conn,
3644 h1c->conn->xprt_ctx,
3645 SUB_RETRY_SEND,
3646 &h1c->wait_event);
3647 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003648 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003649}
3650
Christopher Faulet564e39c2021-09-21 15:50:55 +02003651/* Called from the upper layer, to receive data.
3652 *
3653 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3654 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3655 * means the caller wants to flush input data (from the mux buffer and the
3656 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3657 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3658 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3659 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3660 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3661 * copy as much data as possible.
3662 */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003663static size_t h1_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003664{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003665 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet539e0292018-11-19 10:40:09 +01003666 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003667 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003668 size_t ret = 0;
3669
Willy Tarreau022e5e52020-09-10 09:33:15 +02003670 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003671
3672 /* Do nothing for now if not READY */
3673 if (!(h1c->flags & H1C_F_ST_READY)) {
3674 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3675 goto end;
3676 }
3677
Christopher Faulet539e0292018-11-19 10:40:09 +01003678 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003679 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003680 else
3681 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003682
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003683 if ((flags & CO_RFL_BUF_FLUSH) && se_fl_test(h1s->sd, SE_FL_MAY_SPLICE)) {
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003684 h1c->flags |= H1C_F_WANT_SPLICE;
3685 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 +02003686 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003687 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003688 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 +01003689 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003690 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003691
3692 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003693 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003694 return ret;
3695}
3696
3697
3698/* Called from the upper layer, to send data */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003699static size_t h1_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003700{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003701 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003702 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003703 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003704
3705 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003706 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003707 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003708
Willy Tarreau022e5e52020-09-10 09:33:15 +02003709 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003710
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003711 /* If we're not connected yet, or we're waiting for a handshake, stop
3712 * now, as we don't want to remove everything from the channel buffer
3713 * before we're sure we can send it.
3714 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003715 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003716 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003717 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003718 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003719
Christopher Fauletdea24742021-01-22 15:12:30 +01003720 if (h1c->flags & H1C_F_ST_ERROR) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003721 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet26a26432021-01-27 11:27:50 +01003722 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 +01003723 return 0;
3724 }
3725
Christopher Faulet46230362019-10-17 16:04:20 +02003726 /* Inherit some flags from the upper layer */
3727 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3728 if (flags & CO_SFL_MSG_MORE)
3729 h1c->flags |= H1C_F_CO_MSG_MORE;
3730 if (flags & CO_SFL_STREAMER)
3731 h1c->flags |= H1C_F_CO_STREAMER;
3732
Christopher Fauletc31872f2019-06-04 22:09:36 +02003733 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003734 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003735
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003736 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003737 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003738 else
3739 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003740
Christopher Faulet372b38f2022-07-08 15:20:31 +02003741 if ((h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)))
3742 break;
3743
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003744 if ((count - ret) > 0)
3745 h1c->flags |= H1C_F_CO_MSG_MORE;
3746
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003747 if (!ret)
3748 break;
3749 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003750 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003751 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003752 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003753 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003754
Willy Tarreaucad42a72022-08-29 10:22:56 +02003755 if ((h1c->flags & H1C_F_ST_ERROR) || ((h1c->conn->flags & CO_FL_ERROR) &&
3756 (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf)))) {
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003757 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet26a26432021-01-27 11:27:50 +01003758 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 +01003759 }
3760
Christopher Faulet7a145d62020-08-05 11:31:16 +02003761 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003762 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003763 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003764}
3765
Willy Tarreaue5733232019-05-22 19:24:06 +02003766#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003767/* Send and get, using splicing */
Willy Tarreau000d63c2022-05-27 10:39:17 +02003768static int h1_rcv_pipe(struct stconn *sc, struct pipe *pipe, unsigned int count)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003769{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003770 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003771 struct h1c *h1c = h1s->h1c;
3772 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003773 int ret = 0;
3774
Christopher Faulet897d6122021-12-17 17:28:35 +01003775 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003776
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003777 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003778 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003779 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 +02003780 goto end;
3781 }
3782
Christopher Fauletcf307562021-07-26 10:49:39 +02003783 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003784 if (h1s_data_pending(h1s)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003785 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003786 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003787 }
3788
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003789 if (!h1_recv_allowed(h1c)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003790 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet0060be92020-07-03 15:02:25 +02003791 goto end;
3792 }
3793
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003794 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003795 count = h1m->curr_len;
Christopher Faulet897d6122021-12-17 17:28:35 +01003796 ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003797 if (ret >= 0) {
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003798 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003799 if (ret > h1m->curr_len) {
3800 h1s->flags |= H1S_F_PARSING_ERROR;
3801 h1c->flags |= H1C_F_ST_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003802 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003803 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003804 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 +01003805 goto end;
3806 }
3807 h1m->curr_len -= ret;
3808 if (!h1m->curr_len) {
3809 h1m->state = H1_MSG_DONE;
3810 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003811 TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003812 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003813 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003814 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
3815 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret);
Christopher Faulete18777b2019-04-16 16:46:36 +02003816 }
3817
Christopher Faulet1be55f92018-10-02 15:59:23 +02003818 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003819 if (conn_xprt_read0_pending(h1c->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003820 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003821 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003822 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003823 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003824
Christopher Faulet94d35102021-04-09 11:58:49 +02003825 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003826 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003827 se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE);
Christopher Faulet94d35102021-04-09 11:58:49 +02003828 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003829 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s);
3830 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet94d35102021-04-09 11:58:49 +02003831 }
Christopher Faulet27182292020-07-03 15:08:49 +02003832 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003833
Christopher Faulet897d6122021-12-17 17:28:35 +01003834 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003835 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003836}
3837
Willy Tarreau000d63c2022-05-27 10:39:17 +02003838static int h1_snd_pipe(struct stconn *sc, struct pipe *pipe)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003839{
Willy Tarreau000d63c2022-05-27 10:39:17 +02003840 struct h1s *h1s = __sc_mux_strm(sc);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003841 struct h1c *h1c = h1s->h1c;
3842 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003843 int ret = 0;
3844
Christopher Faulet897d6122021-12-17 17:28:35 +01003845 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003846
Christopher Faulet140f1a52021-11-26 16:37:55 +01003847 if (b_data(&h1c->obuf)) {
3848 if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003849 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s);
3850 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003851 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003852 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003853 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003854
Christopher Faulet897d6122021-12-17 17:28:35 +01003855 ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe);
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003856 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003857 if (ret > h1m->curr_len) {
3858 h1s->flags |= H1S_F_PROCESSING_ERROR;
3859 h1c->flags |= H1C_F_ST_ERROR;
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003860 se_fl_set(h1s->sd, SE_FL_ERROR);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003861 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003862 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 +01003863 goto end;
3864 }
3865 h1m->curr_len -= ret;
3866 if (!h1m->curr_len) {
3867 h1m->state = H1_MSG_DONE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003868 TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003869 }
3870 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003871 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
3872 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret);
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003873
3874 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003875 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003876 return ret;
3877}
3878#endif
3879
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003880static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3881{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003882 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003883 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003884
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003885 switch (mux_ctl) {
3886 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003887 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003888 ret |= MUX_STATUS_READY;
3889 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003890 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003891 if (output)
3892 *((int *)output) = h1c->errcode;
3893 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3894 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3895 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3896 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003897 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003898 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003899 default:
3900 return -1;
3901 }
3902}
3903
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003904/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003905static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003906{
3907 struct h1c *h1c = conn->ctx;
3908 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003909 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003910
Christopher Fauletf376a312019-01-04 15:16:06 +01003911 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3912 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003913 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3914 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3915 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3916 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3917
3918 if (h1s) {
3919 char *method;
3920
3921 if (h1s->meth < HTTP_METH_OTHER)
3922 method = http_known_methods[h1s->meth].ptr;
3923 else
3924 method = "UNKNOWN";
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003925 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .sd.flg=0x%x .req.state=%s .res.state=%s"
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003926 " .meth=%s status=%d",
Willy Tarreau1a0d9ac2022-05-27 16:12:05 +02003927 h1s, h1s->flags, se_fl_get(h1s->sd),
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003928 h1m_state_str(h1s->req.state),
3929 h1m_state_str(h1s->res.state), method, h1s->status);
Christopher Faulet186367f2022-05-30 08:45:15 +02003930
3931 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(h1s->sd));
3932 if (!se_fl_test(h1s->sd, SE_FL_ORPHAN))
3933 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
3934 h1s_sc(h1s)->flags, h1s_sc(h1s)->app);
3935
Willy Tarreau150c4f82021-01-20 17:05:58 +01003936 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3937 if (h1s->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01003938 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3939 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3940 h1s->subs->tasklet->calls,
3941 h1s->subs->tasklet->context);
3942 if (h1s->subs->tasklet->calls >= 1000000)
3943 ret = 1;
3944 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3945 chunk_appendf(&trash, ")");
Willy Tarreau150c4f82021-01-20 17:05:58 +01003946 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003947 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003948 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003949}
3950
3951
3952/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3953static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3954{
3955 struct h1_hdr_entry *entry;
3956
3957 /* Be sure there is a non-empty <to> */
3958 if (!strlen(to)) {
3959 memprintf(err, "expect <to>");
3960 return -1;
3961 }
3962
3963 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003964 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003965 memprintf(err, "<from> and <to> must not differ execpt the case");
3966 return -1;
3967 }
3968
3969 /* Be sure <from> does not already existsin the tree */
3970 if (ebis_lookup(&hdrs_map.map, from)) {
3971 memprintf(err, "duplicate entry '%s'", from);
3972 return -1;
3973 }
3974
3975 /* Create the entry and insert it in the tree */
3976 entry = malloc(sizeof(*entry));
3977 if (!entry) {
3978 memprintf(err, "out of memory");
3979 return -1;
3980 }
3981
3982 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01003983 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003984 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003985 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003986 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003987 free(entry);
3988 memprintf(err, "out of memory");
3989 return -1;
3990 }
3991 ebis_insert(&hdrs_map.map, &entry->node);
3992 return 0;
3993}
3994
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003995/* Migrate the the connection to the current thread.
3996 * Return 0 if successful, non-zero otherwise.
3997 * Expected to be called with the old thread lock held.
3998 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003999static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004000{
4001 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004002 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004003
4004 if (fd_takeover(conn->handle.fd, conn) != 0)
4005 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004006
4007 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4008 /* We failed to takeover the xprt, even if the connection may
4009 * still be valid, flag it as error'd, as we have already
4010 * taken over the fd, and wake the tasklet, so that it will
4011 * destroy it.
4012 */
4013 conn->flags |= CO_FL_ERROR;
4014 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
4015 return -1;
4016 }
4017
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004018 if (h1c->wait_event.events)
4019 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
4020 h1c->wait_event.events, &h1c->wait_event);
4021 /* To let the tasklet know it should free itself, and do nothing else,
4022 * set its context to NULL.
4023 */
4024 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004025 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02004026
4027 task = h1c->task;
4028 if (task) {
4029 task->context = NULL;
4030 h1c->task = NULL;
4031 __ha_barrier_store();
4032 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004033
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004034 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004035 if (!h1c->task) {
4036 h1_release(h1c);
4037 return -1;
4038 }
4039 h1c->task->process = h1_timeout_task;
4040 h1c->task->context = h1c;
4041 }
4042 h1c->wait_event.tasklet = tasklet_new();
4043 if (!h1c->wait_event.tasklet) {
4044 h1_release(h1c);
4045 return -1;
4046 }
4047 h1c->wait_event.tasklet->process = h1_io_cb;
4048 h1c->wait_event.tasklet->context = h1c;
4049 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
4050 SUB_RETRY_RECV, &h1c->wait_event);
4051
4052 return 0;
4053}
4054
4055
Christopher Faulet98fbe952019-07-22 16:18:24 +02004056static void h1_hdeaders_case_adjust_deinit()
4057{
4058 struct ebpt_node *node, *next;
4059 struct h1_hdr_entry *entry;
4060
4061 node = ebpt_first(&hdrs_map.map);
4062 while (node) {
4063 next = ebpt_next(node);
4064 ebpt_delete(node);
4065 entry = container_of(node, struct h1_hdr_entry, node);
4066 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004067 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004068 free(entry);
4069 node = next;
4070 }
4071 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004072}
4073
Christopher Faulet98fbe952019-07-22 16:18:24 +02004074static int cfg_h1_headers_case_adjust_postparser()
4075{
4076 FILE *file = NULL;
4077 char *c, *key_beg, *key_end, *value_beg, *value_end;
4078 char *err;
4079 int rc, line = 0, err_code = 0;
4080
4081 if (!hdrs_map.name)
4082 goto end;
4083
4084 file = fopen(hdrs_map.name, "r");
4085 if (!file) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004086 ha_alert("h1-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004087 hdrs_map.name);
4088 err_code |= ERR_ALERT | ERR_FATAL;
4089 goto end;
4090 }
4091
4092 /* now parse all lines. The file may contain only two header name per
4093 * line, separated by spaces. All heading and trailing spaces will be
4094 * ignored. Lines starting with a # are ignored.
4095 */
4096 while (fgets(trash.area, trash.size, file) != NULL) {
4097 line++;
4098 c = trash.area;
4099
4100 /* strip leading spaces and tabs */
4101 while (*c == ' ' || *c == '\t')
4102 c++;
4103
4104 /* ignore emptu lines, or lines beginning with a dash */
4105 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
4106 continue;
4107
4108 /* look for the end of the key */
4109 key_beg = c;
4110 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
4111 c++;
4112 key_end = c;
4113
4114 /* strip middle spaces and tabs */
4115 while (*c == ' ' || *c == '\t')
4116 c++;
4117
4118 /* look for the end of the value, it is the end of the line */
4119 value_beg = c;
4120 while (*c && *c != '\n' && *c != '\r')
4121 c++;
4122 value_end = c;
4123
4124 /* trim possibly trailing spaces and tabs */
4125 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
4126 value_end--;
4127
4128 /* set final \0 and check entries */
4129 *key_end = '\0';
4130 *value_end = '\0';
4131
4132 err = NULL;
4133 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
4134 if (rc < 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004135 ha_alert("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004136 hdrs_map.name, err, line);
4137 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004138 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004139 goto end;
4140 }
4141 if (rc > 0) {
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004142 ha_warning("h1-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004143 hdrs_map.name, err, line);
4144 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004145 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004146 }
4147 }
4148
4149 end:
4150 if (file)
4151 fclose(file);
4152 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
4153 return err_code;
4154}
4155
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004156/* config parser for global "h1-accept-payload_=-with-any-method" */
4157static int cfg_parse_h1_accept_payload_with_any_method(char **args, int section_type, struct proxy *curpx,
4158 const struct proxy *defpx, const char *file, int line,
4159 char **err)
4160{
4161 if (too_many_args(0, args, err, NULL))
4162 return -1;
4163 accept_payload_with_any_method = 1;
4164 return 0;
4165}
4166
Christopher Faulet98fbe952019-07-22 16:18:24 +02004167
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004168/* config parser for global "h1-header-case-adjust" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004169static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004170 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004171 char **err)
4172{
4173 if (too_many_args(2, args, err, NULL))
4174 return -1;
4175 if (!*(args[1]) || !*(args[2])) {
4176 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
4177 return -1;
4178 }
4179 return add_hdr_case_adjust(args[1], args[2], err);
4180}
4181
Christopher Fauletb112b1d2022-05-13 09:27:13 +02004182/* config parser for global "h1-headers-case-adjust-file" */
Christopher Faulet98fbe952019-07-22 16:18:24 +02004183static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004184 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004185 char **err)
4186{
4187 if (too_many_args(1, args, err, NULL))
4188 return -1;
4189 if (!*(args[1])) {
4190 memprintf(err, "'%s' expects <file> as argument.", args[0]);
4191 return -1;
4192 }
4193 free(hdrs_map.name);
4194 hdrs_map.name = strdup(args[1]);
4195 return 0;
4196}
4197
Christopher Faulet98fbe952019-07-22 16:18:24 +02004198/* config keyword parsers */
4199static struct cfg_kw_list cfg_kws = {{ }, {
Christopher Faulet0f9c0f52022-05-13 09:20:13 +02004200 { CFG_GLOBAL, "h1-accept-payload-with-any-method", cfg_parse_h1_accept_payload_with_any_method },
Christopher Faulet98fbe952019-07-22 16:18:24 +02004201 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
4202 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
4203 { 0, NULL, NULL },
4204 }
4205};
4206
4207INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4208REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
4209
4210
Christopher Faulet51dbc942018-09-13 09:05:15 +02004211/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05004212/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02004213/****************************************/
4214
4215/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01004216static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02004217 .init = h1_init,
4218 .wake = h1_wake,
4219 .attach = h1_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004220 .get_first_sc = h1_get_first_sc,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004221 .detach = h1_detach,
4222 .destroy = h1_destroy,
4223 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01004224 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004225 .rcv_buf = h1_rcv_buf,
4226 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02004227#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02004228 .rcv_pipe = h1_rcv_pipe,
4229 .snd_pipe = h1_snd_pipe,
4230#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02004231 .subscribe = h1_subscribe,
4232 .unsubscribe = h1_unsubscribe,
4233 .shutr = h1_shutr,
4234 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004235 .show_fd = h1_show_fd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004236 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004237 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02004238 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02004239 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02004240};
4241
Christopher Faulet3f612f72021-02-05 16:44:21 +01004242static const struct mux_ops mux_h1_ops = {
4243 .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 Faulet3f612f72021-02-05 16:44:21 +01004247 .detach = h1_detach,
4248 .destroy = h1_destroy,
4249 .avail_streams = h1_avail_streams,
4250 .used_streams = h1_used_streams,
4251 .rcv_buf = h1_rcv_buf,
4252 .snd_buf = h1_snd_buf,
4253#if defined(USE_LINUX_SPLICE)
4254 .rcv_pipe = h1_rcv_pipe,
4255 .snd_pipe = h1_snd_pipe,
4256#endif
4257 .subscribe = h1_subscribe,
4258 .unsubscribe = h1_unsubscribe,
4259 .shutr = h1_shutr,
4260 .shutw = h1_shutw,
4261 .show_fd = h1_show_fd,
4262 .ctl = h1_ctl,
4263 .takeover = h1_takeover,
4264 .flags = MX_FL_HTX|MX_FL_NO_UPG,
4265 .name = "H1",
4266};
Christopher Faulet51dbc942018-09-13 09:05:15 +02004267
Christopher Faulet3f612f72021-02-05 16:44:21 +01004268/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
4269static struct mux_proto_list mux_proto_h1 =
4270 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
4271static struct mux_proto_list mux_proto_http =
4272 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02004273
Christopher Faulet3f612f72021-02-05 16:44:21 +01004274INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
4275INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01004276
Christopher Faulet51dbc942018-09-13 09:05:15 +02004277/*
4278 * Local variables:
4279 * c-indent-level: 8
4280 * c-basic-offset: 8
4281 * End:
4282 */