blob: 29dee6ff94ec8116454bb9ea2ea6be74f43b3d74 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
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 Faulet1329f2a2021-12-16 17:32:56 +010018#include <haproxy/conn_stream.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 Tarreaudfd3de82020-06-04 23:46:14 +020030#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020031#include <haproxy/stream_interface.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 */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010050#define H1C_F_ST_EMBRYONIC 0x00000100 /* Set when a H1 stream with no conn-stream is attached to the connection */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010051#define H1C_F_ST_ATTACHED 0x00000200 /* Set when a H1 stream with a conn-stream 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) */
54#define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
55#define H1C_F_ST_SHUTDOWN 0x00001000 /* connection must be shut down ASAP flushing output first (conn-stream may still be attached) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010056#define H1C_F_ST_READY 0x00002000 /* Set in ATTACHED state with a READY conn-stream. A conn-stream is not ready when
57 * 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;
121 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100122 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200123
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100124 struct wait_event *subs; /* Address of the wait_event the conn_stream 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 };
151
152
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
Christopher Faulet6b81df72019-10-01 22:08:43 +0200367/* the H1 traces always expect that arg1, if non-null, is of type connection
368 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
369 * that arg3, if non-null, is a htx for rx/tx headers.
370 */
371static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
372 const struct ist where, const struct ist func,
373 const void *a1, const void *a2, const void *a3, const void *a4)
374{
375 const struct connection *conn = a1;
376 const struct h1c *h1c = conn ? conn->ctx : NULL;
377 const struct h1s *h1s = a2;
378 const struct htx *htx = a3;
379 const size_t *val = a4;
380
381 if (!h1c)
382 h1c = (h1s ? h1s->h1c : NULL);
383
384 if (!h1c || src->verbosity < H1_VERB_CLEAN)
385 return;
386
387 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200388 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200389
390 /* Display request and response states if h1s is defined */
Christopher Faulet6580f282021-11-26 17:31:35 +0100391 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200392 chunk_appendf(&trace_buf, " [%s, %s]",
393 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
394
Christopher Faulet6580f282021-11-26 17:31:35 +0100395 if (src->verbosity > H1_VERB_SIMPLE) {
396 chunk_appendf(&trace_buf, " - req=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
397 h1s->req.flags, (unsigned long)h1s->req.curr_len, (unsigned long)h1s->req.body_len);
398 chunk_appendf(&trace_buf, " res=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
399 h1s->res.flags, (unsigned long)h1s->res.curr_len, (unsigned long)h1s->res.body_len);
400 }
401
402 }
403
Christopher Faulet6b81df72019-10-01 22:08:43 +0200404 if (src->verbosity == H1_VERB_CLEAN)
405 return;
406
407 /* Display the value to the 4th argument (level > STATE) */
408 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100409 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200410
411 /* Display status-line if possible (verbosity > MINIMAL) */
412 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
413 const struct htx_blk *blk = htx_get_head_blk(htx);
414 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
415 enum htx_blk_type type = htx_get_blk_type(blk);
416
417 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
418 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
419 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
420 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
421 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
422 }
423
424 /* Display h1c info and, if defined, h1s info (pointer + flags) */
425 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100426 if (h1c->conn)
427 chunk_appendf(&trace_buf, " conn=%p(0x%08x)", h1c->conn, h1c->conn->flags);
428 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200429 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100430 if (h1s->cs)
431 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", h1s->cs, h1s->cs->flags);
432 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200433
434 if (src->verbosity == H1_VERB_MINIMAL)
435 return;
436
437 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
438 if (src->level > TRACE_LEVEL_USER) {
439 if (src->verbosity == H1_VERB_COMPLETE ||
440 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
441 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
442 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
443 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
444 if (src->verbosity == H1_VERB_COMPLETE ||
445 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
446 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
447 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
448 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
449 }
450
451 /* Display htx info if defined (level > USER) */
452 if (src->level > TRACE_LEVEL_USER && htx) {
453 int full = 0;
454
455 /* Full htx info (level > STATE && verbosity > SIMPLE) */
456 if (src->level > TRACE_LEVEL_STATE) {
457 if (src->verbosity == H1_VERB_COMPLETE)
458 full = 1;
459 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
460 full = 1;
461 }
462
463 chunk_memcat(&trace_buf, "\n\t", 2);
464 htx_dump(&trace_buf, htx, full);
465 }
466}
467
468
Christopher Faulet51dbc942018-09-13 09:05:15 +0200469/*****************************************************/
470/* functions below are for dynamic buffer management */
471/*****************************************************/
472/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100473 * Indicates whether or not we may receive data. The rules are the following :
474 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200475 * must not attempt to receive
476 * - if we are waiting for the connection establishment, we must not attempt
477 * to receive
478 * - if an error was detected on the stream we must not attempt to receive
479 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100480 * - if the input buffer failed to be allocated or is full , we must not try
481 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200482 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200483 * - otherwise must may not attempt to receive
484 */
485static inline int h1_recv_allowed(const struct h1c *h1c)
486{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100487 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100488 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 +0200489 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200490 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200491
Willy Tarreau2febb842020-07-31 09:15:43 +0200492 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
493 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 +0100494 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200495 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100496
Christopher Faulet119ac872020-09-30 17:33:22 +0200497 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
498 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
499 return 0;
500 }
501
Christopher Fauletd17ad822020-09-24 15:14:29 +0200502 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200503 return 1;
504
Christopher Faulet6b81df72019-10-01 22:08:43 +0200505 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 +0200506 return 0;
507}
508
509/*
510 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
511 * flags are used to figure what buffer was requested. It returns 1 if the
512 * allocation succeeds, in which case the connection is woken up, or 0 if it's
513 * impossible to wake up and we prefer to be woken up later.
514 */
515static int h1_buf_available(void *target)
516{
517 struct h1c *h1c = target;
518
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100519 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200520 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 +0200521 h1c->flags &= ~H1C_F_IN_ALLOC;
522 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200523 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200524 return 1;
525 }
526
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100527 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200528 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 +0200529 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200530 if (h1c->h1s)
531 h1_wake_stream_for_send(h1c->h1s);
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_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200536 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
537 h1c->flags &= ~H1C_F_IN_SALLOC;
538 tasklet_wakeup(h1c->wait_event.tasklet);
539 return 1;
540 }
541
Christopher Faulet51dbc942018-09-13 09:05:15 +0200542 return 0;
543}
544
545/*
546 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
547 */
548static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
549{
550 struct buffer *buf = NULL;
551
Willy Tarreau2b718102021-04-21 07:32:39 +0200552 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100553 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200554 h1c->buf_wait.target = h1c;
555 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200556 LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200557 }
558 return buf;
559}
560
561/*
562 * Release a buffer, if any, and try to wake up entities waiting in the buffer
563 * wait queue.
564 */
565static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
566{
567 if (bptr->size) {
568 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100569 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200570 }
571}
572
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100573/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100574 * 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 +0100575 * not. This flag is only set when no H1S is attached and when the previous
576 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100577 */
578static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200579{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100580 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200581
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100582 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200583}
584
Willy Tarreau00f18a32019-01-26 12:19:01 +0100585/* returns the number of streams still available on a connection */
586static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100587{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100588 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100589}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200590
Christopher Faulet7a145d62020-08-05 11:31:16 +0200591/* Refresh the h1c task timeout if necessary */
592static void h1_refresh_timeout(struct h1c *h1c)
593{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200594 int is_idle_conn = 0;
595
Christopher Faulet7a145d62020-08-05 11:31:16 +0200596 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100597 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200598 /* half-closed or dead connections : switch to clientfin/serverfin
599 * timeouts so that we don't hang too long on clients that have
600 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200601 */
602 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200603 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 +0200604 is_idle_conn = 1;
Christopher Fauletadcd7892020-10-05 17:13:05 +0200605 }
606 else if (b_data(&h1c->obuf)) {
607 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200608 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200609 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
610 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100611 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
612 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200613 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100614 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 +0200615 /* A frontend connection not yet ready could be treated the same way as an idle
616 * one in case of soft-close.
617 */
618 is_idle_conn = 1;
Christopher Faulet7a145d62020-08-05 11:31:16 +0200619 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200620 else {
621 /* alive back connections of front connections with a conn-stream attached */
622 h1c->task->expire = TICK_ETERNITY;
623 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
624 }
625
Christopher Fauletdbe57792020-10-05 17:50:58 +0200626 /* Finally set the idle expiration date if shorter */
627 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200628
629 if ((h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
630 is_idle_conn && tick_isset(global.close_spread_end)) {
631 /* If a soft-stop is in progress and a close-spread-time
632 * is set, we want to spread idle connection closing roughly
633 * evenly across the defined window. This should only
634 * act on idle frontend connections.
635 * If the window end is already in the past, we wake the
636 * timeout task up immediately so that it can be closed.
637 */
638 int remaining_window = tick_remain(now_ms, global.close_spread_end);
639 if (remaining_window) {
640 /* We don't need to reset the expire if it would
641 * already happen before the close window end.
642 */
643 if (tick_is_le(global.close_spread_end, h1c->task->expire)) {
644 /* Set an expire value shorter than the current value
645 * because the close spread window end comes earlier.
646 */
647 h1c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
648 TRACE_DEVEL("connection timeout set to value before close-spread window end", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
649 }
650 }
651 else {
652 /* We are past the soft close window end, wake the timeout
653 * task up immediately.
654 */
655 task_wakeup(h1c->task, TASK_WOKEN_TIMER);
656 }
657 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200658 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
659 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200660 }
661}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200662
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200663static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200664{
665 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
666 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
667 h1c->idle_exp = TICK_ETERNITY;
668 return;
669 }
670
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100671 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200672 if (!tick_isset(h1c->idle_exp)) {
673 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
674 !b_data(&h1c->ibuf) && /* No input data */
675 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
676 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
677 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
678 }
679 else {
680 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
681 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
682 }
683 }
684 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100685 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200686 if (!tick_isset(h1c->idle_exp)) {
687 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
688 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
689 }
690 }
691 else { // CS_ATTACHED or SHUTDOWN
692 h1c->idle_exp = TICK_ETERNITY;
693 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
694 }
695}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200696/*****************************************************************/
697/* functions below are dedicated to the mux setup and management */
698/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200699
700/* returns non-zero if there are input data pending for stream h1s. */
701static inline size_t h1s_data_pending(const struct h1s *h1s)
702{
703 const struct h1m *h1m;
704
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200705 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100706 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200707}
708
Christopher Faulet16df1782020-12-04 16:47:41 +0100709/* Creates a new conn-stream and the associate stream. <input> is used as input
710 * buffer for the stream. On success, it is transferred to the stream and the
711 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
712 * mux must still take care of it. However, there is nothing special to do
713 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
714 * b_free() on it is always safe. This function returns the conn-stream on
715 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200716static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100717{
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100718 struct h1c *h1c = h1s->h1c;
Christopher Faulet47365272018-10-31 17:40:50 +0100719 struct conn_stream *cs;
720
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100721 TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100722 cs = cs_new();
Christopher Faulet6b81df72019-10-01 22:08:43 +0200723 if (!cs) {
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100724 TRACE_ERROR("CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100725 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200726 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100727 cs_attach_endp(cs, &h1c->conn->obj_type, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100728 h1s->cs = cs;
Christopher Faulet47365272018-10-31 17:40:50 +0100729
730 if (h1s->flags & H1S_F_NOT_FIRST)
731 cs->flags |= CS_FL_NOT_FIRST;
732
Amaury Denoyelle90ac6052021-10-18 14:45:49 +0200733 if (h1s->req.flags & H1_MF_UPG_WEBSOCKET)
734 cs->flags |= CS_FL_WEBSOCKET;
735
Christopher Faulet13a35e52021-12-20 15:34:16 +0100736 if (!stream_new(h1c->conn->owner, cs, input)) {
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100737 TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet91449b02022-03-22 18:45:55 +0100738 goto err_cs;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200739 }
740
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100741 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
742 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100743
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100744 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
745 TRACE_LEAVE(H1_EV_STRM_NEW, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100746 return cs;
747
Christopher Faulet91449b02022-03-22 18:45:55 +0100748 err_cs:
Christopher Faulet47365272018-10-31 17:40:50 +0100749 cs_free(cs);
Christopher Faulet91449b02022-03-22 18:45:55 +0100750 err:
Christopher Faulet47365272018-10-31 17:40:50 +0100751 h1s->cs = NULL;
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100752 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100753 return NULL;
754}
755
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100756static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
757{
758 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
759
760 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100761 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 +0100762 goto err;
763 }
764
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100765 h1s->h1c->flags |= H1C_F_ST_READY;
766 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
767 return h1s->cs;
768
769 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100770 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100771 return NULL;
772}
773
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200774static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200775{
776 struct h1s *h1s;
777
Christopher Faulet6b81df72019-10-01 22:08:43 +0200778 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
779
Christopher Faulet51dbc942018-09-13 09:05:15 +0200780 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100781 if (!h1s) {
782 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 +0100783 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100784 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200785 h1s->h1c = h1c;
786 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200787 h1s->sess = NULL;
788 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100789 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100790 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200791 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100792 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200793
794 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100795 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200796
Christopher Faulet129817b2018-09-20 16:14:40 +0200797 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100798 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200799
800 h1s->status = 0;
801 h1s->meth = HTTP_METH_OTHER;
802
Christopher Faulet47365272018-10-31 17:40:50 +0100803 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
804 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100805 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100806
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200807 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
808 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200809
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200810 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100811 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200812 return NULL;
813}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100814
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200815static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
816{
817 struct session *sess = h1c->conn->owner;
818 struct h1s *h1s;
819
820 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
821
822 h1s = h1s_new(h1c);
823 if (!h1s)
824 goto fail;
825
826 h1s->sess = sess;
827
828 if (h1c->px->options2 & PR_O2_REQBUG_OK)
829 h1s->req.err_pos = -1;
830
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200831 h1c->idle_exp = TICK_ETERNITY;
832 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200833 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200834 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100835
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200836 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100837 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200838 return NULL;
839}
840
841static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
842{
843 struct h1s *h1s;
844
845 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
846
847 h1s = h1s_new(h1c);
848 if (!h1s)
849 goto fail;
850
Christopher Faulet10a86702021-04-07 14:18:11 +0200851 h1s->flags |= H1S_F_RX_BLK;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200852 h1s->cs = cs;
853 h1s->sess = sess;
854 cs->ctx = h1s;
855
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100856 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200857
858 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
859 h1s->res.err_pos = -1;
860
Christopher Faulet60fa0512021-11-26 18:10:39 +0100861 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100862 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100863
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200864 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
865 return h1s;
866
867 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100868 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100869 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200870}
871
872static void h1s_destroy(struct h1s *h1s)
873{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200874 if (h1s) {
875 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200876
Christopher Faulet6b81df72019-10-01 22:08:43 +0200877 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200879
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100880 if (h1s->subs)
881 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200882
Christopher Fauletd17ad822020-09-24 15:14:29 +0200883 h1_release_buf(h1c, &h1s->rxbuf);
884
Christopher Faulet10a86702021-04-07 14:18:11 +0200885 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Fauletb385b502021-01-13 18:47:57 +0100886 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200887 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
888 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200889 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100890 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +0100891 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 +0200892 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100893
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100894 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100895 !(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 +0100896 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100897 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100898 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100899 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
900 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200901 else {
Christopher Faulet26a26432021-01-27 11:27:50 +0100902 TRACE_STATE("set shudown on h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100903 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200904 }
Christopher Faulet60fa0512021-11-26 18:10:39 +0100905
906 HA_ATOMIC_DEC(&h1c->px_counters->open_streams);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200907 pool_free(pool_head_h1s, h1s);
908 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200909}
910
911/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200912 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500913 * to the existing conn_stream (for outgoing connections or for incoming ones
914 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200915 * establishment). <input> is always used as Input buffer and may contain
916 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
917 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200918 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200919static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
920 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200921{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200922 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100923 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200924 void *conn_ctx = conn->ctx;
925
926 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200927
928 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100929 if (!h1c) {
930 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200931 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100932 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200933 h1c->conn = conn;
934 h1c->px = proxy;
935
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100936 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200937 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200938 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200939 h1c->obuf = BUF_NULL;
940 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200941 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200942
Willy Tarreau90f366b2021-02-20 11:49:49 +0100943 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200944 h1c->wait_event.tasklet = tasklet_new();
945 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200946 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200947 h1c->wait_event.tasklet->process = h1_io_cb;
948 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100949 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200950 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200951
Christopher Faulete9b70722019-04-08 10:46:02 +0200952 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200953 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100954 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
955 if (tick_isset(proxy->timeout.serverfin))
956 h1c->shut_timeout = proxy->timeout.serverfin;
Christopher Faulet563c3452021-11-26 17:37:51 +0100957
958 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be,
959 &h1_stats_module);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100960 } else {
961 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
962 if (tick_isset(proxy->timeout.clientfin))
963 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200964
Christopher Faulet563c3452021-11-26 17:37:51 +0100965 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe,
966 &h1_stats_module);
967
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200968 LIST_APPEND(&mux_stopping_data[tid].list,
969 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100970 }
971 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200972 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100973 if (!t) {
974 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 +0100975 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100976 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100977
978 h1c->task = t;
979 t->process = h1_timeout_task;
980 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200981
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100982 t->expire = tick_add(now_ms, h1c->timeout);
983 }
984
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100985 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200986
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200987 if (h1c->flags & H1C_F_IS_BACK) {
988 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200989 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
990 goto fail;
991 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100992 else if (conn_ctx) {
993 /* Upgraded frontend connection (from TCP) */
994 struct conn_stream *cs = conn_ctx;
995
996 if (!h1c_frt_stream_new(h1c))
997 goto fail;
998
999 h1c->h1s->cs = cs;
1000 cs->ctx = h1c->h1s;
1001
1002 /* Attach the CS but Not ready yet */
1003 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
1004 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
1005 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
1006 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001007
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001008 if (t) {
1009 h1_set_idle_expiration(h1c);
1010 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001011 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001012 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001013
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001014 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +01001015 if (b_data(&h1c->ibuf))
1016 tasklet_wakeup(h1c->wait_event.tasklet);
1017 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +02001018 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001019
Christopher Faulet60fa0512021-11-26 18:10:39 +01001020 HA_ATOMIC_INC(&h1c->px_counters->open_conns);
Christopher Faulet3bca28c2021-11-26 18:12:04 +01001021 HA_ATOMIC_INC(&h1c->px_counters->total_conns);
Christopher Faulet60fa0512021-11-26 18:10:39 +01001022
Christopher Faulet51dbc942018-09-13 09:05:15 +02001023 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001024 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001025 return 0;
1026
1027 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +02001028 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001029 if (h1c->wait_event.tasklet)
1030 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001031 pool_free(pool_head_h1c, h1c);
1032 fail_h1c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +01001033 if (!conn_is_back(conn))
1034 LIST_DEL_INIT(&conn->stopping_list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001035 conn->ctx = conn_ctx; // restore saved context
1036 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001037 return -1;
1038}
1039
Christopher Faulet73c12072019-04-08 11:23:22 +02001040/* release function. This one should be called to free all resources allocated
1041 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +02001042 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001043static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001044{
Christopher Faulet61840e72019-04-15 09:33:32 +02001045 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001046
Christopher Faulet6b81df72019-10-01 22:08:43 +02001047 TRACE_POINT(H1_EV_H1C_END);
1048
Christopher Faulet51dbc942018-09-13 09:05:15 +02001049 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +02001050 /* The connection must be aattached to this mux to be released */
1051 if (h1c->conn && h1c->conn->ctx == h1c)
1052 conn = h1c->conn;
1053
Christopher Faulet6b81df72019-10-01 22:08:43 +02001054 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
1055
Christopher Faulet61840e72019-04-15 09:33:32 +02001056 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001057 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +02001058 /* Make sure we're no longer subscribed to anything */
1059 if (h1c->wait_event.events)
1060 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1061 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +02001062 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001063 /* connection successfully upgraded to H2, this
1064 * mux was already released */
1065 return;
1066 }
Christopher Faulet26a26432021-01-27 11:27:50 +01001067 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001068 sess_log(conn->owner); /* Log if the upgrade failed */
1069 }
1070
Olivier Houchard45c44372019-06-11 14:06:23 +02001071
Willy Tarreau2b718102021-04-21 07:32:39 +02001072 if (LIST_INLIST(&h1c->buf_wait.list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01001073 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001074
1075 h1_release_buf(h1c, &h1c->ibuf);
1076 h1_release_buf(h1c, &h1c->obuf);
1077
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001078 if (h1c->task) {
1079 h1c->task->context = NULL;
1080 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
1081 h1c->task = NULL;
1082 }
1083
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001084 if (h1c->wait_event.tasklet)
1085 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001086
Christopher Fauletf2824e62018-10-01 12:12:37 +02001087 h1s_destroy(h1c->h1s);
Christopher Faulete76b4f02021-10-27 15:42:13 +02001088 if (conn) {
1089 if (h1c->wait_event.events != 0)
1090 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
1091 &h1c->wait_event);
1092 h1_shutw_conn(conn);
1093 }
Christopher Faulet60fa0512021-11-26 18:10:39 +01001094
1095 HA_ATOMIC_DEC(&h1c->px_counters->open_conns);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001096 pool_free(pool_head_h1c, h1c);
1097 }
1098
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001099 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001100 if (!conn_is_back(conn))
1101 LIST_DEL_INIT(&conn->stopping_list);
1102
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001103 conn->mux = NULL;
1104 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001105 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001106
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001107 conn_stop_tracking(conn);
1108 conn_full_close(conn);
1109 if (conn->destroy_cb)
1110 conn->destroy_cb(conn);
1111 conn_free(conn);
1112 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001113}
1114
1115/******************************************************/
1116/* functions below are for the H1 protocol processing */
1117/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +02001118/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
1119 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +02001120 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001121static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001122{
Christopher Faulet570d1612018-11-26 11:13:57 +01001123 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001124
Christopher Faulet570d1612018-11-26 11:13:57 +01001125 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001126 (*(p + 5) > '1' ||
1127 (*(p + 5) == '1' && *(p + 7) >= '1')))
1128 h1m->flags |= H1_MF_VER_11;
1129}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001130
Christopher Faulet9768c262018-10-22 09:34:31 +02001131/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
1132 * greater or equal to 1.1
1133 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001134static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +02001135{
Christopher Faulet570d1612018-11-26 11:13:57 +01001136 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001137
Christopher Faulet570d1612018-11-26 11:13:57 +01001138 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001139 (*(p + 5) > '1' ||
1140 (*(p + 5) == '1' && *(p + 7) >= '1')))
1141 h1m->flags |= H1_MF_VER_11;
1142}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001143
Christopher Fauletf2824e62018-10-01 12:12:37 +02001144/* Deduce the connection mode of the client connection, depending on the
1145 * configuration and the H1 message flags. This function is called twice, the
1146 * first time when the request is parsed and the second time when the response
1147 * is parsed.
1148 */
1149static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
1150{
1151 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001152
1153 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001154 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001155 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001156 h1s->status == 101) {
1157 /* Either we've established an explicit tunnel, or we're
1158 * switching the protocol. In both cases, we're very unlikely to
1159 * understand the next protocols. We have to switch to tunnel
1160 * mode, so that we transfer the request and responses then let
1161 * this protocol pass unmodified. When we later implement
1162 * specific parsers for such protocols, we'll want to check the
1163 * Upgrade header which contains information about that protocol
1164 * for responses with status 101 (eg: see RFC2817 about TLS).
1165 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001166 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001167 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 +01001168 }
1169 else if (h1s->flags & H1S_F_WANT_KAL) {
1170 /* By default the client is in KAL mode. CLOSE mode mean
1171 * it is imposed by the client itself. So only change
1172 * KAL mode here. */
1173 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1174 /* no length known or explicit close => close */
1175 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001176 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 +01001177 }
1178 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1179 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001180 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001181 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001182 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 +01001183 }
1184 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001185 }
1186 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001187 /* Input direction: first pass */
1188 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1189 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001190 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001191 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 +01001192 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001193 }
1194
1195 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001196 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001197 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001198 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);
1199 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001200}
1201
1202/* Deduce the connection mode of the client connection, depending on the
1203 * configuration and the H1 message flags. This function is called twice, the
1204 * first time when the request is parsed and the second time when the response
1205 * is parsed.
1206 */
1207static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1208{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001209 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001210 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001211 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001212
Christopher Fauletf2824e62018-10-01 12:12:37 +02001213 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001214 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001215 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001216 h1s->status == 101) {
1217 /* Either we've established an explicit tunnel, or we're
1218 * switching the protocol. In both cases, we're very unlikely to
1219 * understand the next protocols. We have to switch to tunnel
1220 * mode, so that we transfer the request and responses then let
1221 * this protocol pass unmodified. When we later implement
1222 * specific parsers for such protocols, we'll want to check the
1223 * Upgrade header which contains information about that protocol
1224 * for responses with status 101 (eg: see RFC2817 about TLS).
1225 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001226 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001227 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 +01001228 }
1229 else if (h1s->flags & H1S_F_WANT_KAL) {
1230 /* By default the server is in KAL mode. CLOSE mode mean
1231 * it is imposed by haproxy itself. So only change KAL
1232 * mode here. */
1233 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1234 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1235 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1236 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001237 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 +01001238 }
1239 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001240 }
Christopher Faulet70033782018-12-05 13:50:11 +01001241 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001242 /* Output direction: first pass */
1243 if (h1m->flags & H1_MF_CONN_CLO) {
1244 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001245 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001246 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 +01001247 }
1248 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1249 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1250 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1251 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1252 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1253 /* no explicit keep-alive option httpclose/server-close => close */
1254 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001255 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 +01001256 }
Christopher Faulet70033782018-12-05 13:50:11 +01001257 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001258
1259 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001260 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001261 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001262 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);
1263 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001264}
1265
Christopher Fauletb992af02019-03-28 15:42:24 +01001266static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001267{
1268 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001269
1270 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1271 * token is found
1272 */
1273 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001274 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001275
1276 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001277 if (!(h1m->flags & H1_MF_VER_11)) {
1278 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 +01001279 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001280 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001281 }
1282 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001283 if (h1m->flags & H1_MF_VER_11) {
1284 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001285 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001286 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001287 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001288}
1289
Christopher Fauletb992af02019-03-28 15:42:24 +01001290static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001291{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001292 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1293 * token is found
1294 */
1295 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001296 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001297
1298 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001299 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001300 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1301 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 +01001302 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001303 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001304 }
1305 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001306 if (h1m->flags & H1_MF_VER_11) {
1307 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001308 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001309 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001310 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001311}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001312
Christopher Fauletb992af02019-03-28 15:42:24 +01001313static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001314{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001315 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001316 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001317 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001318 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001319}
1320
Christopher Fauletb992af02019-03-28 15:42:24 +01001321static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1322{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001323 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001324 h1_set_cli_conn_mode(h1s, h1m);
1325 else
1326 h1_set_srv_conn_mode(h1s, h1m);
1327
1328 if (!(h1m->flags & H1_MF_RESP))
1329 h1_update_req_conn_value(h1s, h1m, conn_val);
1330 else
1331 h1_update_res_conn_value(h1s, h1m, conn_val);
1332}
Christopher Faulete44769b2018-11-29 23:01:45 +01001333
Christopher Faulet98fbe952019-07-22 16:18:24 +02001334/* Try to adjust the case of the message header name using the global map
1335 * <hdrs_map>.
1336 */
1337static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1338{
1339 struct ebpt_node *node;
1340 struct h1_hdr_entry *entry;
1341
1342 /* No entry in the map, do nothing */
1343 if (eb_is_empty(&hdrs_map.map))
1344 return;
1345
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001346 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001347 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1348 return;
1349
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001350 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001351 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1352 return;
1353
1354 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1355 if (!node)
1356 return;
1357 entry = container_of(node, struct h1_hdr_entry, node);
1358 name->ptr = entry->name.ptr;
1359 name->len = entry->name.len;
1360}
1361
Christopher Faulete44769b2018-11-29 23:01:45 +01001362/* Append the description of what is present in error snapshot <es> into <out>.
1363 * The description must be small enough to always fit in a buffer. The output
1364 * buffer may be the trash so the trash must not be used inside this function.
1365 */
1366static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1367{
1368 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001369 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1370 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1371 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1372 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1373 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1374 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001375}
1376/*
1377 * Capture a bad request or response and archive it in the proxy's structure.
1378 * By default it tries to report the error position as h1m->err_pos. However if
1379 * this one is not set, it will then report h1m->next, which is the last known
1380 * parsing point. The function is able to deal with wrapping buffers. It always
1381 * displays buffers as a contiguous area starting at buf->p. The direction is
1382 * determined thanks to the h1m's flags.
1383 */
1384static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1385 struct h1m *h1m, struct buffer *buf)
1386{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001387 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001388 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001389 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001390 union error_snapshot_ctx ctx;
1391
Christopher Fauletf835dea2021-12-21 14:35:17 +01001392 if ((h1c->flags & H1C_F_ST_ATTACHED) && cs_strm(h1s->cs)) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001393 if (sess == NULL)
Christopher Faulet693b23b2022-02-28 09:09:05 +01001394 sess = __cs_strm(h1s->cs)->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001395 if (!(h1m->flags & H1_MF_RESP))
Christopher Faulet693b23b2022-02-28 09:09:05 +01001396 other_end = __cs_strm(h1s->cs)->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001397 else
1398 other_end = sess->fe;
1399 } else
1400 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001401
1402 /* http-specific part now */
1403 ctx.h1.state = h1m->state;
1404 ctx.h1.c_flags = h1c->flags;
1405 ctx.h1.s_flags = h1s->flags;
1406 ctx.h1.m_flags = h1m->flags;
1407 ctx.h1.m_clen = h1m->curr_len;
1408 ctx.h1.m_blen = h1m->body_len;
1409
1410 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1411 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001412 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1413 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001414}
1415
Christopher Fauletadb22202018-12-12 10:32:09 +01001416/* Emit the chunksize followed by a CRLF in front of data of the buffer
1417 * <buf>. It goes backwards and starts with the byte before the buffer's
1418 * head. The caller is responsible for ensuring there is enough room left before
1419 * the buffer's head for the string.
1420 */
1421static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1422{
1423 char *beg, *end;
1424
1425 beg = end = b_head(buf);
1426 *--beg = '\n';
1427 *--beg = '\r';
1428 do {
1429 *--beg = hextab[chksz & 0xF];
1430 } while (chksz >>= 4);
1431 buf->head -= (end - beg);
1432 b_add(buf, end - beg);
1433}
1434
1435/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1436 * ensuring there is enough room left in the buffer for the string. */
1437static void h1_emit_chunk_crlf(struct buffer *buf)
1438{
1439 *(b_peek(buf, b_data(buf))) = '\r';
1440 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1441 b_add(buf, 2);
1442}
1443
Christopher Faulet129817b2018-09-20 16:14:40 +02001444/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001445 * Switch the stream to tunnel mode. This function must only be called on 2xx
1446 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001447 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001448static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001449{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001450 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001451
Christopher Faulet5be651d2021-01-22 15:28:03 +01001452 h1s->req.state = H1_MSG_TUNNEL;
1453 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001454
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001455 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001456 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001457
Christopher Faulet5be651d2021-01-22 15:28:03 +01001458 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 +01001459
Christopher Faulet10a86702021-04-07 14:18:11 +02001460 if (h1s->flags & H1S_F_RX_BLK) {
1461 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001462 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001463 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 +02001464 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001465 if (h1s->flags & H1S_F_TX_BLK) {
1466 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001467 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001468 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 +01001469 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001470}
1471
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001472/* Search for a websocket key header. The message should have been identified
1473 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001474 *
1475 * On the request side, if found the key is stored in the session. It might be
1476 * needed to calculate response key if the server side is using http/2.
1477 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001478 * On the response side, the key might be verified if haproxy has been
1479 * responsible for the generation of a key. This happens when a h2 client is
1480 * interfaced with a h1 server.
1481 *
1482 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001483 */
1484static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1485{
1486 struct htx_blk *blk;
1487 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001488 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001489 int ws_key_found = 0, idx;
1490
1491 idx = htx_get_head(htx); // returns the SL that we skip
1492 while ((idx = htx_get_next(htx, idx)) != -1) {
1493 blk = htx_get_blk(htx, idx);
1494 type = htx_get_blk_type(blk);
1495
1496 if (type == HTX_BLK_UNUSED)
1497 continue;
1498
1499 if (type != HTX_BLK_HDR)
1500 break;
1501
1502 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001503 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001504
Amaury Denoyellec1938232020-12-11 17:53:03 +01001505 /* Websocket key is base64 encoded of 16 bytes */
1506 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001507 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001508 /* Copy the key on request side
1509 * we might need it if the server is using h2 and does
1510 * not provide the response
1511 */
1512 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001513 ws_key_found = 1;
1514 break;
1515 }
1516 else if (isteqi(n, ist("sec-websocket-accept")) &&
1517 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001518 /* Need to verify the response key if the input was
1519 * generated by haproxy
1520 */
1521 if (h1s->ws_key[0]) {
1522 char key[29];
1523 h1_calculate_ws_output_key(h1s->ws_key, key);
1524 if (!isteqi(ist(key), v))
1525 break;
1526 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001527 ws_key_found = 1;
1528 break;
1529 }
1530 }
1531
1532 /* missing websocket key, reject the message */
1533 if (!ws_key_found) {
1534 htx->flags |= HTX_FL_PARSING_ERROR;
1535 return 0;
1536 }
1537
1538 return 1;
1539}
1540
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001541/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001542 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001543 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001544 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1545 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001546 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001547static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1548 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001549{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001550 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001551 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001552
Willy Tarreau022e5e52020-09-10 09:33:15 +02001553 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 +02001554
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001555 if (h1s->meth == HTTP_METH_CONNECT)
1556 h1m->flags |= H1_MF_METH_CONNECT;
1557 if (h1s->meth == HTTP_METH_HEAD)
1558 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001559
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001560 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001561 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001562 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 +02001563 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001564 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001565 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 +02001566 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1567 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001568 else if (ret == -2) {
1569 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);
1570 h1s->flags |= H1S_F_RX_CONGESTED;
1571 }
1572 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001573 goto end;
1574 }
1575
Christopher Faulete136bd12021-09-21 18:44:55 +02001576
1577 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload. There is a
1578 * payload if the c-l is not null or the the payload is chunk-encoded.
1579 * A parsing error is reported but a A 413-Payload-Too-Large is returned
1580 * instead of a 400-Bad-Request.
1581 */
1582 if (!(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
1583 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1584 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1585 h1s->flags |= H1S_F_PARSING_ERROR;
1586 htx->flags |= HTX_FL_PARSING_ERROR;
1587 h1s->h1c->errcode = 413;
1588 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);
1589 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1590 ret = 0;
1591 goto end;
1592 }
1593
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001594 /* Reject any message with an unknown transfer-encoding. In fact if any
1595 * encoding other than "chunked". A 422-Unprocessable-Content is
1596 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1597 * response.
1598 */
1599 if (h1m->flags & H1_MF_TE_OTHER) {
1600 h1s->flags |= H1S_F_PARSING_ERROR;
1601 htx->flags |= HTX_FL_PARSING_ERROR;
1602 if (!(h1m->flags & H1_MF_RESP))
1603 h1s->h1c->errcode = 422;
1604 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1605 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1606 ret = 0;
1607 goto end;
1608 }
1609
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001610 /* If websocket handshake, search for the websocket key */
1611 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1612 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1613 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1614 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001615 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001616 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 +01001617 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1618
1619 ret = 0;
1620 goto end;
1621 }
1622 }
1623
Christopher Faulet486498c2019-10-11 14:22:00 +02001624 if (h1m->err_pos >= 0) {
1625 /* Maybe we found an error during the parsing while we were
1626 * configured not to block on that, so we have to capture it
1627 * now.
1628 */
1629 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1630 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1631 }
1632
Christopher Faulet5696f542020-12-02 16:08:38 +01001633 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001634 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001635 if (h1s->meth == HTTP_METH_HEAD)
1636 h1s->flags |= H1S_F_BODYLESS_RESP;
1637 }
1638 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001639 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001640 if (h1s->status == 204 || h1s->status == 304)
1641 h1s->flags |= H1S_F_BODYLESS_RESP;
1642 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001643 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001645
Christopher Faulet129817b2018-09-20 16:14:40 +02001646 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001647 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 +02001648 return ret;
1649}
1650
1651/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001652 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001653 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1654 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001655 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001656static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1657 struct buffer *buf, size_t *ofs, size_t max,
1658 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001659{
Christopher Fauletde471a42021-02-01 16:37:28 +01001660 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001661
Willy Tarreau022e5e52020-09-10 09:33:15 +02001662 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 +02001663 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001664 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001665 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 +01001666 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001667 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001668 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 +02001669 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001670 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001671 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001672 }
1673
Christopher Faulet02a02532019-11-15 09:36:28 +01001674 *ofs += ret;
1675
1676 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001677 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1678 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);
1679 h1s->flags |= H1S_F_RX_CONGESTED;
1680 }
1681
Willy Tarreau022e5e52020-09-10 09:33:15 +02001682 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 +02001683 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001684}
1685
1686/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001687 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1688 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1689 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001690 * responsible to update the parser state <h1m>. If more room is requested,
1691 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001692 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001693static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1694 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001695{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001696 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001697
Willy Tarreau022e5e52020-09-10 09:33:15 +02001698 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 +02001699 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001700 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001701 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 +02001702 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001703 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001704 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 +02001705 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1706 }
Christopher Faulet744451c2022-03-28 16:19:02 +02001707 else if (ret == -2 || b_data(buf) != *ofs) {
Christopher Faulet46e058d2021-09-20 07:47:27 +02001708 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);
1709 h1s->flags |= H1S_F_RX_CONGESTED;
1710 }
1711 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001712 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001713 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001714
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001715 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001716
1717 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001718 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 +02001719 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001720}
1721
1722/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001723 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001724 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1725 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001726 *
1727 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001728 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001729static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001730{
Christopher Faulet539e0292018-11-19 10:40:09 +01001731 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001732 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001733 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001734 size_t data;
1735 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001736 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001737
Christopher Faulet539e0292018-11-19 10:40:09 +01001738 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001739 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001740
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001741 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001742 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001743
Christopher Faulet2eed8002020-12-07 11:26:13 +01001744 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001745 goto end;
1746
Christopher Faulet10a86702021-04-07 14:18:11 +02001747 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001748 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001749
Christopher Faulet46e058d2021-09-20 07:47:27 +02001750 /* Always remove congestion flags and try to process more input data */
1751 h1s->flags &= ~H1S_F_RX_CONGESTED;
1752
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001753 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001754 size_t used = htx_used_space(htx);
1755
Christopher Faulet129817b2018-09-20 16:14:40 +02001756 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001757 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001758 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001759 if (!ret)
1760 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001761
1762 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1763 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1764
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001765 if ((h1m->flags & H1_MF_RESP) &&
1766 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1767 h1m_init_res(&h1s->res);
1768 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001769 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001770 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001771 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001772 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001773 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001774 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001775 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001776 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001777
1778 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1779 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001780 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001781 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001782 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001783 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001784 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001785 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001786
Christopher Faulet76014fd2019-12-10 11:47:22 +01001787 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1788 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001789 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001790 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001791 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1792 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001793
Christopher Faulet5be651d2021-01-22 15:28:03 +01001794 if ((h1m->flags & H1_MF_RESP) &&
1795 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1796 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001797 else {
1798 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1799 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001800 h1s->flags |= H1S_F_RX_BLK;
1801 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001802 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001803 if (h1s->flags & H1S_F_TX_BLK) {
1804 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001805 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001806 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 +01001807 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001808 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001809 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001810 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001811 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001812 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001813 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001814 if (!ret)
1815 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001816
1817 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1818 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001819 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001820 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001821 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001822 break;
1823 }
1824
Christopher Faulet30db3d72019-05-17 15:35:33 +02001825 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001826 } 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 +01001827
Christopher Faulet129817b2018-09-20 16:14:40 +02001828
Christopher Faulet2eed8002020-12-07 11:26:13 +01001829 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001830 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 +02001831 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001832 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001833
Christopher Faulet539e0292018-11-19 10:40:09 +01001834 b_del(&h1c->ibuf, total);
1835
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001836 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001837 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1838
Christopher Faulet30db3d72019-05-17 15:35:33 +02001839 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001840 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001841 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001842 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 +01001843 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001844 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001845
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001846 if (!b_data(&h1c->ibuf))
1847 h1_release_buf(h1c, &h1c->ibuf);
1848
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001849 if (!(h1c->flags & H1C_F_ST_READY)) {
1850 /* The H1 connection is not ready. Most of time, there is no CS
1851 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1852 * cases, it is only possible on the client side.
1853 */
1854 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1855
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001856 if (h1m->state <= H1_MSG_LAST_LF) {
1857 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1858 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1859 goto end;
1860 }
1861
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001862 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1863 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1864 BUG_ON(h1s->cs);
1865 if (!h1s_new_cs(h1s, buf)) {
1866 h1c->flags |= H1C_F_ST_ERROR;
1867 goto err;
1868 }
1869 }
1870 else {
1871 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1872 BUG_ON(h1s->cs == NULL);
1873 if (!h1s_upgrade_cs(h1s, buf)) {
1874 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001875 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001876 goto err;
1877 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001878 }
1879 }
1880
1881 /* Here h1s->cs is always defined */
Christopher Fauletf5ce3202021-11-26 17:26:19 +01001882 if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) {
Christopher Fauleta583af62020-09-24 15:35:37 +02001883 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1884 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1885 }
1886 else {
1887 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1888 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1889 }
1890
Christopher Fauleta22782b2021-02-08 17:18:01 +01001891 /* Set EOI on conn-stream in DONE state iff:
1892 * - it is a response
1893 * - it is a request but no a protocol upgrade nor a CONNECT
1894 *
1895 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001896 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001897 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001898 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1899 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Christopher Fauleta583af62020-09-24 15:35:37 +02001900 h1s->cs->flags |= CS_FL_EOI;
1901
Christopher Fauletec4207c2021-04-08 18:34:30 +02001902 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001903 /* When Input data are pending for this message, notify upper layer that
1904 * the mux need more space in the HTX buffer to continue if :
1905 *
1906 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1907 * - Headers or trailers are pending to be copied.
1908 */
1909 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Christopher Fauletcf56b992018-12-11 16:12:31 +01001910 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001911 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1912 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001913 else {
1914 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1915 if (h1s->flags & H1S_F_REOS) {
1916 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001917 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1918 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1919 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001920 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001921 }
Christopher Faulet26a26432021-01-27 11:27:50 +01001922 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001923 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001924 TRACE_ERROR("message aborted, set error on CS", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
1925 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001926
Christopher Faulet10a86702021-04-07 14:18:11 +02001927 if (h1s->flags & H1S_F_TX_BLK) {
1928 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001929 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001930 TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001931 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001932 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001933 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001934
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001935 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001936 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001937 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001938
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001939 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001940 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001941 if (h1s->cs)
1942 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001943 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001944 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001945}
1946
Christopher Faulet129817b2018-09-20 16:14:40 +02001947/*
1948 * Process outgoing data. It parses data and transfer them from the channel buffer into
1949 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1950 * 0 if it couldn't proceed.
1951 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001952static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001953{
Christopher Faulet129817b2018-09-20 16:14:40 +02001954 struct h1s *h1s = h1c->h1s;
1955 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001956 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001957 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001958 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001959 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001960 int last_data = 0;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001961 int ws_key_found = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001962
Christopher Faulet94b2c762019-05-24 15:28:57 +02001963 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001964 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1965
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001966 if (htx_is_empty(chn_htx))
1967 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001968
Christopher Faulet10a86702021-04-07 14:18:11 +02001969 if (h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01001970 goto end;
1971
Christopher Faulet51dbc942018-09-13 09:05:15 +02001972 if (!h1_get_buf(h1c, &h1c->obuf)) {
1973 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001974 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 +02001975 goto end;
1976 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001977
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001978 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001979
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001980 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001981 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001982
Willy Tarreau3815b222018-12-11 19:50:43 +01001983 /* Perform some optimizations to reduce the number of buffer copies.
1984 * First, if the mux's buffer is empty and the htx area contains
1985 * exactly one data block of the same size as the requested count,
1986 * then it's possible to simply swap the caller's buffer with the
1987 * mux's output buffer and adjust offsets and length to match the
1988 * entire DATA HTX block in the middle. In this case we perform a
1989 * true zero-copy operation from end-to-end. This is the situation
1990 * that happens all the time with large files. Second, if this is not
1991 * possible, but the mux's output buffer is empty, we still have an
1992 * opportunity to avoid the copy to the intermediary buffer, by making
1993 * the intermediary buffer's area point to the output buffer's area.
1994 * In this case we want to skip the HTX header to make sure that copies
1995 * remain aligned and that this operation remains possible all the
1996 * time. This goes for headers, data blocks and any data extracted from
1997 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001998 */
1999 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01002000 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002001 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01002002 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01002003 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01002004 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01002005 void *old_area;
2006
Christopher Faulet6b81df72019-10-01 22:08:43 +02002007 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 +01002008 if (h1m->state == H1_MSG_DATA) {
2009 if (h1m->flags & H1_MF_CLEN) {
2010 if (count > h1m->curr_len) {
2011 TRACE_ERROR("too much payload, more than announced",
2012 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2013 goto error;
2014 }
2015 h1m->curr_len -= count;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002016 if (!h1m->curr_len)
2017 last_data = 1;
Christopher Faulet140f1a52021-11-26 16:37:55 +01002018 }
2019 if (chn_htx->flags & HTX_FL_EOM) {
2020 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2021 last_data = 1;
2022 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002023 }
2024
Christopher Faulete5596bf2020-12-02 16:13:22 +01002025 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01002026 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02002027 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01002028 h1c->obuf.data = count;
2029
2030 buf->area = old_area;
2031 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01002032
Christopher Faulet6b81df72019-10-01 22:08:43 +02002033 chn_htx = (struct htx *)buf->area;
2034 htx_reset(chn_htx);
2035
Christopher Fauletadb22202018-12-12 10:32:09 +01002036 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002037 * size and eventually the last chunk. We have at least
2038 * the size of the struct htx to write the chunk
2039 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01002040 */
2041 if (h1m->flags & H1_MF_CHNK) {
2042 h1_emit_chunk_size(&h1c->obuf, count);
2043 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002044 if (last_data) {
2045 /* Emit the last chunk too at the buffer's end */
2046 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
2047 }
Christopher Fauletadb22202018-12-12 10:32:09 +01002048 }
2049
Christopher Faulet6b81df72019-10-01 22:08:43 +02002050 if (h1m->state == H1_MSG_DATA)
2051 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002052 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002053 else
2054 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002055 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002056
Christopher Faulete5596bf2020-12-02 16:13:22 +01002057 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002058 if (last_data) {
2059 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02002060 if (h1s->flags & H1S_F_RX_BLK) {
2061 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002062 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002063 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 +01002064 }
2065
2066 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2067 H1_EV_TX_DATA, h1c->conn, h1s);
2068 }
Willy Tarreau3815b222018-12-11 19:50:43 +01002069 goto out;
2070 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002071 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002072 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02002073 else
2074 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02002075
Christopher Fauletc2518a52019-06-25 21:41:02 +02002076 tmp.data = 0;
2077 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02002078 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01002079 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02002080 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01002081 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02002082 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02002083 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02002084
Christopher Fauletb2e84162018-12-06 11:39:49 +01002085 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002086 if (type != HTX_BLK_DATA && vlen > count)
2087 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002088
Christopher Faulet94b2c762019-05-24 15:28:57 +02002089 if (type == HTX_BLK_UNUSED)
2090 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02002091
Christopher Faulet94b2c762019-05-24 15:28:57 +02002092 switch (h1m->state) {
2093 case H1_MSG_RQBEFORE:
2094 if (type != HTX_BLK_REQ_SL)
2095 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002096 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 +02002097 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002098 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02002099 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002100 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002101 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002102 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002103 if (sl->flags & HTX_SL_F_BODYLESS)
2104 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02002105 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01002106 if (h1s->meth == HTTP_METH_HEAD)
2107 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02002108 if (h1s->flags & H1S_F_RX_BLK) {
2109 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002110 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002111 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 +02002112 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002113 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02002114
Christopher Faulet94b2c762019-05-24 15:28:57 +02002115 case H1_MSG_RPBEFORE:
2116 if (type != HTX_BLK_RES_SL)
2117 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002118 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 +02002119 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002120 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02002121 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002122 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002123 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01002124 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002125 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01002126 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02002127 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01002128 else if (h1s->status == 204 || h1s->status == 304)
2129 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02002130 h1m->state = H1_MSG_HDR_FIRST;
2131 break;
2132
Christopher Faulet94b2c762019-05-24 15:28:57 +02002133 case H1_MSG_HDR_FIRST:
2134 case H1_MSG_HDR_NAME:
2135 case H1_MSG_HDR_L2_LWS:
2136 if (type == HTX_BLK_EOH)
2137 goto last_lf;
2138 if (type != HTX_BLK_HDR)
2139 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002140
Christopher Faulet9768c262018-10-22 09:34:31 +02002141 h1m->state = H1_MSG_HDR_NAME;
2142 n = htx_get_blk_name(chn_htx, blk);
2143 v = htx_get_blk_value(chn_htx, blk);
2144
Christopher Faulet86d144c2019-08-14 16:32:25 +02002145 /* Skip all pseudo-headers */
2146 if (*(n.ptr) == ':')
2147 goto skip_hdr;
2148
Christopher Faulet91fcf212020-12-02 16:17:15 +01002149 if (isteq(n, ist("transfer-encoding"))) {
2150 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2151 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02002152 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01002153 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002154 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01002155 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2156 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01002157 /* Only skip C-L header with invalid value. */
2158 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01002159 goto skip_hdr;
2160 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002161 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01002162 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02002163 if (!v.len)
2164 goto skip_hdr;
2165 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01002166 else if (isteq(n, ist("upgrade"))) {
2167 h1_parse_upgrade_header(h1m, v);
2168 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002169 else if ((isteq(n, ist("sec-websocket-accept")) &&
2170 h1m->flags & H1_MF_RESP) ||
2171 (isteq(n, ist("sec-websocket-key")) &&
2172 !(h1m->flags & H1_MF_RESP))) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002173 ws_key_found = 1;
2174 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02002175 else if (isteq(n, ist("te"))) {
2176 /* "te" may only be sent with "trailers" if this value
2177 * is present, otherwise it must be deleted.
2178 */
2179 v = istist(v, ist("trailers"));
2180 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2181 goto skip_hdr;
2182 v = ist("trailers");
2183 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002184
Christopher Faulet67d58092019-10-02 10:51:38 +02002185 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002186 if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) &&
2187 isteqi(n, h1c->px->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02002188 goto skip_hdr;
2189
Christopher Faulet98fbe952019-07-22 16:18:24 +02002190 /* Try to adjust the case of the header name */
2191 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2192 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002193 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002194 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002195 skip_hdr:
2196 h1m->state = H1_MSG_HDR_L2_LWS;
2197 break;
2198
Christopher Faulet94b2c762019-05-24 15:28:57 +02002199 case H1_MSG_LAST_LF:
2200 if (type != HTX_BLK_EOH)
2201 goto error;
2202 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002203 h1m->state = H1_MSG_LAST_LF;
2204 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002205 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002206 /* If the reply comes from haproxy while the request is
2207 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002208 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2209 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2210 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002211 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2212 /* T-E + C-L: force close */
2213 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2214 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2215 }
2216 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2217 /* T-E + HTTP/1.0: force close */
2218 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2219 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2220 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002221
2222 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002223 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002224 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002225 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002226 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002227 /* Try to adjust the case of the header name */
2228 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2229 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002230 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002231 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002232 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002233 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002234 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002235
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002236 if ((h1s->meth != HTTP_METH_CONNECT &&
2237 (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 +01002238 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002239 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002240 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002241 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2242 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002243 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002244 n = ist("transfer-encoding");
2245 v = ist("chunked");
2246 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2247 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002248 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002249 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002250 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002251 h1m->flags |= H1_MF_CHNK;
2252 }
2253
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002254 /* Now add the server name to a header (if requested) */
2255 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002256 !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002257 struct server *srv = objt_server(h1c->conn->target);
2258
2259 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002260 n = h1c->px->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002261 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002262
2263 /* Try to adjust the case of the header name */
2264 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2265 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002266 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002267 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002268 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002269 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002270 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2271 }
2272
Amaury Denoyellec1938232020-12-11 17:53:03 +01002273 /* Add websocket handshake key if needed */
2274 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) &&
2275 !ws_key_found) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002276 if (!(h1m->flags & H1_MF_RESP)) {
2277 /* generate a random websocket key
2278 * stored in the session to
2279 * verify it on the response side
2280 */
2281 h1_generate_random_ws_input_key(h1s->ws_key);
2282
2283 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2284 ist(h1s->ws_key),
2285 &tmp)) {
2286 goto full;
2287 }
2288 }
2289 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002290 /* add the response header key */
2291 char key[29];
2292 h1_calculate_ws_output_key(h1s->ws_key, key);
2293 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2294 ist(key),
2295 &tmp)) {
2296 goto full;
2297 }
2298 }
2299 }
2300
Christopher Faulet6b81df72019-10-01 22:08:43 +02002301 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2302 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2303
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002304 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002305 if (!chunk_memcat(&tmp, "\r\n", 2))
2306 goto full;
2307 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002308 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002309 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002310 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002311 if (!chunk_memcat(&tmp, "\r\n", 2))
2312 goto full;
2313 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002314 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002315 else if ((h1m->flags & H1_MF_RESP) &&
2316 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002317 if (!chunk_memcat(&tmp, "\r\n", 2))
2318 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002319 h1m_init_res(&h1s->res);
2320 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002321 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002322 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002323 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002324 else {
Christopher Faulet2eb52432022-04-07 10:29:38 +02002325 /* EOM flag is set or empty payload (C-L to 0) and it is the last block */
2326 if (htx_is_unique_blk(chn_htx, blk) &&
2327 ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002328 if (h1m->flags & H1_MF_CHNK) {
2329 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2330 goto full;
2331 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002332 else if (!chunk_memcat(&tmp, "\r\n", 2))
2333 goto full;
2334 goto done;
2335 }
2336 else if (!chunk_memcat(&tmp, "\r\n", 2))
2337 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002338 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002339 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002340 break;
2341
Christopher Faulet94b2c762019-05-24 15:28:57 +02002342 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002343 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002344 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002345 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2346 goto trailers;
2347
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002348 /* If the message is not chunked, never
2349 * add the last chunk. */
2350 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002351 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002352 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 +02002353 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002354 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002355 else if (type != HTX_BLK_DATA)
2356 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002357
2358 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 +02002359
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002360 /* It is the last block of this message. After this one,
2361 * only tunneled data may be forwarded. */
2362 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2363 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2364 last_data = 1;
2365 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002366
2367 if (vlen > count) {
2368 /* Get the maximum amount of data we can xferred */
2369 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002370 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002371 }
2372
Christopher Faulet140f1a52021-11-26 16:37:55 +01002373 if (h1m->state == H1_MSG_DATA) {
2374 if (h1m->flags & H1_MF_CLEN) {
2375 if (vlen > h1m->curr_len) {
2376 TRACE_ERROR("too much payload, more than announced",
2377 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2378 goto error;
2379 }
Christopher Faulet140f1a52021-11-26 16:37:55 +01002380 }
2381 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2382 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2383 goto skip_data;
2384 }
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002385 }
2386
Christopher Fauletd9233f02019-10-14 14:01:24 +02002387 chklen = 0;
2388 if (h1m->flags & H1_MF_CHNK) {
2389 chklen = b_room(&tmp);
2390 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2391 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2392 (chklen < 1048576) ? 5 : 8);
2393 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002394
2395 /* If it is the end of the chunked message (without EOT), reserve the
2396 * last chunk size */
2397 if (last_data)
2398 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002399 }
2400
2401 if (vlen + chklen > b_room(&tmp)) {
2402 /* too large for the buffer */
2403 if (chklen >= b_room(&tmp))
2404 goto full;
2405 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002406 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002407 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002408 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002409 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002410 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002411 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002412
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002413 /* Space already reserved, so it must succeed */
2414 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2415 goto error;
2416
Christopher Faulet6b81df72019-10-01 22:08:43 +02002417 if (h1m->state == H1_MSG_DATA)
2418 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002419 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002420 else
2421 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002422 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002423
2424 skip_data:
Christopher Faulet2eb52432022-04-07 10:29:38 +02002425 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Fauletb4eca0e2022-01-10 17:27:51 +01002426 h1m->curr_len -= vlen;
Christopher Faulet2eb52432022-04-07 10:29:38 +02002427 if (!h1m->curr_len)
2428 last_data = 1;
2429 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002430 if (last_data)
2431 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002432 break;
2433
Christopher Faulet94b2c762019-05-24 15:28:57 +02002434 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002435 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002436 goto error;
2437 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002438 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002439
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002440 /* If the message is not chunked, ignore
2441 * trailers. It may happen with H2 messages. */
Christopher Faulet0a916d22021-02-10 08:48:19 +01002442 if (!(h1m->flags & H1_MF_CHNK)) {
2443 if (type == HTX_BLK_EOT)
2444 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002445 break;
Christopher Faulet0a916d22021-02-10 08:48:19 +01002446 }
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002447
Christopher Faulete5596bf2020-12-02 16:13:22 +01002448 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2449 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 +01002450 if (type == HTX_BLK_EOT)
2451 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002452 break;
2453 }
2454
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002455 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002456 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002457 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002458 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2459 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002460 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002461 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002462 else { // HTX_BLK_TLR
2463 n = htx_get_blk_name(chn_htx, blk);
2464 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002465
2466 /* Try to adjust the case of the header name */
2467 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2468 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002469 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002470 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002471 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002472 break;
2473
Christopher Faulet94b2c762019-05-24 15:28:57 +02002474 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002475 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2476 goto error; /* For now return an error */
2477
Christopher Faulet94b2c762019-05-24 15:28:57 +02002478 done:
Christopher Faulet2eb52432022-04-07 10:29:38 +02002479 if (!(chn_htx->flags & HTX_FL_EOM) && (!(h1m->flags & H1_MF_CLEN) || h1m->curr_len)) {
Christopher Faulet36893672021-02-10 09:52:07 +01002480 TRACE_STATE("No EOM flags in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2481 goto error; /* For now return an error */
2482 }
2483
Christopher Faulet94b2c762019-05-24 15:28:57 +02002484 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002485 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002486 h1s->flags |= H1S_F_TX_BLK;
2487 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002488 }
2489 else if ((h1m->flags & H1_MF_RESP) &&
2490 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2491 /* a successful reply to a CONNECT or a protocol switching is sent
2492 * to the client. Switch the response to tunnel mode.
2493 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002494 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002495 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 +01002496 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002497
Christopher Faulet10a86702021-04-07 14:18:11 +02002498 if (h1s->flags & H1S_F_RX_BLK) {
2499 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002500 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002501 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 +02002502 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002503
2504 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2505 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002506 break;
2507
Christopher Faulet9768c262018-10-22 09:34:31 +02002508 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002509 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002510 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002511 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002512 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002513 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002514 TRACE_ERROR("processing output error, set error on h1c/h1s",
2515 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 +01002516 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02002517 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002518
2519 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002520 total += vlen;
2521 count -= vlen;
2522 if (sz == vlen)
2523 blk = htx_remove_blk(chn_htx, blk);
2524 else {
2525 htx_cut_data_blk(chn_htx, blk, vlen);
2526 break;
2527 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002528 }
2529
Christopher Faulet9768c262018-10-22 09:34:31 +02002530 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002531 /* when the output buffer is empty, tmp shares the same area so that we
2532 * only have to update pointers and lengths.
2533 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002534 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2535 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002536 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002537 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002538
Willy Tarreau3815b222018-12-11 19:50:43 +01002539 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002540 out:
2541 if (!buf_room_for_htx_data(&h1c->obuf)) {
2542 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002543 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002544 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002545 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002546 /* Both the request and the response reached the DONE state. So set EOI
2547 * flag on the conn-stream. Most of time, the flag will already be set,
2548 * except for protocol upgrades. Report an error if data remains blocked
2549 * in the output buffer.
2550 */
2551 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2552 if (!htx_is_empty(chn_htx)) {
2553 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002554 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 +01002555 }
2556 h1s->cs->flags |= CS_FL_EOI;
2557 }
2558
Christopher Faulet6b81df72019-10-01 22:08:43 +02002559 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002560 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002561
2562 full:
2563 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2564 h1c->flags |= H1C_F_OUT_FULL;
2565 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002566}
2567
Christopher Faulet51dbc942018-09-13 09:05:15 +02002568/*********************************************************/
2569/* functions below are I/O callbacks from the connection */
2570/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002571static void h1_wake_stream_for_recv(struct h1s *h1s)
2572{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002573 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002574 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002575 tasklet_wakeup(h1s->subs->tasklet);
2576 h1s->subs->events &= ~SUB_RETRY_RECV;
2577 if (!h1s->subs->events)
2578 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002579 }
2580}
2581static void h1_wake_stream_for_send(struct h1s *h1s)
2582{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002583 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002584 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002585 tasklet_wakeup(h1s->subs->tasklet);
2586 h1s->subs->events &= ~SUB_RETRY_SEND;
2587 if (!h1s->subs->events)
2588 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002589 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002590}
2591
Christopher Fauletad4daf62021-01-21 17:49:01 +01002592/* alerts the data layer following this sequence :
2593 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2594 * - if its subscribed to send, then it's woken up for send
2595 * - if it was subscribed to neither, its ->wake() callback is called
2596 */
2597static void h1_alert(struct h1s *h1s)
2598{
2599 if (h1s->subs) {
2600 h1_wake_stream_for_recv(h1s);
2601 h1_wake_stream_for_send(h1s);
2602 }
2603 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2604 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2605 h1s->cs->data_cb->wake(h1s->cs);
2606 }
2607}
2608
Christopher Fauletc18fc232020-10-06 17:41:36 +02002609/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2610 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2611 * retryable errors (allocation error or buffer full). On success, the error is
2612 * copied in the output buffer.
2613*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002614static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002615{
2616 int rc = http_get_status_idx(h1c->errcode);
2617 int ret = 0;
2618
2619 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2620
2621 /* Verify if the error is mapped on /dev/null or any empty file */
2622 /// XXX: do a function !
2623 if (h1c->px->replies[rc] &&
2624 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2625 h1c->px->replies[rc]->body.errmsg &&
2626 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2627 /* Empty error, so claim a success */
2628 ret = 1;
2629 goto out;
2630 }
2631
2632 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2633 h1c->flags |= H1C_F_ERR_PENDING;
2634 goto out;
2635 }
2636
2637 if (!h1_get_buf(h1c, &h1c->obuf)) {
2638 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2639 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2640 goto out;
2641 }
Tim Duesterhus77508502022-03-15 13:11:06 +01002642 ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc]));
Christopher Fauletc18fc232020-10-06 17:41:36 +02002643 if (unlikely(ret <= 0)) {
2644 if (!ret) {
2645 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2646 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2647 goto out;
2648 }
2649 else {
2650 /* we cannot report this error, so claim a success */
2651 ret = 1;
2652 }
2653 }
2654 h1c->flags &= ~H1C_F_ERR_PENDING;
2655 out:
2656 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2657 return ret;
2658}
2659
2660/* Try to send a 500 internal error. It relies on h1_send_error to send the
2661 * error. This function takes care of incrementing stats and tracked counters.
2662 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002663static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002664{
2665 struct session *sess = h1c->conn->owner;
2666 int ret = 1;
2667
2668 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002669 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002670 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2671 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002672 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002673 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002674
2675 h1c->errcode = 500;
2676 ret = h1_send_error(h1c);
2677 sess_log(sess);
2678 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002679}
2680
Christopher Fauletb3230f72021-09-21 18:38:20 +02002681/* Try to send an error because of a parsing error. By default a 400 bad request
2682 * error is returned. But the status code may be specified by setting
2683 * h1c->errcode. It relies on h1_send_error to send the error. This function
2684 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002685 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002686static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002687{
2688 struct session *sess = h1c->conn->owner;
2689 int ret = 1;
2690
2691 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2692 goto end;
2693
2694 session_inc_http_req_ctr(sess);
2695 session_inc_http_err_ctr(sess);
2696 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002697 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2698 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002699 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002700 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002701
Christopher Fauletb3230f72021-09-21 18:38:20 +02002702 if (!h1c->errcode)
2703 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002704 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002705 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2706 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002707
2708 end:
2709 return ret;
2710}
2711
Christopher Faulet2eed8002020-12-07 11:26:13 +01002712/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2713 * the error. This function takes care of incrementing stats and tracked
2714 * counters.
2715 */
2716static int h1_handle_not_impl_err(struct h1c *h1c)
2717{
2718 struct session *sess = h1c->conn->owner;
2719 int ret = 1;
2720
2721 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2722 goto end;
2723
2724 session_inc_http_req_ctr(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002725 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002726 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2727 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002728 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002729 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002730
2731 h1c->errcode = 501;
2732 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002733 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2734 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002735
2736 end:
2737 return ret;
2738}
2739
Christopher Fauletc18fc232020-10-06 17:41:36 +02002740/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2741 * error. This function takes care of incrementing stats and tracked counters.
2742 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002743static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002744{
2745 struct session *sess = h1c->conn->owner;
2746 int ret = 1;
2747
2748 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2749 goto end;
2750
2751 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002752 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002753 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2754 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002755 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002756 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002757
2758 h1c->errcode = 408;
Christopher Faulet07e10de2021-07-26 09:42:49 +02002759 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2760 ret = h1_send_error(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002761 sess_log(sess);
2762 end:
2763 return ret;
2764}
2765
2766
Christopher Faulet51dbc942018-09-13 09:05:15 +02002767/*
2768 * Attempt to read data, and subscribe if none available
2769 */
2770static int h1_recv(struct h1c *h1c)
2771{
2772 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002773 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002774 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002775
Christopher Faulet6b81df72019-10-01 22:08:43 +02002776 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2777
2778 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2779 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002780 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002781 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002782
Christopher Fauletae635762020-09-21 11:47:16 +02002783 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2784 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002785 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002786 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002787
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002788 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2789 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002790 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002791 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002792 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002793
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002794 /*
2795 * If we only have a small amount of data, realign it,
2796 * it's probably cheaper than doing 2 recv() calls.
2797 */
2798 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002799 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002800
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002801 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002802 if (!h1c->h1s ||
2803 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2804 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002805 flags |= CO_RFL_READ_ONCE;
2806
Willy Tarreau45f2b892018-12-05 07:59:27 +01002807 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002808 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002809 if (h1c->flags & H1C_F_IN_FULL) {
2810 h1c->flags &= ~H1C_F_IN_FULL;
2811 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2812 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002813
2814 if (!b_data(&h1c->ibuf)) {
2815 /* try to pre-align the buffer like the rxbufs will be
2816 * to optimize memory copies.
2817 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002818 h1c->ibuf.head = sizeof(struct htx);
2819 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002820 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet41951ab2021-11-26 18:12:51 +01002821 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002822 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002823 if (max && !ret && h1_recv_allowed(h1c)) {
2824 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2825 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002826 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002827 else {
2828 h1_wake_stream_for_recv(h1c->h1s);
2829 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002830 }
2831
Christopher Faulet51dbc942018-09-13 09:05:15 +02002832 if (!b_data(&h1c->ibuf))
2833 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002834 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002835 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002836 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2837 }
2838
2839 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002840 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002841}
2842
2843
2844/*
2845 * Try to send data if possible
2846 */
2847static int h1_send(struct h1c *h1c)
2848{
2849 struct connection *conn = h1c->conn;
2850 unsigned int flags = 0;
2851 size_t ret;
2852 int sent = 0;
2853
Christopher Faulet6b81df72019-10-01 22:08:43 +02002854 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2855
2856 if (conn->flags & CO_FL_ERROR) {
2857 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002858 b_reset(&h1c->obuf);
2859 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002860 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002861
2862 if (!b_data(&h1c->obuf))
2863 goto end;
2864
Christopher Faulet46230362019-10-17 16:04:20 +02002865 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002866 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002867 if (h1c->flags & H1C_F_CO_STREAMER)
2868 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002869
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002870 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002871 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002872 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002873 if (h1c->flags & H1C_F_OUT_FULL) {
2874 h1c->flags &= ~H1C_F_OUT_FULL;
2875 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2876 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01002877 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002878 b_del(&h1c->obuf, ret);
2879 sent = 1;
2880 }
2881
Christopher Faulet145aa472018-12-06 10:56:20 +01002882 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002883 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002884 /* error or output closed, nothing to send, clear the buffer to release it */
2885 b_reset(&h1c->obuf);
2886 }
2887
Christopher Faulet51dbc942018-09-13 09:05:15 +02002888 end:
Christopher Fauletc17c31c2022-02-01 18:25:06 +01002889 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002890 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002891
Christopher Faulet51dbc942018-09-13 09:05:15 +02002892 /* We're done, no more to send */
2893 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002894 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002895 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002896 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002897 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002898 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002899 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002900 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002901 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2902 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002903 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002904 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002905
Christopher Faulet6b81df72019-10-01 22:08:43 +02002906 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002907 return sent;
2908}
2909
Christopher Faulet51dbc942018-09-13 09:05:15 +02002910/* callback called on any event by the connection handler.
2911 * It applies changes and returns zero, or < 0 if it wants immediate
2912 * destruction of the connection.
2913 */
2914static int h1_process(struct h1c * h1c)
2915{
2916 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002917 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002918
Christopher Faulet6b81df72019-10-01 22:08:43 +02002919 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2920
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002921 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002922 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Fauletab7389d2021-09-16 08:16:23 +02002923 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2924 !(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 +02002925 struct buffer *buf;
2926 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002927
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002928 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2929 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002930 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002931
2932 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002933 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002934 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2935 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002936 /* Try to match H2 preface before parsing the request headers. */
2937 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2938 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002939 if (h1c->flags & H1C_F_ST_ATTACHED) {
2940 /* Force the REOS here to be sure to release the CS.
2941 Here ATTACHED implies !READY, and h1s defined
2942 */
2943 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2944 h1s->flags |= H1S_F_REOS;
2945 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002946 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002947 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002948 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002949 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002950
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002951 /* Create the H1 stream if not already there */
2952 if (!h1s) {
2953 h1s = h1c_frt_stream_new(h1c);
2954 if (!h1s) {
2955 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002956 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002957 goto no_parsing;
2958 }
2959 }
2960
2961 if (h1s->sess->t_idle == -1)
2962 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2963
2964 /* Get the stream rxbuf */
2965 buf = h1_get_buf(h1c, &h1s->rxbuf);
2966 if (!buf) {
2967 h1c->flags |= H1C_F_IN_SALLOC;
2968 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2969 return 0;
2970 }
2971
2972 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01002973 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002974 h1_release_buf(h1c, &h1s->rxbuf);
2975 h1_set_idle_expiration(h1c);
2976
2977 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002978 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002979 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002980 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet26a26432021-01-27 11:27:50 +01002981 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002982 }
2983 else if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02002984 h1_handle_parsing_error(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002985 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002986 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002987 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002988 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2989 h1_handle_not_impl_err(h1c);
2990 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002991 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002992 }
Christopher Fauletae635762020-09-21 11:47:16 +02002993 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002994 h1_send(h1c);
2995
Christopher Faulet75308302021-11-15 14:51:37 +01002996 /* H1 connection must be released ASAP if:
2997 * - an error occurred on the connection or the H1C or
2998 * - a read0 was received or
2999 * - a silent shutdown was emitted and all outgoing data sent
3000 */
3001 if ((conn->flags & CO_FL_ERROR) ||
3002 conn_xprt_read0_pending(conn) ||
3003 (h1c->flags & H1C_F_ST_ERROR) ||
3004 ((h1c->flags & H1C_F_ST_SILENT_SHUT) && !b_data(&h1c->obuf))) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003005 if (!(h1c->flags & H1C_F_ST_READY)) {
3006 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003007 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet75308302021-11-15 14:51:37 +01003008 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN))) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02003009 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003010 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003011 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003012 }
3013
3014 /* Handle pending error, if any (only possible on frontend connection) */
3015 if (h1c->flags & H1C_F_ERR_PENDING) {
3016 BUG_ON(h1c->flags & H1C_F_IS_BACK);
3017 if (h1_send_error(h1c))
3018 h1_send(h1c);
3019 }
Christopher Fauletae635762020-09-21 11:47:16 +02003020
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003021 /* If there is some pending outgoing data or error, just wait */
3022 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
3023 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01003024
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003025 /* Otherwise we can release the H1 connection */
3026 goto release;
3027 }
3028 else {
3029 /* Here there is still a H1 stream with a conn-stream.
3030 * Report the connection state at the stream level
3031 */
3032 if (conn_xprt_read0_pending(conn)) {
3033 h1s->flags |= H1S_F_REOS;
3034 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
3035 }
Willy Tarreau99bbdbc2022-03-17 17:10:36 +01003036 if ((h1c->flags & H1C_F_ST_ERROR) || ((conn->flags & CO_FL_ERROR) && !b_data(&h1c->ibuf)))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003037 h1s->cs->flags |= CS_FL_ERROR;
3038 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01003039 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003040 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003041 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003042
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003043 if (!b_data(&h1c->ibuf))
3044 h1_release_buf(h1c, &h1c->ibuf);
3045
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003046 /* Check if a soft-stop is in progress.
3047 * Release idling front connection if this is the case.
3048 */
3049 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003050 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
William Dauchya9dd9012022-01-05 22:53:24 +01003051 if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) &&
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02003052 h1c->flags & H1C_F_WAIT_NEXT_REQ) {
3053
3054 int send_close = 1;
3055 /* If a close-spread-time option is set, we want to avoid
3056 * closing all the active HTTP2 connections at once so we add a
3057 * random factor that will spread the closing.
3058 */
3059 if (tick_isset(global.close_spread_end)) {
3060 int remaining_window = tick_remain(now_ms, global.close_spread_end);
3061 if (remaining_window) {
3062 /* This should increase the closing rate the
3063 * further along the window we are.
3064 */
3065 send_close = (remaining_window <= statistical_prng_range(global.close_spread_time));
3066 }
3067 }
3068 if (send_close)
3069 goto release;
3070 }
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02003071 }
3072 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003073
3074 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
3075 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
3076 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003077 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003078
Christopher Faulet47365272018-10-31 17:40:50 +01003079 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02003080 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003081 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003082 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01003083
3084 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003085 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003086 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003087 * attached CS first. Here, the H1C must not be READY */
3088 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
3089
3090 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
3091 h1s->cs->flags |= CS_FL_EOS;
3092 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
3093 h1s->cs->flags |= CS_FL_ERROR;
3094 h1_alert(h1s);
3095 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
3096 }
3097 else {
3098 h1_release(h1c);
3099 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
3100 }
Christopher Faulet539e0292018-11-19 10:40:09 +01003101 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003102}
3103
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003104struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003105{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003106 struct connection *conn;
3107 struct tasklet *tl = (struct tasklet *)t;
3108 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003109 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003110 int ret = 0;
3111
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003112 if (state & TASK_F_USR1) {
3113 /* the tasklet was idling on an idle connection, it might have
3114 * been stolen, let's be careful!
3115 */
3116 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3117 if (tl->context == NULL) {
3118 /* The connection has been taken over by another thread,
3119 * we're no longer responsible for it, so just free the
3120 * tasklet, and do nothing.
3121 */
3122 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3123 tasklet_free(tl);
3124 return NULL;
3125 }
3126 conn = h1c->conn;
3127 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003128
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003129 /* Remove the connection from the list, to be sure nobody attempts
3130 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003131 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003132 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3133 if (conn_in_list)
3134 conn_delete_from_tree(&conn->hash_node->node);
3135
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003136 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003137 } else {
3138 /* we're certain the connection was not in an idle list */
3139 conn = h1c->conn;
3140 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
3141 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003142 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003143
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003144 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003145 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003146 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003147 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003148 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003149 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01003150
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003151 /* If we were in an idle list, we want to add it back into it,
3152 * unless h1_process() returned -1, which mean it has destroyed
3153 * the connection (testing !ret is enough, if h1_process() wasn't
3154 * called then ret will be 0 anyway.
3155 */
Willy Tarreau74163142021-03-13 11:30:19 +01003156 if (ret < 0)
3157 t = NULL;
3158
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003159 if (!ret && conn_in_list) {
3160 struct server *srv = objt_server(conn->target);
3161
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003162 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003163 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003164 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003165 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003166 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003167 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003168 }
Willy Tarreau74163142021-03-13 11:30:19 +01003169 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003170}
3171
Christopher Faulet51dbc942018-09-13 09:05:15 +02003172static int h1_wake(struct connection *conn)
3173{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003174 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01003175 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003176
Christopher Faulet6b81df72019-10-01 22:08:43 +02003177 TRACE_POINT(H1_EV_H1C_WAKE, conn);
3178
Christopher Faulet539e0292018-11-19 10:40:09 +01003179 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01003180 ret = h1_process(h1c);
3181 if (ret == 0) {
3182 struct h1s *h1s = h1c->h1s;
3183
Christopher Fauletad4daf62021-01-21 17:49:01 +01003184 if (h1c->flags & H1C_F_ST_ATTACHED)
3185 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003186 }
3187 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003188}
3189
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003190/* Connection timeout management. The principle is that if there's no receipt
3191 * nor sending for a certain amount of time, the connection is closed.
3192 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003193struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003194{
3195 struct h1c *h1c = context;
3196 int expired = tick_is_expired(t->expire, now_ms);
3197
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003198 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003199
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003200 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003201 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003202 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003203
3204 /* Somebody already stole the connection from us, so we should not
3205 * free it, we just have to free the task.
3206 */
3207 if (!t->context) {
3208 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003209 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003210 goto do_leave;
3211 }
3212
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003213 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003214 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003215 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003216 return t;
3217 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003218
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003219 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003220 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003221 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003222 if (h1c->flags & H1C_F_ST_READY) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003223 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003224 t->expire = TICK_ETERNITY;
3225 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
3226 return t;
3227 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003228
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003229 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003230 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
3231 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003232 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003233 if (h1_handle_req_tout(h1c))
3234 h1_send(h1c);
3235 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
3236 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003237 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003238 return t;
3239 }
3240 }
3241
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003242 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003243 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003244 * attached CS first. Here, the H1C must not be READY */
3245 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
3246 h1_alert(h1c->h1s);
3247 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003248 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003249 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
3250 return t;
3251 }
3252
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003253 /* We're about to destroy the connection, so make sure nobody attempts
3254 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003255 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003256 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003257 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003258
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003259 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003260 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003261
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003262 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003263 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003264
3265 if (!h1c) {
3266 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003267 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003268 return NULL;
3269 }
3270
3271 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003272 h1_release(h1c);
3273 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003274 return NULL;
3275}
3276
Christopher Faulet51dbc942018-09-13 09:05:15 +02003277/*******************************************/
3278/* functions below are used by the streams */
3279/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003280
Christopher Faulet51dbc942018-09-13 09:05:15 +02003281/*
3282 * Attach a new stream to a connection
3283 * (Used for outgoing connections)
3284 */
Christopher Faulete00ad352021-12-16 14:44:31 +01003285static int h1_attach(struct connection *conn, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003286{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003287 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003288 struct h1s *h1s;
3289
Christopher Faulet6b81df72019-10-01 22:08:43 +02003290 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003291 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003292 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3293 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003294 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003295
Christopher Faulet2f0ec662020-09-24 10:30:15 +02003296 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003297 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003298 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3299 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003300 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003301
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003302 /* the connection is not idle anymore, let's mark this */
3303 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003304 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003305
Christopher Faulet6b81df72019-10-01 22:08:43 +02003306 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulete00ad352021-12-16 14:44:31 +01003307 return 0;
Christopher Faulet26a26432021-01-27 11:27:50 +01003308 err:
Christopher Faulet26a26432021-01-27 11:27:50 +01003309 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 +01003310 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003311}
3312
3313/* Retrieves a valid conn_stream from this connection, or returns NULL. For
3314 * this mux, it's easy as we can only store a single conn_stream.
3315 */
3316static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
3317{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003318 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003319 struct h1s *h1s = h1c->h1s;
3320
3321 if (h1s)
3322 return h1s->cs;
3323
3324 return NULL;
3325}
3326
Christopher Faulet73c12072019-04-08 11:23:22 +02003327static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003328{
Christopher Faulet73c12072019-04-08 11:23:22 +02003329 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003330
Christopher Faulet6b81df72019-10-01 22:08:43 +02003331 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003332 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003333 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003334}
3335
3336/*
3337 * Detach the stream from the connection and possibly release the connection.
3338 */
3339static void h1_detach(struct conn_stream *cs)
3340{
3341 struct h1s *h1s = cs->ctx;
3342 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003343 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003344 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003345
Christopher Faulet6b81df72019-10-01 22:08:43 +02003346 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3347
Christopher Faulet51dbc942018-09-13 09:05:15 +02003348 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003349 if (!h1s) {
3350 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003351 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003352 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003353
Olivier Houchardf502aca2018-12-14 19:42:40 +01003354 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003355 h1c = h1s->h1c;
3356 h1s->cs = NULL;
3357
Christopher Faulet42849b02020-10-05 11:35:17 +02003358 sess->accept_date = date;
3359 sess->tv_accept = now;
3360 sess->t_handshake = 0;
3361 sess->t_idle = -1;
3362
Olivier Houchard8a786902018-12-15 16:05:40 +01003363 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3364 h1s_destroy(h1s);
3365
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003366 if ((h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_IDLE)) == (H1C_F_IS_BACK|H1C_F_ST_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02003367 /* If there are any excess server data in the input buffer,
3368 * release it and close the connection ASAP (some data may
3369 * remain in the output buffer). This happens if a server sends
3370 * invalid responses. So in such case, we don't want to reuse
3371 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003372 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003373 if (b_data(&h1c->ibuf)) {
3374 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003375 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003376 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003377 goto release;
3378 }
Christopher Faulet03627242019-07-19 11:34:08 +02003379
Christopher Faulet08016ab2020-07-01 16:10:06 +02003380 if (h1c->conn->flags & CO_FL_PRIVATE) {
3381 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003382 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3383 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003384 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003385 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003386 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003387 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003388 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003389 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003390 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3391 goto end;
3392 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003393 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003394 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003395 if (h1c->conn->owner == sess)
3396 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003397
3398 /* mark that the tasklet may lose its context to another thread and
3399 * that the handler needs to check it under the idle conns lock.
3400 */
3401 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003402 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003403 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3404
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003405 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003406 /* The server doesn't want it, let's kill the connection right away */
3407 h1c->conn->mux->destroy(h1c);
3408 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3409 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003410 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003411 /* At this point, the connection has been added to the
3412 * server idle list, so another thread may already have
3413 * hijacked it, so we can't do anything with it.
3414 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003415 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003416 }
3417 }
3418
Christopher Fauletf1204b82019-07-19 14:51:06 +02003419 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003420 /* 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 +01003421 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02003422 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003423 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003424 !h1c->conn->owner) {
3425 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003426 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003427 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003428 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003429 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003430 /* If we have a new request, process it immediately or
3431 * subscribe for reads waiting for new data
3432 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003433 if (unlikely(b_data(&h1c->ibuf))) {
3434 if (h1_process(h1c) == -1)
3435 goto end;
3436 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003437 else
3438 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3439 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003440 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003441 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003442 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003443 end:
3444 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003445}
3446
3447
3448static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3449{
3450 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02003451 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003452
3453 if (!h1s)
3454 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003455 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003456
Christopher Faulet99293b02021-11-10 10:30:15 +01003457 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003458
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003459 if (cs->flags & CS_FL_SHR)
3460 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003461 if (cs->flags & CS_FL_KILL_CONN) {
3462 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
3463 goto do_shutr;
3464 }
3465 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3466 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003467 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003468 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003469
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003470 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3471 /* Here attached is implicit because there is CS */
3472 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3473 goto end;
3474 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003475 if (h1s->flags & H1S_F_WANT_KAL) {
3476 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003477 goto end;
3478 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003479
Christopher Faulet7f366362019-04-08 10:51:20 +02003480 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003481 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
3482 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003483 goto end;
Christopher Faulet897d6122021-12-17 17:28:35 +01003484
3485 if (conn_xprt_ready(h1c->conn) && h1c->conn->xprt->shutr)
3486 h1c->conn->xprt->shutr(h1c->conn, h1c->conn->xprt_ctx, (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003487 end:
3488 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003489}
3490
3491static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3492{
3493 struct h1s *h1s = cs->ctx;
3494 struct h1c *h1c;
3495
3496 if (!h1s)
3497 return;
3498 h1c = h1s->h1c;
3499
Christopher Faulet99293b02021-11-10 10:30:15 +01003500 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003501
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003502 if (cs->flags & CS_FL_SHW)
3503 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003504 if (cs->flags & CS_FL_KILL_CONN) {
3505 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003506 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003507 }
3508 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3509 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3510 goto do_shutw;
3511 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003512
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003513 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3514 /* Here attached is implicit because there is CS */
3515 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3516 goto end;
3517 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003518 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3519 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003520 goto end;
3521 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003522
Christopher Faulet7f366362019-04-08 10:51:20 +02003523 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003524 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Fauleta85c5222021-10-27 15:36:38 +02003525 if (mode != CS_SHW_NORMAL)
3526 h1c->flags |= H1C_F_ST_SILENT_SHUT;
3527
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003528 if (!b_data(&h1c->obuf))
Christopher Faulet897d6122021-12-17 17:28:35 +01003529 h1_shutw_conn(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003530 end:
3531 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003532}
3533
Christopher Fauleta85c5222021-10-27 15:36:38 +02003534static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003535{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003536 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003537
Willy Tarreau62592ad2021-03-26 09:09:42 +01003538 if (conn->flags & CO_FL_SOCK_WR_SH)
3539 return;
3540
Christopher Fauleta85c5222021-10-27 15:36:38 +02003541 TRACE_ENTER(H1_EV_H1C_END, conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003542 conn_xprt_shutw(conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02003543 conn_sock_shutw(conn, (h1c && !(h1c->flags & H1C_F_ST_SILENT_SHUT)));
3544 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003545}
3546
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003547/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3548 * The <es> pointer is not allowed to differ from the one passed to the
3549 * subscribe() call. It always returns zero.
3550 */
3551static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003552{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003553 struct h1s *h1s = cs->ctx;
3554
3555 if (!h1s)
3556 return 0;
3557
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003558 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003559 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003560
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003561 es->events &= ~event_type;
3562 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003563 h1s->subs = NULL;
3564
3565 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003566 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003567
3568 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003569 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003570
Christopher Faulet51dbc942018-09-13 09:05:15 +02003571 return 0;
3572}
3573
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003574/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3575 * event subscriber <es> is not allowed to change from a previous call as long
3576 * as at least one event is still subscribed. The <event_type> must only be a
3577 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3578 * the conn_stream <cs> was already detached, in which case it will return -1.
3579 */
3580static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003581{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003582 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003583 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003584
3585 if (!h1s)
3586 return -1;
3587
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003588 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003589 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003590
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003591 es->events |= event_type;
3592 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003593
3594 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003595 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003596
3597
Christopher Faulet6b81df72019-10-01 22:08:43 +02003598 if (event_type & SUB_RETRY_SEND) {
3599 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003600 /*
3601 * If the conn_stream attempt to subscribe, and the
3602 * mux isn't subscribed to the connection, then it
3603 * probably means the connection wasn't established
3604 * yet, so we have to subscribe.
3605 */
3606 h1c = h1s->h1c;
3607 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3608 h1c->conn->xprt->subscribe(h1c->conn,
3609 h1c->conn->xprt_ctx,
3610 SUB_RETRY_SEND,
3611 &h1c->wait_event);
3612 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003613 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003614}
3615
Christopher Faulet564e39c2021-09-21 15:50:55 +02003616/* Called from the upper layer, to receive data.
3617 *
3618 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3619 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3620 * means the caller wants to flush input data (from the mux buffer and the
3621 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3622 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3623 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3624 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3625 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3626 * copy as much data as possible.
3627 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003628static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3629{
3630 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003631 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003632 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003633 size_t ret = 0;
3634
Willy Tarreau022e5e52020-09-10 09:33:15 +02003635 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003636
3637 /* Do nothing for now if not READY */
3638 if (!(h1c->flags & H1C_F_ST_READY)) {
3639 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3640 goto end;
3641 }
3642
Christopher Faulet539e0292018-11-19 10:40:09 +01003643 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003644 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003645 else
3646 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003647
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003648 if ((flags & CO_RFL_BUF_FLUSH) && (cs->flags & CS_FL_MAY_SPLICE)) {
3649 h1c->flags |= H1C_F_WANT_SPLICE;
3650 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 +02003651 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003652 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003653 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 +01003654 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003655 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003656
3657 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003658 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003659 return ret;
3660}
3661
3662
3663/* Called from the upper layer, to send data */
3664static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3665{
3666 struct h1s *h1s = cs->ctx;
3667 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003668 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003669
3670 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003671 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003672 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003673
Willy Tarreau022e5e52020-09-10 09:33:15 +02003674 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003675
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003676 /* If we're not connected yet, or we're waiting for a handshake, stop
3677 * now, as we don't want to remove everything from the channel buffer
3678 * before we're sure we can send it.
3679 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003680 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003681 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003682 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003683 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003684
Christopher Fauletdea24742021-01-22 15:12:30 +01003685 if (h1c->flags & H1C_F_ST_ERROR) {
3686 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003687 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 +01003688 return 0;
3689 }
3690
Christopher Faulet46230362019-10-17 16:04:20 +02003691 /* Inherit some flags from the upper layer */
3692 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3693 if (flags & CO_SFL_MSG_MORE)
3694 h1c->flags |= H1C_F_CO_MSG_MORE;
3695 if (flags & CO_SFL_STREAMER)
3696 h1c->flags |= H1C_F_CO_STREAMER;
3697
Christopher Fauletc31872f2019-06-04 22:09:36 +02003698 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003699 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003700
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003701 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003702 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003703 else
3704 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003705
3706 if ((count - ret) > 0)
3707 h1c->flags |= H1C_F_CO_MSG_MORE;
3708
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003709 if (!ret)
3710 break;
3711 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003712 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003713 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003714 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003715 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003716
3717 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletdea24742021-01-22 15:12:30 +01003718 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003719 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 +01003720 }
3721
Christopher Faulet7a145d62020-08-05 11:31:16 +02003722 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003723 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003724 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003725}
3726
Willy Tarreaue5733232019-05-22 19:24:06 +02003727#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003728/* Send and get, using splicing */
3729static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3730{
3731 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003732 struct h1c *h1c = h1s->h1c;
3733 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003734 int ret = 0;
3735
Christopher Faulet897d6122021-12-17 17:28:35 +01003736 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003737
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003738 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003739 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003740 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 +02003741 goto end;
3742 }
3743
Christopher Fauletcf307562021-07-26 10:49:39 +02003744 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003745 if (h1s_data_pending(h1s)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003746 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003747 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003748 }
3749
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003750 if (!h1_recv_allowed(h1c)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003751 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet0060be92020-07-03 15:02:25 +02003752 goto end;
3753 }
3754
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003755 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003756 count = h1m->curr_len;
Christopher Faulet897d6122021-12-17 17:28:35 +01003757 ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003758 if (ret >= 0) {
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003759 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003760 if (ret > h1m->curr_len) {
3761 h1s->flags |= H1S_F_PARSING_ERROR;
3762 h1c->flags |= H1C_F_ST_ERROR;
3763 cs->flags |= CS_FL_ERROR;
3764 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003765 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 +01003766 goto end;
3767 }
3768 h1m->curr_len -= ret;
3769 if (!h1m->curr_len) {
3770 h1m->state = H1_MSG_DONE;
3771 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003772 TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003773 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003774 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003775 HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret);
3776 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret);
Christopher Faulete18777b2019-04-16 16:46:36 +02003777 }
3778
Christopher Faulet1be55f92018-10-02 15:59:23 +02003779 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003780 if (conn_xprt_read0_pending(h1c->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003781 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003782 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003783 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003784 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003785
Christopher Faulet94d35102021-04-09 11:58:49 +02003786 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003787 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003788 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet94d35102021-04-09 11:58:49 +02003789 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003790 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s);
3791 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet94d35102021-04-09 11:58:49 +02003792 }
Christopher Faulet27182292020-07-03 15:08:49 +02003793 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003794
Christopher Faulet897d6122021-12-17 17:28:35 +01003795 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003796 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003797}
3798
3799static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3800{
3801 struct h1s *h1s = cs->ctx;
Christopher Faulet140f1a52021-11-26 16:37:55 +01003802 struct h1c *h1c = h1s->h1c;
3803 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003804 int ret = 0;
3805
Christopher Faulet897d6122021-12-17 17:28:35 +01003806 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003807
Christopher Faulet140f1a52021-11-26 16:37:55 +01003808 if (b_data(&h1c->obuf)) {
3809 if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
Christopher Faulet897d6122021-12-17 17:28:35 +01003810 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s);
3811 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003812 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003813 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003814 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003815
Christopher Faulet897d6122021-12-17 17:28:35 +01003816 ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe);
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003817 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003818 if (ret > h1m->curr_len) {
3819 h1s->flags |= H1S_F_PROCESSING_ERROR;
3820 h1c->flags |= H1C_F_ST_ERROR;
3821 cs->flags |= CS_FL_ERROR;
3822 TRACE_ERROR("too much payload, more than announced",
Christopher Faulet897d6122021-12-17 17:28:35 +01003823 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 +01003824 goto end;
3825 }
3826 h1m->curr_len -= ret;
3827 if (!h1m->curr_len) {
3828 h1m->state = H1_MSG_DONE;
Christopher Faulet897d6122021-12-17 17:28:35 +01003829 TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003830 }
3831 }
Christopher Faulet41951ab2021-11-26 18:12:51 +01003832 HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret);
3833 HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret);
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003834
3835 end:
Christopher Faulet897d6122021-12-17 17:28:35 +01003836 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003837 return ret;
3838}
3839#endif
3840
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003841static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3842{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003843 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003844 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003845
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003846 switch (mux_ctl) {
3847 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003848 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003849 ret |= MUX_STATUS_READY;
3850 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003851 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003852 if (output)
3853 *((int *)output) = h1c->errcode;
3854 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3855 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3856 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3857 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003858 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003859 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003860 default:
3861 return -1;
3862 }
3863}
3864
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003865/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003866static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003867{
3868 struct h1c *h1c = conn->ctx;
3869 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003870 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003871
Christopher Fauletf376a312019-01-04 15:16:06 +01003872 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3873 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003874 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3875 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3876 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3877 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3878
3879 if (h1s) {
3880 char *method;
3881
3882 if (h1s->meth < HTTP_METH_OTHER)
3883 method = http_known_methods[h1s->meth].ptr;
3884 else
3885 method = "UNKNOWN";
3886 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3887 " .meth=%s status=%d",
3888 h1s, h1s->flags,
3889 h1m_state_str(h1s->req.state),
3890 h1m_state_str(h1s->res.state), method, h1s->status);
3891 if (h1s->cs)
Christopher Fauletf835dea2021-12-21 14:35:17 +01003892 chunk_appendf(msg, " .cs.flg=0x%08x .cs.app=%p",
3893 h1s->cs->flags, h1s->cs->app);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003894
3895 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3896 if (h1s->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01003897 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3898 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3899 h1s->subs->tasklet->calls,
3900 h1s->subs->tasklet->context);
3901 if (h1s->subs->tasklet->calls >= 1000000)
3902 ret = 1;
3903 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3904 chunk_appendf(&trash, ")");
Willy Tarreau150c4f82021-01-20 17:05:58 +01003905 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003906 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003907 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003908}
3909
3910
3911/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3912static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3913{
3914 struct h1_hdr_entry *entry;
3915
3916 /* Be sure there is a non-empty <to> */
3917 if (!strlen(to)) {
3918 memprintf(err, "expect <to>");
3919 return -1;
3920 }
3921
3922 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003923 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003924 memprintf(err, "<from> and <to> must not differ execpt the case");
3925 return -1;
3926 }
3927
3928 /* Be sure <from> does not already existsin the tree */
3929 if (ebis_lookup(&hdrs_map.map, from)) {
3930 memprintf(err, "duplicate entry '%s'", from);
3931 return -1;
3932 }
3933
3934 /* Create the entry and insert it in the tree */
3935 entry = malloc(sizeof(*entry));
3936 if (!entry) {
3937 memprintf(err, "out of memory");
3938 return -1;
3939 }
3940
3941 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01003942 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003943 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003944 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003945 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003946 free(entry);
3947 memprintf(err, "out of memory");
3948 return -1;
3949 }
3950 ebis_insert(&hdrs_map.map, &entry->node);
3951 return 0;
3952}
3953
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003954/* Migrate the the connection to the current thread.
3955 * Return 0 if successful, non-zero otherwise.
3956 * Expected to be called with the old thread lock held.
3957 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003958static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003959{
3960 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003961 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003962
3963 if (fd_takeover(conn->handle.fd, conn) != 0)
3964 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003965
3966 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3967 /* We failed to takeover the xprt, even if the connection may
3968 * still be valid, flag it as error'd, as we have already
3969 * taken over the fd, and wake the tasklet, so that it will
3970 * destroy it.
3971 */
3972 conn->flags |= CO_FL_ERROR;
3973 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3974 return -1;
3975 }
3976
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003977 if (h1c->wait_event.events)
3978 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3979 h1c->wait_event.events, &h1c->wait_event);
3980 /* To let the tasklet know it should free itself, and do nothing else,
3981 * set its context to NULL.
3982 */
3983 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003984 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003985
3986 task = h1c->task;
3987 if (task) {
3988 task->context = NULL;
3989 h1c->task = NULL;
3990 __ha_barrier_store();
3991 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003992
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003993 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003994 if (!h1c->task) {
3995 h1_release(h1c);
3996 return -1;
3997 }
3998 h1c->task->process = h1_timeout_task;
3999 h1c->task->context = h1c;
4000 }
4001 h1c->wait_event.tasklet = tasklet_new();
4002 if (!h1c->wait_event.tasklet) {
4003 h1_release(h1c);
4004 return -1;
4005 }
4006 h1c->wait_event.tasklet->process = h1_io_cb;
4007 h1c->wait_event.tasklet->context = h1c;
4008 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
4009 SUB_RETRY_RECV, &h1c->wait_event);
4010
4011 return 0;
4012}
4013
4014
Christopher Faulet98fbe952019-07-22 16:18:24 +02004015static void h1_hdeaders_case_adjust_deinit()
4016{
4017 struct ebpt_node *node, *next;
4018 struct h1_hdr_entry *entry;
4019
4020 node = ebpt_first(&hdrs_map.map);
4021 while (node) {
4022 next = ebpt_next(node);
4023 ebpt_delete(node);
4024 entry = container_of(node, struct h1_hdr_entry, node);
4025 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01004026 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004027 free(entry);
4028 node = next;
4029 }
4030 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004031}
4032
Christopher Faulet98fbe952019-07-22 16:18:24 +02004033static int cfg_h1_headers_case_adjust_postparser()
4034{
4035 FILE *file = NULL;
4036 char *c, *key_beg, *key_end, *value_beg, *value_end;
4037 char *err;
4038 int rc, line = 0, err_code = 0;
4039
4040 if (!hdrs_map.name)
4041 goto end;
4042
4043 file = fopen(hdrs_map.name, "r");
4044 if (!file) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004045 ha_alert("h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004046 hdrs_map.name);
4047 err_code |= ERR_ALERT | ERR_FATAL;
4048 goto end;
4049 }
4050
4051 /* now parse all lines. The file may contain only two header name per
4052 * line, separated by spaces. All heading and trailing spaces will be
4053 * ignored. Lines starting with a # are ignored.
4054 */
4055 while (fgets(trash.area, trash.size, file) != NULL) {
4056 line++;
4057 c = trash.area;
4058
4059 /* strip leading spaces and tabs */
4060 while (*c == ' ' || *c == '\t')
4061 c++;
4062
4063 /* ignore emptu lines, or lines beginning with a dash */
4064 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
4065 continue;
4066
4067 /* look for the end of the key */
4068 key_beg = c;
4069 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
4070 c++;
4071 key_end = c;
4072
4073 /* strip middle spaces and tabs */
4074 while (*c == ' ' || *c == '\t')
4075 c++;
4076
4077 /* look for the end of the value, it is the end of the line */
4078 value_beg = c;
4079 while (*c && *c != '\n' && *c != '\r')
4080 c++;
4081 value_end = c;
4082
4083 /* trim possibly trailing spaces and tabs */
4084 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
4085 value_end--;
4086
4087 /* set final \0 and check entries */
4088 *key_end = '\0';
4089 *value_end = '\0';
4090
4091 err = NULL;
4092 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
4093 if (rc < 0) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004094 ha_alert("h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004095 hdrs_map.name, err, line);
4096 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004097 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004098 goto end;
4099 }
4100 if (rc > 0) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004101 ha_warning("h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004102 hdrs_map.name, err, line);
4103 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004104 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004105 }
4106 }
4107
4108 end:
4109 if (file)
4110 fclose(file);
4111 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
4112 return err_code;
4113}
4114
4115
4116/* config parser for global "h1-outgoing-header-case-adjust" */
4117static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004118 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004119 char **err)
4120{
4121 if (too_many_args(2, args, err, NULL))
4122 return -1;
4123 if (!*(args[1]) || !*(args[2])) {
4124 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
4125 return -1;
4126 }
4127 return add_hdr_case_adjust(args[1], args[2], err);
4128}
4129
4130/* config parser for global "h1-outgoing-headers-case-adjust-file" */
4131static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004132 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004133 char **err)
4134{
4135 if (too_many_args(1, args, err, NULL))
4136 return -1;
4137 if (!*(args[1])) {
4138 memprintf(err, "'%s' expects <file> as argument.", args[0]);
4139 return -1;
4140 }
4141 free(hdrs_map.name);
4142 hdrs_map.name = strdup(args[1]);
4143 return 0;
4144}
4145
4146
4147/* config keyword parsers */
4148static struct cfg_kw_list cfg_kws = {{ }, {
4149 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
4150 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
4151 { 0, NULL, NULL },
4152 }
4153};
4154
4155INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4156REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
4157
4158
Christopher Faulet51dbc942018-09-13 09:05:15 +02004159/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05004160/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02004161/****************************************/
4162
4163/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01004164static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02004165 .init = h1_init,
4166 .wake = h1_wake,
4167 .attach = h1_attach,
4168 .get_first_cs = h1_get_first_cs,
4169 .detach = h1_detach,
4170 .destroy = h1_destroy,
4171 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01004172 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004173 .rcv_buf = h1_rcv_buf,
4174 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02004175#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02004176 .rcv_pipe = h1_rcv_pipe,
4177 .snd_pipe = h1_snd_pipe,
4178#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02004179 .subscribe = h1_subscribe,
4180 .unsubscribe = h1_unsubscribe,
4181 .shutr = h1_shutr,
4182 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004183 .show_fd = h1_show_fd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004184 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004185 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02004186 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02004187 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02004188};
4189
Christopher Faulet3f612f72021-02-05 16:44:21 +01004190static const struct mux_ops mux_h1_ops = {
4191 .init = h1_init,
4192 .wake = h1_wake,
4193 .attach = h1_attach,
4194 .get_first_cs = h1_get_first_cs,
4195 .detach = h1_detach,
4196 .destroy = h1_destroy,
4197 .avail_streams = h1_avail_streams,
4198 .used_streams = h1_used_streams,
4199 .rcv_buf = h1_rcv_buf,
4200 .snd_buf = h1_snd_buf,
4201#if defined(USE_LINUX_SPLICE)
4202 .rcv_pipe = h1_rcv_pipe,
4203 .snd_pipe = h1_snd_pipe,
4204#endif
4205 .subscribe = h1_subscribe,
4206 .unsubscribe = h1_unsubscribe,
4207 .shutr = h1_shutr,
4208 .shutw = h1_shutw,
4209 .show_fd = h1_show_fd,
4210 .ctl = h1_ctl,
4211 .takeover = h1_takeover,
4212 .flags = MX_FL_HTX|MX_FL_NO_UPG,
4213 .name = "H1",
4214};
Christopher Faulet51dbc942018-09-13 09:05:15 +02004215
Christopher Faulet3f612f72021-02-05 16:44:21 +01004216/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
4217static struct mux_proto_list mux_proto_h1 =
4218 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
4219static struct mux_proto_list mux_proto_http =
4220 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02004221
Christopher Faulet3f612f72021-02-05 16:44:21 +01004222INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
4223INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01004224
Christopher Faulet51dbc942018-09-13 09:05:15 +02004225/*
4226 * Local variables:
4227 * c-indent-level: 8
4228 * c-basic-offset: 8
4229 * End:
4230 */