blob: 467d593c5ec66c2d3aa8305463160e9515cce4ad [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>
Willy Tarreau5413a872020-06-02 19:33:08 +020018#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020019#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020020#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020022#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/istbuf.h>
24#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020025#include <haproxy/pipe-t.h>
Willy Tarreauadc02402021-05-08 20:28:54 +020026#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020027#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020029#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020030#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020031
32/*
33 * H1 Connection flags (32 bits)
34 */
35#define H1C_F_NONE 0x00000000
36
37/* Flags indicating why writing output data are blocked */
38#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
39#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
40/* 0x00000004 - 0x00000008 unused */
41
42/* Flags indicating why reading input data are blocked. */
43#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
44#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletd17ad822020-09-24 15:14:29 +020045#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010048#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 +010049#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 +010050#define H1C_F_ST_IDLE 0x00000400 /* connection is idle and may be reused
51 * (exclusive to all H1C_F_ST flags and never set when an h1s is attached) */
52#define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
53#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 +010054#define H1C_F_ST_READY 0x00002000 /* Set in ATTACHED state with a READY conn-stream. A conn-stream is not ready when
55 * 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 +010056#define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED)
Christopher Fauleta85c5222021-10-27 15:36:38 +020057#define H1C_F_ST_SILENT_SHUT 0x00004000 /* silent (or dirty) shutdown must be performed */
58/* 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020059
Christopher Fauletb385b502021-01-13 18:47:57 +010060#define H1C_F_WANT_SPLICE 0x00010000 /* Don't read into a buffer because we want to use or we are using splicing */
61#define H1C_F_ERR_PENDING 0x00020000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
62#define H1C_F_WAIT_NEXT_REQ 0x00040000 /* waiting for the next request to start, use keep-alive timeout */
63#define H1C_F_UPG_H2C 0x00080000 /* set if an upgrade to h2 should be done */
64#define H1C_F_CO_MSG_MORE 0x00100000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
65#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 +020066
Christopher Faulet10a86702021-04-07 14:18:11 +020067/* 0x00400000 - 0x40000000 unusued*/
Christopher Faulet3ced1d12020-11-27 16:44:01 +010068#define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */
Christopher Faulet46230362019-10-17 16:04:20 +020069
Christopher Faulet51dbc942018-09-13 09:05:15 +020070/*
71 * H1 Stream flags (32 bits)
72 */
Christopher Faulet129817b2018-09-20 16:14:40 +020073#define H1S_F_NONE 0x00000000
Christopher Faulet10a86702021-04-07 14:18:11 +020074
75#define H1S_F_RX_BLK 0x00100000 /* Don't process more input data, waiting sync with output side */
76#define H1S_F_TX_BLK 0x00200000 /* Don't process more output data, waiting sync with input side */
Christopher Faulet46e058d2021-09-20 07:47:27 +020077#define H1S_F_RX_CONGESTED 0x00000004 /* Cannot process input data RX path is congested (waiting for more space in channel's buffer) */
78
Willy Tarreauc493c9c2019-06-03 14:18:22 +020079#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020080#define H1S_F_WANT_KAL 0x00000010
81#define H1S_F_WANT_TUN 0x00000020
82#define H1S_F_WANT_CLO 0x00000040
83#define H1S_F_WANT_MSK 0x00000070
84#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet5696f542020-12-02 16:08:38 +010085#define H1S_F_BODYLESS_RESP 0x00000100 /* Bodyless response message */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020086
Ilya Shipitsinacf84592021-02-06 22:29:08 +050087/* 0x00000200 unused */
Christopher Faulet2eed8002020-12-07 11:26:13 +010088#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 +020089#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
90#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
91#define H1S_F_ERROR 0x00001800 /* stream error mask */
92
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020093#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 +020094#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020095
96/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020097struct h1c {
98 struct connection *conn;
99 struct proxy *px;
100 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +0200101 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200102 struct buffer ibuf; /* Input buffer to store data before parsing */
103 struct buffer obuf; /* Output buffer to store data after reformatting */
104
105 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
106 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
107
108 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100109 struct task *task; /* timeout management task */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200110 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
111 int timeout; /* client/server timeout duration */
112 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200113};
114
115/* H1 stream descriptor */
116struct h1s {
117 struct h1c *h1c;
118 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100119 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200120
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100121 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200122
Olivier Houchardf502aca2018-12-14 19:42:40 +0100123 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200124 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200125 struct h1m req;
126 struct h1m res;
127
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500128 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200129 uint16_t status; /* HTTP response status */
Amaury Denoyellec1938232020-12-11 17:53:03 +0100130
131 char ws_key[25]; /* websocket handshake key */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200132};
133
Christopher Faulet98fbe952019-07-22 16:18:24 +0200134/* Map of headers used to convert outgoing headers */
135struct h1_hdrs_map {
136 char *name;
137 struct eb_root map;
138};
139
140/* An entry in a headers map */
141struct h1_hdr_entry {
142 struct ist name;
143 struct ebpt_node node;
144};
145
146/* Declare the headers map */
147static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
148
149
Christopher Faulet6b81df72019-10-01 22:08:43 +0200150/* trace source and events */
151static void h1_trace(enum trace_level level, uint64_t mask,
152 const struct trace_source *src,
153 const struct ist where, const struct ist func,
154 const void *a1, const void *a2, const void *a3, const void *a4);
155
156/* The event representation is split like this :
157 * h1c - internal H1 connection
158 * h1s - internal H1 stream
159 * strm - application layer
160 * rx - data receipt
161 * tx - data transmission
162 *
163 */
164static const struct trace_event h1_trace_events[] = {
165#define H1_EV_H1C_NEW (1ULL << 0)
166 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
167#define H1_EV_H1C_RECV (1ULL << 1)
168 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
169#define H1_EV_H1C_SEND (1ULL << 2)
170 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
171#define H1_EV_H1C_BLK (1ULL << 3)
172 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
173#define H1_EV_H1C_WAKE (1ULL << 4)
174 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
175#define H1_EV_H1C_END (1ULL << 5)
176 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
177#define H1_EV_H1C_ERR (1ULL << 6)
178 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
179
180#define H1_EV_RX_DATA (1ULL << 7)
181 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
182#define H1_EV_RX_EOI (1ULL << 8)
183 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
184#define H1_EV_RX_HDRS (1ULL << 9)
185 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
186#define H1_EV_RX_BODY (1ULL << 10)
187 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
188#define H1_EV_RX_TLRS (1ULL << 11)
189 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
190
191#define H1_EV_TX_DATA (1ULL << 12)
192 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
193#define H1_EV_TX_EOI (1ULL << 13)
194 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
195#define H1_EV_TX_HDRS (1ULL << 14)
196 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
197#define H1_EV_TX_BODY (1ULL << 15)
198 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
199#define H1_EV_TX_TLRS (1ULL << 16)
200 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
201
202#define H1_EV_H1S_NEW (1ULL << 17)
203 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
204#define H1_EV_H1S_BLK (1ULL << 18)
205 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
206#define H1_EV_H1S_END (1ULL << 19)
207 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
208#define H1_EV_H1S_ERR (1ULL << 20)
209 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
210
211#define H1_EV_STRM_NEW (1ULL << 21)
212 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
213#define H1_EV_STRM_RECV (1ULL << 22)
214 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
215#define H1_EV_STRM_SEND (1ULL << 23)
216 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
217#define H1_EV_STRM_WAKE (1ULL << 24)
218 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
219#define H1_EV_STRM_SHUT (1ULL << 25)
220 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
221#define H1_EV_STRM_END (1ULL << 26)
222 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
223#define H1_EV_STRM_ERR (1ULL << 27)
224 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
225
226 { }
227};
228
229static const struct name_desc h1_trace_lockon_args[4] = {
230 /* arg1 */ { /* already used by the connection */ },
231 /* arg2 */ { .name="h1s", .desc="H1 stream" },
232 /* arg3 */ { },
233 /* arg4 */ { }
234};
235
236static const struct name_desc h1_trace_decoding[] = {
237#define H1_VERB_CLEAN 1
238 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
239#define H1_VERB_MINIMAL 2
240 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
241#define H1_VERB_SIMPLE 3
242 { .name="simple", .desc="add request/response status line or htx info when available" },
243#define H1_VERB_ADVANCED 4
244 { .name="advanced", .desc="add header fields or frame decoding when available" },
245#define H1_VERB_COMPLETE 5
246 { .name="complete", .desc="add full data dump when available" },
247 { /* end */ }
248};
249
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200250static struct trace_source trace_h1 __read_mostly = {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200251 .name = IST("h1"),
252 .desc = "HTTP/1 multiplexer",
253 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
254 .default_cb = h1_trace,
255 .known_events = h1_trace_events,
256 .lockon_args = h1_trace_lockon_args,
257 .decoding = h1_trace_decoding,
258 .report_events = ~0, // report everything by default
259};
260
261#define TRACE_SOURCE &trace_h1
262INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
263
Christopher Faulet51dbc942018-09-13 09:05:15 +0200264/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100265DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
266DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200267
Christopher Faulet51dbc942018-09-13 09:05:15 +0200268static int h1_recv(struct h1c *h1c);
269static int h1_send(struct h1c *h1c);
270static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100271/* h1_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100272struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state);
273struct task *h1_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Fauleta85c5222021-10-27 15:36:38 +0200274static void h1_shutw_conn(struct connection *conn);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200275static void h1_wake_stream_for_recv(struct h1s *h1s);
276static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200277
Christopher Faulet6b81df72019-10-01 22:08:43 +0200278/* the H1 traces always expect that arg1, if non-null, is of type connection
279 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
280 * that arg3, if non-null, is a htx for rx/tx headers.
281 */
282static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
283 const struct ist where, const struct ist func,
284 const void *a1, const void *a2, const void *a3, const void *a4)
285{
286 const struct connection *conn = a1;
287 const struct h1c *h1c = conn ? conn->ctx : NULL;
288 const struct h1s *h1s = a2;
289 const struct htx *htx = a3;
290 const size_t *val = a4;
291
292 if (!h1c)
293 h1c = (h1s ? h1s->h1c : NULL);
294
295 if (!h1c || src->verbosity < H1_VERB_CLEAN)
296 return;
297
298 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200299 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200300
301 /* Display request and response states if h1s is defined */
302 if (h1s)
303 chunk_appendf(&trace_buf, " [%s, %s]",
304 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
305
306 if (src->verbosity == H1_VERB_CLEAN)
307 return;
308
309 /* Display the value to the 4th argument (level > STATE) */
310 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100311 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200312
313 /* Display status-line if possible (verbosity > MINIMAL) */
314 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
315 const struct htx_blk *blk = htx_get_head_blk(htx);
316 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
317 enum htx_blk_type type = htx_get_blk_type(blk);
318
319 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
320 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
321 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
322 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
323 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
324 }
325
326 /* Display h1c info and, if defined, h1s info (pointer + flags) */
327 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
328 if (h1s)
329 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
330
331 if (src->verbosity == H1_VERB_MINIMAL)
332 return;
333
334 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
335 if (src->level > TRACE_LEVEL_USER) {
336 if (src->verbosity == H1_VERB_COMPLETE ||
337 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
338 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
339 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
340 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
341 if (src->verbosity == H1_VERB_COMPLETE ||
342 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
343 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
344 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
345 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
346 }
347
348 /* Display htx info if defined (level > USER) */
349 if (src->level > TRACE_LEVEL_USER && htx) {
350 int full = 0;
351
352 /* Full htx info (level > STATE && verbosity > SIMPLE) */
353 if (src->level > TRACE_LEVEL_STATE) {
354 if (src->verbosity == H1_VERB_COMPLETE)
355 full = 1;
356 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
357 full = 1;
358 }
359
360 chunk_memcat(&trace_buf, "\n\t", 2);
361 htx_dump(&trace_buf, htx, full);
362 }
363}
364
365
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366/*****************************************************/
367/* functions below are for dynamic buffer management */
368/*****************************************************/
369/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100370 * Indicates whether or not we may receive data. The rules are the following :
371 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200372 * must not attempt to receive
373 * - if we are waiting for the connection establishment, we must not attempt
374 * to receive
375 * - if an error was detected on the stream we must not attempt to receive
376 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100377 * - if the input buffer failed to be allocated or is full , we must not try
378 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200379 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380 * - otherwise must may not attempt to receive
381 */
382static inline int h1_recv_allowed(const struct h1c *h1c)
383{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100384 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100385 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 +0200386 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200387 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200388
Willy Tarreau2febb842020-07-31 09:15:43 +0200389 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
390 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 +0100391 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200392 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100393
Christopher Faulet119ac872020-09-30 17:33:22 +0200394 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
395 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
396 return 0;
397 }
398
Christopher Fauletd17ad822020-09-24 15:14:29 +0200399 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200400 return 1;
401
Christopher Faulet6b81df72019-10-01 22:08:43 +0200402 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 +0200403 return 0;
404}
405
406/*
407 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
408 * flags are used to figure what buffer was requested. It returns 1 if the
409 * allocation succeeds, in which case the connection is woken up, or 0 if it's
410 * impossible to wake up and we prefer to be woken up later.
411 */
412static int h1_buf_available(void *target)
413{
414 struct h1c *h1c = target;
415
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100416 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200417 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 +0200418 h1c->flags &= ~H1C_F_IN_ALLOC;
419 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200420 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200421 return 1;
422 }
423
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100424 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200425 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 +0200426 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200427 if (h1c->h1s)
428 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200429 return 1;
430 }
431
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100432 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200433 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
434 h1c->flags &= ~H1C_F_IN_SALLOC;
435 tasklet_wakeup(h1c->wait_event.tasklet);
436 return 1;
437 }
438
Christopher Faulet51dbc942018-09-13 09:05:15 +0200439 return 0;
440}
441
442/*
443 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
444 */
445static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
446{
447 struct buffer *buf = NULL;
448
Willy Tarreau2b718102021-04-21 07:32:39 +0200449 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100450 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200451 h1c->buf_wait.target = h1c;
452 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200453 LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200454 }
455 return buf;
456}
457
458/*
459 * Release a buffer, if any, and try to wake up entities waiting in the buffer
460 * wait queue.
461 */
462static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
463{
464 if (bptr->size) {
465 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100466 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200467 }
468}
469
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100470/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100471 * 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 +0100472 * not. This flag is only set when no H1S is attached and when the previous
473 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100474 */
475static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200476{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100477 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200478
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100479 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200480}
481
Willy Tarreau00f18a32019-01-26 12:19:01 +0100482/* returns the number of streams still available on a connection */
483static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100484{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100485 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100486}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200487
Christopher Faulet7a145d62020-08-05 11:31:16 +0200488/* Refresh the h1c task timeout if necessary */
489static void h1_refresh_timeout(struct h1c *h1c)
490{
491 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100492 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200493 /* half-closed or dead connections : switch to clientfin/serverfin
494 * timeouts so that we don't hang too long on clients that have
495 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200496 */
497 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200498 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
499 }
500 else if (b_data(&h1c->obuf)) {
501 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200502 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200503 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
504 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100505 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
506 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200507 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100508 TRACE_DEVEL("refreshing connection's timeout (alive front h1c but not ready)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200509 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200510 else {
511 /* alive back connections of front connections with a conn-stream attached */
512 h1c->task->expire = TICK_ETERNITY;
513 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
514 }
515
Christopher Fauletdbe57792020-10-05 17:50:58 +0200516 /* Finally set the idle expiration date if shorter */
517 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200518 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
519 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200520 }
521}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200522
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200523static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200524{
525 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
526 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
527 h1c->idle_exp = TICK_ETERNITY;
528 return;
529 }
530
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100531 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200532 if (!tick_isset(h1c->idle_exp)) {
533 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
534 !b_data(&h1c->ibuf) && /* No input data */
535 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
536 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
537 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
538 }
539 else {
540 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
541 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
542 }
543 }
544 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100545 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200546 if (!tick_isset(h1c->idle_exp)) {
547 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
548 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
549 }
550 }
551 else { // CS_ATTACHED or SHUTDOWN
552 h1c->idle_exp = TICK_ETERNITY;
553 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
554 }
555}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200556/*****************************************************************/
557/* functions below are dedicated to the mux setup and management */
558/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200559
560/* returns non-zero if there are input data pending for stream h1s. */
561static inline size_t h1s_data_pending(const struct h1s *h1s)
562{
563 const struct h1m *h1m;
564
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200565 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100566 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200567}
568
Christopher Faulet16df1782020-12-04 16:47:41 +0100569/* Creates a new conn-stream and the associate stream. <input> is used as input
570 * buffer for the stream. On success, it is transferred to the stream and the
571 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
572 * mux must still take care of it. However, there is nothing special to do
573 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
574 * b_free() on it is always safe. This function returns the conn-stream on
575 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200576static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100577{
578 struct conn_stream *cs;
579
Christopher Faulet6b81df72019-10-01 22:08:43 +0200580 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200581 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200582 if (!cs) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100583 TRACE_ERROR("CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100584 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200585 }
Christopher Faulet47365272018-10-31 17:40:50 +0100586 h1s->cs = cs;
587 cs->ctx = h1s;
588
589 if (h1s->flags & H1S_F_NOT_FIRST)
590 cs->flags |= CS_FL_NOT_FIRST;
591
Christopher Faulet26256f82020-09-14 11:40:13 +0200592 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200593 TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100594 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200595 }
596
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100597 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200598 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100599 return cs;
600
601 err:
602 cs_free(cs);
603 h1s->cs = NULL;
Christopher Faulet26a26432021-01-27 11:27:50 +0100604 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100605 return NULL;
606}
607
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100608static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
609{
610 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
611
612 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100613 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 +0100614 goto err;
615 }
616
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100617 h1s->h1c->flags |= H1C_F_ST_READY;
618 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
619 return h1s->cs;
620
621 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100622 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100623 return NULL;
624}
625
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200626static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200627{
628 struct h1s *h1s;
629
Christopher Faulet6b81df72019-10-01 22:08:43 +0200630 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
631
Christopher Faulet51dbc942018-09-13 09:05:15 +0200632 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100633 if (!h1s) {
634 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 +0100635 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100636 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200637 h1s->h1c = h1c;
638 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200639 h1s->sess = NULL;
640 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100641 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100642 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200643 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100644 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200645
646 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100647 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200648
Christopher Faulet129817b2018-09-20 16:14:40 +0200649 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100650 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200651
652 h1s->status = 0;
653 h1s->meth = HTTP_METH_OTHER;
654
Christopher Faulet47365272018-10-31 17:40:50 +0100655 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
656 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100657 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100658
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200659 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
660 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200661
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200662 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100663 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200664 return NULL;
665}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100666
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200667static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
668{
669 struct session *sess = h1c->conn->owner;
670 struct h1s *h1s;
671
672 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
673
674 h1s = h1s_new(h1c);
675 if (!h1s)
676 goto fail;
677
678 h1s->sess = sess;
679
680 if (h1c->px->options2 & PR_O2_REQBUG_OK)
681 h1s->req.err_pos = -1;
682
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200683 h1c->idle_exp = TICK_ETERNITY;
684 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200685 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200686 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100687
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200688 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100689 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200690 return NULL;
691}
692
693static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
694{
695 struct h1s *h1s;
696
697 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
698
699 h1s = h1s_new(h1c);
700 if (!h1s)
701 goto fail;
702
Christopher Faulet10a86702021-04-07 14:18:11 +0200703 h1s->flags |= H1S_F_RX_BLK;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200704 h1s->cs = cs;
705 h1s->sess = sess;
706 cs->ctx = h1s;
707
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100708 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200709
710 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
711 h1s->res.err_pos = -1;
712
713 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
714 return h1s;
715
716 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100717 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100718 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200719}
720
721static void h1s_destroy(struct h1s *h1s)
722{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200723 if (h1s) {
724 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200725
Christopher Faulet6b81df72019-10-01 22:08:43 +0200726 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200727 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200728
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100729 if (h1s->subs)
730 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200731
Christopher Fauletd17ad822020-09-24 15:14:29 +0200732 h1_release_buf(h1c, &h1s->rxbuf);
733
Christopher Faulet10a86702021-04-07 14:18:11 +0200734 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Fauletb385b502021-01-13 18:47:57 +0100735 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200736 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
737 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200738 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100739 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +0100740 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 +0200741 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100742
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100743 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100744 !(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 +0100745 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100746 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100747 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100748 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
749 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200750 else {
Christopher Faulet26a26432021-01-27 11:27:50 +0100751 TRACE_STATE("set shudown on h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100752 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200753 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200754 pool_free(pool_head_h1s, h1s);
755 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200756}
757
758/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200759 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500760 * to the existing conn_stream (for outgoing connections or for incoming ones
761 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200762 * establishment). <input> is always used as Input buffer and may contain
763 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
764 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200765 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200766static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
767 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200768{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200769 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100770 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200771 void *conn_ctx = conn->ctx;
772
773 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200774
775 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100776 if (!h1c) {
777 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200778 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100779 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200780 h1c->conn = conn;
781 h1c->px = proxy;
782
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100783 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200784 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200785 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200786 h1c->obuf = BUF_NULL;
787 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200788 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200789
Willy Tarreau90f366b2021-02-20 11:49:49 +0100790 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200791 h1c->wait_event.tasklet = tasklet_new();
792 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200793 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200794 h1c->wait_event.tasklet->process = h1_io_cb;
795 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100796 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200797 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200798
Christopher Faulete9b70722019-04-08 10:46:02 +0200799 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200800 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100801 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
802 if (tick_isset(proxy->timeout.serverfin))
803 h1c->shut_timeout = proxy->timeout.serverfin;
804 } else {
805 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
806 if (tick_isset(proxy->timeout.clientfin))
807 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200808
809 LIST_APPEND(&mux_stopping_data[tid].list,
810 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100811 }
812 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200813 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100814 if (!t) {
815 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 +0100816 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100817 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100818
819 h1c->task = t;
820 t->process = h1_timeout_task;
821 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200822
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100823 t->expire = tick_add(now_ms, h1c->timeout);
824 }
825
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100826 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200827
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200828 if (h1c->flags & H1C_F_IS_BACK) {
829 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200830 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
831 goto fail;
832 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100833 else if (conn_ctx) {
834 /* Upgraded frontend connection (from TCP) */
835 struct conn_stream *cs = conn_ctx;
836
837 if (!h1c_frt_stream_new(h1c))
838 goto fail;
839
840 h1c->h1s->cs = cs;
841 cs->ctx = h1c->h1s;
842
843 /* Attach the CS but Not ready yet */
844 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
845 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
846 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
847 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100848
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200849 if (t) {
850 h1_set_idle_expiration(h1c);
851 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100852 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200853 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100854
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200855 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100856 if (b_data(&h1c->ibuf))
857 tasklet_wakeup(h1c->wait_event.tasklet);
858 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200859 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200860
861 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200862 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200863 return 0;
864
865 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200866 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200867 if (h1c->wait_event.tasklet)
868 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200869 pool_free(pool_head_h1c, h1c);
870 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200871 conn->ctx = conn_ctx; // restore saved context
872 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200873 return -1;
874}
875
Christopher Faulet73c12072019-04-08 11:23:22 +0200876/* release function. This one should be called to free all resources allocated
877 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200878 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200879static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200880{
Christopher Faulet61840e72019-04-15 09:33:32 +0200881 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200882
Christopher Faulet6b81df72019-10-01 22:08:43 +0200883 TRACE_POINT(H1_EV_H1C_END);
884
Christopher Faulet51dbc942018-09-13 09:05:15 +0200885 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200886 /* The connection must be aattached to this mux to be released */
887 if (h1c->conn && h1c->conn->ctx == h1c)
888 conn = h1c->conn;
889
Christopher Faulet6b81df72019-10-01 22:08:43 +0200890 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
891
Christopher Faulet61840e72019-04-15 09:33:32 +0200892 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200893 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200894 /* Make sure we're no longer subscribed to anything */
895 if (h1c->wait_event.events)
896 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
897 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200898 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200899 /* connection successfully upgraded to H2, this
900 * mux was already released */
901 return;
902 }
Christopher Faulet26a26432021-01-27 11:27:50 +0100903 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200904 sess_log(conn->owner); /* Log if the upgrade failed */
905 }
906
Olivier Houchard45c44372019-06-11 14:06:23 +0200907
Willy Tarreau2b718102021-04-21 07:32:39 +0200908 if (LIST_INLIST(&h1c->buf_wait.list))
Willy Tarreau90f366b2021-02-20 11:49:49 +0100909 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200910
911 h1_release_buf(h1c, &h1c->ibuf);
912 h1_release_buf(h1c, &h1c->obuf);
913
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100914 if (h1c->task) {
915 h1c->task->context = NULL;
916 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
917 h1c->task = NULL;
918 }
919
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200920 if (h1c->wait_event.tasklet)
921 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200922
Christopher Fauletf2824e62018-10-01 12:12:37 +0200923 h1s_destroy(h1c->h1s);
Christopher Faulete76b4f02021-10-27 15:42:13 +0200924 if (conn) {
925 if (h1c->wait_event.events != 0)
926 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
927 &h1c->wait_event);
928 h1_shutw_conn(conn);
929 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200930 pool_free(pool_head_h1c, h1c);
931 }
932
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200933 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200934 if (!conn_is_back(conn))
935 LIST_DEL_INIT(&conn->stopping_list);
936
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200937 conn->mux = NULL;
938 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200939 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200940
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200941 conn_stop_tracking(conn);
942 conn_full_close(conn);
943 if (conn->destroy_cb)
944 conn->destroy_cb(conn);
945 conn_free(conn);
946 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200947}
948
949/******************************************************/
950/* functions below are for the H1 protocol processing */
951/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200952/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
953 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200954 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100955static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200956{
Christopher Faulet570d1612018-11-26 11:13:57 +0100957 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200958
Christopher Faulet570d1612018-11-26 11:13:57 +0100959 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200960 (*(p + 5) > '1' ||
961 (*(p + 5) == '1' && *(p + 7) >= '1')))
962 h1m->flags |= H1_MF_VER_11;
963}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200964
Christopher Faulet9768c262018-10-22 09:34:31 +0200965/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
966 * greater or equal to 1.1
967 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100968static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200969{
Christopher Faulet570d1612018-11-26 11:13:57 +0100970 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200971
Christopher Faulet570d1612018-11-26 11:13:57 +0100972 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200973 (*(p + 5) > '1' ||
974 (*(p + 5) == '1' && *(p + 7) >= '1')))
975 h1m->flags |= H1_MF_VER_11;
976}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200977
Christopher Fauletf2824e62018-10-01 12:12:37 +0200978/* Deduce the connection mode of the client connection, depending on the
979 * configuration and the H1 message flags. This function is called twice, the
980 * first time when the request is parsed and the second time when the response
981 * is parsed.
982 */
983static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
984{
985 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200986
987 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100988 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100989 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100990 h1s->status == 101) {
991 /* Either we've established an explicit tunnel, or we're
992 * switching the protocol. In both cases, we're very unlikely to
993 * understand the next protocols. We have to switch to tunnel
994 * mode, so that we transfer the request and responses then let
995 * this protocol pass unmodified. When we later implement
996 * specific parsers for such protocols, we'll want to check the
997 * Upgrade header which contains information about that protocol
998 * for responses with status 101 (eg: see RFC2817 about TLS).
999 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001000 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001001 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 +01001002 }
1003 else if (h1s->flags & H1S_F_WANT_KAL) {
1004 /* By default the client is in KAL mode. CLOSE mode mean
1005 * it is imposed by the client itself. So only change
1006 * KAL mode here. */
1007 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1008 /* no length known or explicit close => close */
1009 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001010 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 +01001011 }
1012 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1013 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001014 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001015 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001016 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 +01001017 }
1018 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001019 }
1020 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001021 /* Input direction: first pass */
1022 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1023 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001024 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001025 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 +01001026 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001027 }
1028
1029 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001030 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001031 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001032 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);
1033 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001034}
1035
1036/* Deduce the connection mode of the client connection, depending on the
1037 * configuration and the H1 message flags. This function is called twice, the
1038 * first time when the request is parsed and the second time when the response
1039 * is parsed.
1040 */
1041static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1042{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001043 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001044 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001045 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001046
Christopher Fauletf2824e62018-10-01 12:12:37 +02001047 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001048 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001049 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001050 h1s->status == 101) {
1051 /* Either we've established an explicit tunnel, or we're
1052 * switching the protocol. In both cases, we're very unlikely to
1053 * understand the next protocols. We have to switch to tunnel
1054 * mode, so that we transfer the request and responses then let
1055 * this protocol pass unmodified. When we later implement
1056 * specific parsers for such protocols, we'll want to check the
1057 * Upgrade header which contains information about that protocol
1058 * for responses with status 101 (eg: see RFC2817 about TLS).
1059 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001060 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001061 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 +01001062 }
1063 else if (h1s->flags & H1S_F_WANT_KAL) {
1064 /* By default the server is in KAL mode. CLOSE mode mean
1065 * it is imposed by haproxy itself. So only change KAL
1066 * mode here. */
1067 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1068 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1069 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1070 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001071 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 +01001072 }
1073 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001074 }
Christopher Faulet70033782018-12-05 13:50:11 +01001075 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001076 /* Output direction: first pass */
1077 if (h1m->flags & H1_MF_CONN_CLO) {
1078 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001079 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001080 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 +01001081 }
1082 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1083 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1084 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1085 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1086 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1087 /* no explicit keep-alive option httpclose/server-close => close */
1088 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001089 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 +01001090 }
Christopher Faulet70033782018-12-05 13:50:11 +01001091 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001092
1093 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001094 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001095 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001096 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);
1097 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001098}
1099
Christopher Fauletb992af02019-03-28 15:42:24 +01001100static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001101{
1102 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001103
1104 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1105 * token is found
1106 */
1107 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001108 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001109
1110 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001111 if (!(h1m->flags & H1_MF_VER_11)) {
1112 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 +01001113 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001114 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001115 }
1116 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001117 if (h1m->flags & H1_MF_VER_11) {
1118 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001119 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001120 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001121 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001122}
1123
Christopher Fauletb992af02019-03-28 15:42:24 +01001124static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001125{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001126 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1127 * token is found
1128 */
1129 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001130 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001131
1132 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001133 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001134 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1135 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 +01001136 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001137 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001138 }
1139 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001140 if (h1m->flags & H1_MF_VER_11) {
1141 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001142 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001143 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001144 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001145}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146
Christopher Fauletb992af02019-03-28 15:42:24 +01001147static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001148{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001149 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001151 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001152 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001153}
1154
Christopher Fauletb992af02019-03-28 15:42:24 +01001155static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1156{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001157 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001158 h1_set_cli_conn_mode(h1s, h1m);
1159 else
1160 h1_set_srv_conn_mode(h1s, h1m);
1161
1162 if (!(h1m->flags & H1_MF_RESP))
1163 h1_update_req_conn_value(h1s, h1m, conn_val);
1164 else
1165 h1_update_res_conn_value(h1s, h1m, conn_val);
1166}
Christopher Faulete44769b2018-11-29 23:01:45 +01001167
Christopher Faulet98fbe952019-07-22 16:18:24 +02001168/* Try to adjust the case of the message header name using the global map
1169 * <hdrs_map>.
1170 */
1171static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1172{
1173 struct ebpt_node *node;
1174 struct h1_hdr_entry *entry;
1175
1176 /* No entry in the map, do nothing */
1177 if (eb_is_empty(&hdrs_map.map))
1178 return;
1179
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001180 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001181 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1182 return;
1183
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001184 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001185 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1186 return;
1187
1188 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1189 if (!node)
1190 return;
1191 entry = container_of(node, struct h1_hdr_entry, node);
1192 name->ptr = entry->name.ptr;
1193 name->len = entry->name.len;
1194}
1195
Christopher Faulete44769b2018-11-29 23:01:45 +01001196/* Append the description of what is present in error snapshot <es> into <out>.
1197 * The description must be small enough to always fit in a buffer. The output
1198 * buffer may be the trash so the trash must not be used inside this function.
1199 */
1200static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1201{
1202 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001203 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1204 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1205 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1206 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1207 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1208 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001209}
1210/*
1211 * Capture a bad request or response and archive it in the proxy's structure.
1212 * By default it tries to report the error position as h1m->err_pos. However if
1213 * this one is not set, it will then report h1m->next, which is the last known
1214 * parsing point. The function is able to deal with wrapping buffers. It always
1215 * displays buffers as a contiguous area starting at buf->p. The direction is
1216 * determined thanks to the h1m's flags.
1217 */
1218static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1219 struct h1m *h1m, struct buffer *buf)
1220{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001221 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001222 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001223 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001224 union error_snapshot_ctx ctx;
1225
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001226 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001227 if (sess == NULL)
1228 sess = si_strm(h1s->cs->data)->sess;
1229 if (!(h1m->flags & H1_MF_RESP))
1230 other_end = si_strm(h1s->cs->data)->be;
1231 else
1232 other_end = sess->fe;
1233 } else
1234 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001235
1236 /* http-specific part now */
1237 ctx.h1.state = h1m->state;
1238 ctx.h1.c_flags = h1c->flags;
1239 ctx.h1.s_flags = h1s->flags;
1240 ctx.h1.m_flags = h1m->flags;
1241 ctx.h1.m_clen = h1m->curr_len;
1242 ctx.h1.m_blen = h1m->body_len;
1243
1244 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1245 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001246 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1247 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001248}
1249
Christopher Fauletadb22202018-12-12 10:32:09 +01001250/* Emit the chunksize followed by a CRLF in front of data of the buffer
1251 * <buf>. It goes backwards and starts with the byte before the buffer's
1252 * head. The caller is responsible for ensuring there is enough room left before
1253 * the buffer's head for the string.
1254 */
1255static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1256{
1257 char *beg, *end;
1258
1259 beg = end = b_head(buf);
1260 *--beg = '\n';
1261 *--beg = '\r';
1262 do {
1263 *--beg = hextab[chksz & 0xF];
1264 } while (chksz >>= 4);
1265 buf->head -= (end - beg);
1266 b_add(buf, end - beg);
1267}
1268
1269/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1270 * ensuring there is enough room left in the buffer for the string. */
1271static void h1_emit_chunk_crlf(struct buffer *buf)
1272{
1273 *(b_peek(buf, b_data(buf))) = '\r';
1274 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1275 b_add(buf, 2);
1276}
1277
Christopher Faulet129817b2018-09-20 16:14:40 +02001278/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001279 * Switch the stream to tunnel mode. This function must only be called on 2xx
1280 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001281 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001282static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001283{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001284 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001285
Christopher Faulet5be651d2021-01-22 15:28:03 +01001286 h1s->req.state = H1_MSG_TUNNEL;
1287 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001288
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001289 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001290 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001291
Christopher Faulet5be651d2021-01-22 15:28:03 +01001292 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 +01001293
Christopher Faulet10a86702021-04-07 14:18:11 +02001294 if (h1s->flags & H1S_F_RX_BLK) {
1295 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001296 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001297 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 +02001298 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001299 if (h1s->flags & H1S_F_TX_BLK) {
1300 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001301 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001302 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 +01001303 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001304}
1305
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001306/* Search for a websocket key header. The message should have been identified
1307 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001308 *
1309 * On the request side, if found the key is stored in the session. It might be
1310 * needed to calculate response key if the server side is using http/2.
1311 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001312 * On the response side, the key might be verified if haproxy has been
1313 * responsible for the generation of a key. This happens when a h2 client is
1314 * interfaced with a h1 server.
1315 *
1316 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001317 */
1318static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1319{
1320 struct htx_blk *blk;
1321 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001322 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001323 int ws_key_found = 0, idx;
1324
1325 idx = htx_get_head(htx); // returns the SL that we skip
1326 while ((idx = htx_get_next(htx, idx)) != -1) {
1327 blk = htx_get_blk(htx, idx);
1328 type = htx_get_blk_type(blk);
1329
1330 if (type == HTX_BLK_UNUSED)
1331 continue;
1332
1333 if (type != HTX_BLK_HDR)
1334 break;
1335
1336 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001337 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001338
Amaury Denoyellec1938232020-12-11 17:53:03 +01001339 /* Websocket key is base64 encoded of 16 bytes */
1340 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001341 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001342 /* Copy the key on request side
1343 * we might need it if the server is using h2 and does
1344 * not provide the response
1345 */
1346 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001347 ws_key_found = 1;
1348 break;
1349 }
1350 else if (isteqi(n, ist("sec-websocket-accept")) &&
1351 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001352 /* Need to verify the response key if the input was
1353 * generated by haproxy
1354 */
1355 if (h1s->ws_key[0]) {
1356 char key[29];
1357 h1_calculate_ws_output_key(h1s->ws_key, key);
1358 if (!isteqi(ist(key), v))
1359 break;
1360 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001361 ws_key_found = 1;
1362 break;
1363 }
1364 }
1365
1366 /* missing websocket key, reject the message */
1367 if (!ws_key_found) {
1368 htx->flags |= HTX_FL_PARSING_ERROR;
1369 return 0;
1370 }
1371
1372 return 1;
1373}
1374
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001375/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001376 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001377 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001378 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1379 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001380 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001381static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1382 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001383{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001384 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001385 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001386
Willy Tarreau022e5e52020-09-10 09:33:15 +02001387 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 +02001388
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001389 if (h1s->meth == HTTP_METH_CONNECT)
1390 h1m->flags |= H1_MF_METH_CONNECT;
1391 if (h1s->meth == HTTP_METH_HEAD)
1392 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001393
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001394 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001395 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001396 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 +02001397 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001398 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001399 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 +02001400 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1401 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001402 else if (ret == -2) {
1403 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);
1404 h1s->flags |= H1S_F_RX_CONGESTED;
1405 }
1406 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001407 goto end;
1408 }
1409
Christopher Faulete136bd12021-09-21 18:44:55 +02001410
1411 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload. There is a
1412 * payload if the c-l is not null or the the payload is chunk-encoded.
1413 * A parsing error is reported but a A 413-Payload-Too-Large is returned
1414 * instead of a 400-Bad-Request.
1415 */
1416 if (!(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
1417 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1418 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1419 h1s->flags |= H1S_F_PARSING_ERROR;
1420 htx->flags |= HTX_FL_PARSING_ERROR;
1421 h1s->h1c->errcode = 413;
1422 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);
1423 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1424 ret = 0;
1425 goto end;
1426 }
1427
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001428 /* Reject any message with an unknown transfer-encoding. In fact if any
1429 * encoding other than "chunked". A 422-Unprocessable-Content is
1430 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1431 * response.
1432 */
1433 if (h1m->flags & H1_MF_TE_OTHER) {
1434 h1s->flags |= H1S_F_PARSING_ERROR;
1435 htx->flags |= HTX_FL_PARSING_ERROR;
1436 if (!(h1m->flags & H1_MF_RESP))
1437 h1s->h1c->errcode = 422;
1438 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1439 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1440 ret = 0;
1441 goto end;
1442 }
1443
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001444 /* If websocket handshake, search for the websocket key */
1445 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1446 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1447 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1448 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001449 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001450 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 +01001451 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1452
1453 ret = 0;
1454 goto end;
1455 }
1456 }
1457
Christopher Faulet486498c2019-10-11 14:22:00 +02001458 if (h1m->err_pos >= 0) {
1459 /* Maybe we found an error during the parsing while we were
1460 * configured not to block on that, so we have to capture it
1461 * now.
1462 */
1463 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1464 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1465 }
1466
Christopher Faulet5696f542020-12-02 16:08:38 +01001467 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001468 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001469 if (h1s->meth == HTTP_METH_HEAD)
1470 h1s->flags |= H1S_F_BODYLESS_RESP;
1471 }
1472 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001473 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001474 if (h1s->status == 204 || h1s->status == 304)
1475 h1s->flags |= H1S_F_BODYLESS_RESP;
1476 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001477 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001479
Christopher Faulet129817b2018-09-20 16:14:40 +02001480 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001481 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 +02001482 return ret;
1483}
1484
1485/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001486 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001487 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1488 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001489 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001490static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1491 struct buffer *buf, size_t *ofs, size_t max,
1492 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001493{
Christopher Fauletde471a42021-02-01 16:37:28 +01001494 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001495
Willy Tarreau022e5e52020-09-10 09:33:15 +02001496 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 +02001497 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001498 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001499 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 +01001500 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001501 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001502 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 +02001503 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001504 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001505 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001506 }
1507
Christopher Faulet02a02532019-11-15 09:36:28 +01001508 *ofs += ret;
1509
1510 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001511 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1512 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);
1513 h1s->flags |= H1S_F_RX_CONGESTED;
1514 }
1515
Willy Tarreau022e5e52020-09-10 09:33:15 +02001516 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 +02001517 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001518}
1519
1520/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001521 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1522 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1523 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001524 * responsible to update the parser state <h1m>. If more room is requested,
1525 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001526 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001527static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1528 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001529{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001530 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001531
Willy Tarreau022e5e52020-09-10 09:33:15 +02001532 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 +02001533 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001534 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001535 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 +02001536 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001537 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001538 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 +02001539 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1540 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001541 else if (ret == -2) {
1542 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);
1543 h1s->flags |= H1S_F_RX_CONGESTED;
1544 }
1545 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001546 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001547 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001548
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001549 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001550
1551 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001552 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 +02001553 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001554}
1555
1556/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001557 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001558 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1559 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001560 *
1561 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001562 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001563static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001564{
Christopher Faulet539e0292018-11-19 10:40:09 +01001565 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001566 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001567 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001568 size_t data;
1569 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001570 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001571
Christopher Faulet539e0292018-11-19 10:40:09 +01001572 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001573 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001574
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001575 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001576 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001577
Christopher Faulet2eed8002020-12-07 11:26:13 +01001578 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001579 goto end;
1580
Christopher Faulet10a86702021-04-07 14:18:11 +02001581 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001582 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001583
Christopher Faulet46e058d2021-09-20 07:47:27 +02001584 /* Always remove congestion flags and try to process more input data */
1585 h1s->flags &= ~H1S_F_RX_CONGESTED;
1586
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001587 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001588 size_t used = htx_used_space(htx);
1589
Christopher Faulet129817b2018-09-20 16:14:40 +02001590 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001591 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001592 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001593 if (!ret)
1594 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001595
1596 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1597 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1598
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001599 if ((h1m->flags & H1_MF_RESP) &&
1600 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1601 h1m_init_res(&h1s->res);
1602 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001603 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001604 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001605 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001606 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001607 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001608 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001609 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001610 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001611
1612 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1613 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001614 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001615 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001616 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001617 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001618 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001619 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001620
Christopher Faulet76014fd2019-12-10 11:47:22 +01001621 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1622 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001623 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001624 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001625 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1626 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001627
Christopher Faulet5be651d2021-01-22 15:28:03 +01001628 if ((h1m->flags & H1_MF_RESP) &&
1629 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1630 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001631 else {
1632 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1633 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001634 h1s->flags |= H1S_F_RX_BLK;
1635 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001636 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001637 if (h1s->flags & H1S_F_TX_BLK) {
1638 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001639 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001640 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 +01001641 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001642 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001643 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001644 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001645 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001646 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001647 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001648 if (!ret)
1649 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001650
1651 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1652 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001653 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001654 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001655 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001656 break;
1657 }
1658
Christopher Faulet30db3d72019-05-17 15:35:33 +02001659 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001660 } 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 +01001661
Christopher Faulet129817b2018-09-20 16:14:40 +02001662
Christopher Faulet2eed8002020-12-07 11:26:13 +01001663 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001664 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 +02001665 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001666 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001667
Christopher Faulet539e0292018-11-19 10:40:09 +01001668 b_del(&h1c->ibuf, total);
1669
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001670 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001671 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1672
Christopher Faulet30db3d72019-05-17 15:35:33 +02001673 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001674 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001675 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001676 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 +01001677 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001678 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001679
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001680 if (!b_data(&h1c->ibuf))
1681 h1_release_buf(h1c, &h1c->ibuf);
1682
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001683 if (!(h1c->flags & H1C_F_ST_READY)) {
1684 /* The H1 connection is not ready. Most of time, there is no CS
1685 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1686 * cases, it is only possible on the client side.
1687 */
1688 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1689
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001690 if (h1m->state <= H1_MSG_LAST_LF) {
1691 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1692 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1693 goto end;
1694 }
1695
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001696 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1697 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1698 BUG_ON(h1s->cs);
1699 if (!h1s_new_cs(h1s, buf)) {
1700 h1c->flags |= H1C_F_ST_ERROR;
1701 goto err;
1702 }
1703 }
1704 else {
1705 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1706 BUG_ON(h1s->cs == NULL);
1707 if (!h1s_upgrade_cs(h1s, buf)) {
1708 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001709 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001710 goto err;
1711 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001712 }
1713 }
1714
1715 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001716 if (!(h1m->flags & H1_MF_CHNK) &&
1717 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1718 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1719 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1720 }
1721 else {
1722 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1723 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1724 }
1725
Christopher Fauleta22782b2021-02-08 17:18:01 +01001726 /* Set EOI on conn-stream in DONE state iff:
1727 * - it is a response
1728 * - it is a request but no a protocol upgrade nor a CONNECT
1729 *
1730 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001731 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001732 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001733 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1734 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Christopher Fauleta583af62020-09-24 15:35:37 +02001735 h1s->cs->flags |= CS_FL_EOI;
1736
Christopher Fauletec4207c2021-04-08 18:34:30 +02001737 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001738 /* When Input data are pending for this message, notify upper layer that
1739 * the mux need more space in the HTX buffer to continue if :
1740 *
1741 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1742 * - Headers or trailers are pending to be copied.
1743 */
1744 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Christopher Fauletcf56b992018-12-11 16:12:31 +01001745 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001746 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1747 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001748 else {
1749 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1750 if (h1s->flags & H1S_F_REOS) {
1751 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001752 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1753 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1754 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001755 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001756 }
Christopher Faulet26a26432021-01-27 11:27:50 +01001757 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001758 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001759 TRACE_ERROR("message aborted, set error on CS", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
1760 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001761
Christopher Faulet10a86702021-04-07 14:18:11 +02001762 if (h1s->flags & H1S_F_TX_BLK) {
1763 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001764 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001765 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 +01001766 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001767 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001768 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001769
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001770 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001771 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001772 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001773
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001774 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001775 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001776 if (h1s->cs)
1777 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001778 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001779 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001780}
1781
Christopher Faulet129817b2018-09-20 16:14:40 +02001782/*
1783 * Process outgoing data. It parses data and transfer them from the channel buffer into
1784 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1785 * 0 if it couldn't proceed.
1786 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001787static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001788{
Christopher Faulet129817b2018-09-20 16:14:40 +02001789 struct h1s *h1s = h1c->h1s;
1790 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001791 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001792 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001793 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001794 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001795 int last_data = 0;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001796 int ws_key_found = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001797
Christopher Faulet94b2c762019-05-24 15:28:57 +02001798 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001799 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1800
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001801 if (htx_is_empty(chn_htx))
1802 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001803
Christopher Faulet10a86702021-04-07 14:18:11 +02001804 if (h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01001805 goto end;
1806
Christopher Faulet51dbc942018-09-13 09:05:15 +02001807 if (!h1_get_buf(h1c, &h1c->obuf)) {
1808 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001809 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 +02001810 goto end;
1811 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001812
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001813 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001814
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001815 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001816 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001817
Willy Tarreau3815b222018-12-11 19:50:43 +01001818 /* Perform some optimizations to reduce the number of buffer copies.
1819 * First, if the mux's buffer is empty and the htx area contains
1820 * exactly one data block of the same size as the requested count,
1821 * then it's possible to simply swap the caller's buffer with the
1822 * mux's output buffer and adjust offsets and length to match the
1823 * entire DATA HTX block in the middle. In this case we perform a
1824 * true zero-copy operation from end-to-end. This is the situation
1825 * that happens all the time with large files. Second, if this is not
1826 * possible, but the mux's output buffer is empty, we still have an
1827 * opportunity to avoid the copy to the intermediary buffer, by making
1828 * the intermediary buffer's area point to the output buffer's area.
1829 * In this case we want to skip the HTX header to make sure that copies
1830 * remain aligned and that this operation remains possible all the
1831 * time. This goes for headers, data blocks and any data extracted from
1832 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001833 */
1834 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001835 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01001836 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01001837 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001838 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001839 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001840 void *old_area;
1841
Christopher Faulet6b81df72019-10-01 22:08:43 +02001842 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001843 if (h1m->state == H1_MSG_DATA && chn_htx->flags & HTX_FL_EOM) {
1844 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1845 last_data = 1;
1846 }
1847
Christopher Faulete5596bf2020-12-02 16:13:22 +01001848 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01001849 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001850 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001851 h1c->obuf.data = count;
1852
1853 buf->area = old_area;
1854 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001855
Christopher Faulet6b81df72019-10-01 22:08:43 +02001856 chn_htx = (struct htx *)buf->area;
1857 htx_reset(chn_htx);
1858
Christopher Fauletadb22202018-12-12 10:32:09 +01001859 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001860 * size and eventually the last chunk. We have at least
1861 * the size of the struct htx to write the chunk
1862 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01001863 */
1864 if (h1m->flags & H1_MF_CHNK) {
1865 h1_emit_chunk_size(&h1c->obuf, count);
1866 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001867 if (last_data) {
1868 /* Emit the last chunk too at the buffer's end */
1869 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
1870 }
Christopher Fauletadb22202018-12-12 10:32:09 +01001871 }
1872
Christopher Faulet6b81df72019-10-01 22:08:43 +02001873 if (h1m->state == H1_MSG_DATA)
1874 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001875 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001876 else
1877 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001878 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001879
Christopher Faulete5596bf2020-12-02 16:13:22 +01001880 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001881 if (last_data) {
1882 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02001883 if (h1s->flags & H1S_F_RX_BLK) {
1884 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001885 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001886 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 +01001887 }
1888
1889 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1890 H1_EV_TX_DATA, h1c->conn, h1s);
1891 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001892 goto out;
1893 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001894 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001895 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001896 else
1897 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001898
Christopher Fauletc2518a52019-06-25 21:41:02 +02001899 tmp.data = 0;
1900 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02001901 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001902 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001903 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001904 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001905 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001906 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001907
Christopher Fauletb2e84162018-12-06 11:39:49 +01001908 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001909 if (type != HTX_BLK_DATA && vlen > count)
1910 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001911
Christopher Faulet94b2c762019-05-24 15:28:57 +02001912 if (type == HTX_BLK_UNUSED)
1913 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001914
Christopher Faulet94b2c762019-05-24 15:28:57 +02001915 switch (h1m->state) {
1916 case H1_MSG_RQBEFORE:
1917 if (type != HTX_BLK_REQ_SL)
1918 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001919 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 +02001920 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001921 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001922 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001923 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001924 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001925 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001926 if (sl->flags & HTX_SL_F_BODYLESS)
1927 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001928 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01001929 if (h1s->meth == HTTP_METH_HEAD)
1930 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02001931 if (h1s->flags & H1S_F_RX_BLK) {
1932 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001933 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001934 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 +02001935 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001936 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001937
Christopher Faulet94b2c762019-05-24 15:28:57 +02001938 case H1_MSG_RPBEFORE:
1939 if (type != HTX_BLK_RES_SL)
1940 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001941 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 +02001942 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001943 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001944 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001945 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001946 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001947 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001948 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01001949 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02001950 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001951 else if (h1s->status == 204 || h1s->status == 304)
1952 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02001953 h1m->state = H1_MSG_HDR_FIRST;
1954 break;
1955
Christopher Faulet94b2c762019-05-24 15:28:57 +02001956 case H1_MSG_HDR_FIRST:
1957 case H1_MSG_HDR_NAME:
1958 case H1_MSG_HDR_L2_LWS:
1959 if (type == HTX_BLK_EOH)
1960 goto last_lf;
1961 if (type != HTX_BLK_HDR)
1962 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001963
Christopher Faulet9768c262018-10-22 09:34:31 +02001964 h1m->state = H1_MSG_HDR_NAME;
1965 n = htx_get_blk_name(chn_htx, blk);
1966 v = htx_get_blk_value(chn_htx, blk);
1967
Christopher Faulet86d144c2019-08-14 16:32:25 +02001968 /* Skip all pseudo-headers */
1969 if (*(n.ptr) == ':')
1970 goto skip_hdr;
1971
Christopher Faulet91fcf212020-12-02 16:17:15 +01001972 if (isteq(n, ist("transfer-encoding"))) {
1973 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
1974 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02001975 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01001976 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001977 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01001978 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
1979 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01001980 /* Only skip C-L header with invalid value. */
1981 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001982 goto skip_hdr;
1983 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001984 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001985 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001986 if (!v.len)
1987 goto skip_hdr;
1988 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01001989 else if (isteq(n, ist("upgrade"))) {
1990 h1_parse_upgrade_header(h1m, v);
1991 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001992 else if ((isteq(n, ist("sec-websocket-accept")) &&
1993 h1m->flags & H1_MF_RESP) ||
1994 (isteq(n, ist("sec-websocket-key")) &&
1995 !(h1m->flags & H1_MF_RESP))) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001996 ws_key_found = 1;
1997 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02001998 else if (isteq(n, ist("te"))) {
1999 /* "te" may only be sent with "trailers" if this value
2000 * is present, otherwise it must be deleted.
2001 */
2002 v = istist(v, ist("trailers"));
2003 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2004 goto skip_hdr;
2005 v = ist("trailers");
2006 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002007
Christopher Faulet67d58092019-10-02 10:51:38 +02002008 /* Skip header if same name is used to add the server name */
2009 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
2010 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
2011 goto skip_hdr;
2012
Christopher Faulet98fbe952019-07-22 16:18:24 +02002013 /* Try to adjust the case of the header name */
2014 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2015 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002016 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002017 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002018 skip_hdr:
2019 h1m->state = H1_MSG_HDR_L2_LWS;
2020 break;
2021
Christopher Faulet94b2c762019-05-24 15:28:57 +02002022 case H1_MSG_LAST_LF:
2023 if (type != HTX_BLK_EOH)
2024 goto error;
2025 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002026 h1m->state = H1_MSG_LAST_LF;
2027 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002028 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002029 /* If the reply comes from haproxy while the request is
2030 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002031 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2032 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2033 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002034 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2035 /* T-E + C-L: force close */
2036 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2037 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2038 }
2039 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2040 /* T-E + HTTP/1.0: force close */
2041 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2042 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2043 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002044
2045 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002046 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002047 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002048 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002049 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002050 /* Try to adjust the case of the header name */
2051 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2052 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002053 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002054 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002055 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002056 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002057 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002058
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002059 if ((h1s->meth != HTTP_METH_CONNECT &&
2060 (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 +01002061 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002062 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002063 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002064 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2065 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002066 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002067 n = ist("transfer-encoding");
2068 v = ist("chunked");
2069 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2070 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002071 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002072 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002073 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002074 h1m->flags |= H1_MF_CHNK;
2075 }
2076
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002077 /* Now add the server name to a header (if requested) */
2078 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
2079 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
2080 struct server *srv = objt_server(h1c->conn->target);
2081
2082 if (srv) {
2083 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
2084 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002085
2086 /* Try to adjust the case of the header name */
2087 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2088 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002089 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002090 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002091 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002092 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002093 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2094 }
2095
Amaury Denoyellec1938232020-12-11 17:53:03 +01002096 /* Add websocket handshake key if needed */
2097 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) &&
2098 !ws_key_found) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002099 if (!(h1m->flags & H1_MF_RESP)) {
2100 /* generate a random websocket key
2101 * stored in the session to
2102 * verify it on the response side
2103 */
2104 h1_generate_random_ws_input_key(h1s->ws_key);
2105
2106 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2107 ist(h1s->ws_key),
2108 &tmp)) {
2109 goto full;
2110 }
2111 }
2112 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002113 /* add the response header key */
2114 char key[29];
2115 h1_calculate_ws_output_key(h1s->ws_key, key);
2116 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2117 ist(key),
2118 &tmp)) {
2119 goto full;
2120 }
2121 }
2122 }
2123
Christopher Faulet6b81df72019-10-01 22:08:43 +02002124 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2125 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2126
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002127 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002128 if (!chunk_memcat(&tmp, "\r\n", 2))
2129 goto full;
2130 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002131 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002132 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002133 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002134 if (!chunk_memcat(&tmp, "\r\n", 2))
2135 goto full;
2136 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002137 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002138 else if ((h1m->flags & H1_MF_RESP) &&
2139 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002140 if (!chunk_memcat(&tmp, "\r\n", 2))
2141 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002142 h1m_init_res(&h1s->res);
2143 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002144 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002145 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002146 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002147 else {
2148 /* EOM flag is set and it is the last block */
2149 if (htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002150 if (h1m->flags & H1_MF_CHNK) {
2151 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2152 goto full;
2153 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002154 else if (!chunk_memcat(&tmp, "\r\n", 2))
2155 goto full;
2156 goto done;
2157 }
2158 else if (!chunk_memcat(&tmp, "\r\n", 2))
2159 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002160 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002161 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002162 break;
2163
Christopher Faulet94b2c762019-05-24 15:28:57 +02002164 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002165 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002166 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002167 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2168 goto trailers;
2169
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002170 /* If the message is not chunked, never
2171 * add the last chunk. */
2172 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002173 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002174 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 +02002175 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002176 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002177 else if (type != HTX_BLK_DATA)
2178 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002179
2180 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 +02002181
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002182 /* It is the last block of this message. After this one,
2183 * only tunneled data may be forwarded. */
2184 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2185 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2186 last_data = 1;
2187 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002188
2189 if (vlen > count) {
2190 /* Get the maximum amount of data we can xferred */
2191 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002192 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002193 }
2194
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002195 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2196 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2197 goto skip_data;
2198 }
2199
Christopher Fauletd9233f02019-10-14 14:01:24 +02002200 chklen = 0;
2201 if (h1m->flags & H1_MF_CHNK) {
2202 chklen = b_room(&tmp);
2203 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2204 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2205 (chklen < 1048576) ? 5 : 8);
2206 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002207
2208 /* If it is the end of the chunked message (without EOT), reserve the
2209 * last chunk size */
2210 if (last_data)
2211 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002212 }
2213
2214 if (vlen + chklen > b_room(&tmp)) {
2215 /* too large for the buffer */
2216 if (chklen >= b_room(&tmp))
2217 goto full;
2218 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002219 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002220 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002221 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002222 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002223 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002224 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002225
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002226 /* Space already reserved, so it must succeed */
2227 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2228 goto error;
2229
Christopher Faulet6b81df72019-10-01 22:08:43 +02002230 if (h1m->state == H1_MSG_DATA)
2231 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002232 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002233 else
2234 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002235 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002236
2237 skip_data:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002238 if (last_data)
2239 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002240 break;
2241
Christopher Faulet94b2c762019-05-24 15:28:57 +02002242 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002243 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002244 goto error;
2245 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002246 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002247
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002248 /* If the message is not chunked, ignore
2249 * trailers. It may happen with H2 messages. */
Christopher Faulet0a916d22021-02-10 08:48:19 +01002250 if (!(h1m->flags & H1_MF_CHNK)) {
2251 if (type == HTX_BLK_EOT)
2252 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002253 break;
Christopher Faulet0a916d22021-02-10 08:48:19 +01002254 }
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002255
Christopher Faulete5596bf2020-12-02 16:13:22 +01002256 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2257 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 +01002258 if (type == HTX_BLK_EOT)
2259 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002260 break;
2261 }
2262
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002263 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002264 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002265 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002266 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2267 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002268 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002269 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002270 else { // HTX_BLK_TLR
2271 n = htx_get_blk_name(chn_htx, blk);
2272 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002273
2274 /* Try to adjust the case of the header name */
2275 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2276 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002277 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002278 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002279 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002280 break;
2281
Christopher Faulet94b2c762019-05-24 15:28:57 +02002282 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002283 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2284 goto error; /* For now return an error */
2285
Christopher Faulet94b2c762019-05-24 15:28:57 +02002286 done:
Christopher Faulet36893672021-02-10 09:52:07 +01002287 if (!(chn_htx->flags & HTX_FL_EOM)) {
2288 TRACE_STATE("No EOM flags in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2289 goto error; /* For now return an error */
2290 }
2291
Christopher Faulet94b2c762019-05-24 15:28:57 +02002292 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002293 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002294 h1s->flags |= H1S_F_TX_BLK;
2295 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002296 }
2297 else if ((h1m->flags & H1_MF_RESP) &&
2298 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2299 /* a successful reply to a CONNECT or a protocol switching is sent
2300 * to the client. Switch the response to tunnel mode.
2301 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002302 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002303 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 +01002304 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002305
Christopher Faulet10a86702021-04-07 14:18:11 +02002306 if (h1s->flags & H1S_F_RX_BLK) {
2307 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002308 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002309 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 +02002310 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002311
2312 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2313 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002314 break;
2315
Christopher Faulet9768c262018-10-22 09:34:31 +02002316 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002317 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002318 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002319 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002320 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002321 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002322 TRACE_ERROR("processing output error, set error on h1c/h1s",
2323 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002324 break;
2325 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002326
2327 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002328 total += vlen;
2329 count -= vlen;
2330 if (sz == vlen)
2331 blk = htx_remove_blk(chn_htx, blk);
2332 else {
2333 htx_cut_data_blk(chn_htx, blk, vlen);
2334 break;
2335 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002336 }
2337
Christopher Faulet9768c262018-10-22 09:34:31 +02002338 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002339 /* when the output buffer is empty, tmp shares the same area so that we
2340 * only have to update pointers and lengths.
2341 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002342 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2343 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002344 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002345 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002346
Willy Tarreau3815b222018-12-11 19:50:43 +01002347 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002348 out:
2349 if (!buf_room_for_htx_data(&h1c->obuf)) {
2350 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002351 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002352 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002353 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002354 /* Both the request and the response reached the DONE state. So set EOI
2355 * flag on the conn-stream. Most of time, the flag will already be set,
2356 * except for protocol upgrades. Report an error if data remains blocked
2357 * in the output buffer.
2358 */
2359 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2360 if (!htx_is_empty(chn_htx)) {
2361 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002362 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 +01002363 }
2364 h1s->cs->flags |= CS_FL_EOI;
2365 }
2366
Christopher Faulet6b81df72019-10-01 22:08:43 +02002367 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002368 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002369
2370 full:
2371 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2372 h1c->flags |= H1C_F_OUT_FULL;
2373 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374}
2375
Christopher Faulet51dbc942018-09-13 09:05:15 +02002376/*********************************************************/
2377/* functions below are I/O callbacks from the connection */
2378/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002379static void h1_wake_stream_for_recv(struct h1s *h1s)
2380{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002381 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002382 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002383 tasklet_wakeup(h1s->subs->tasklet);
2384 h1s->subs->events &= ~SUB_RETRY_RECV;
2385 if (!h1s->subs->events)
2386 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002387 }
2388}
2389static void h1_wake_stream_for_send(struct h1s *h1s)
2390{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002391 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002392 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002393 tasklet_wakeup(h1s->subs->tasklet);
2394 h1s->subs->events &= ~SUB_RETRY_SEND;
2395 if (!h1s->subs->events)
2396 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002397 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002398}
2399
Christopher Fauletad4daf62021-01-21 17:49:01 +01002400/* alerts the data layer following this sequence :
2401 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2402 * - if its subscribed to send, then it's woken up for send
2403 * - if it was subscribed to neither, its ->wake() callback is called
2404 */
2405static void h1_alert(struct h1s *h1s)
2406{
2407 if (h1s->subs) {
2408 h1_wake_stream_for_recv(h1s);
2409 h1_wake_stream_for_send(h1s);
2410 }
2411 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2412 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2413 h1s->cs->data_cb->wake(h1s->cs);
2414 }
2415}
2416
Christopher Fauletc18fc232020-10-06 17:41:36 +02002417/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2418 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2419 * retryable errors (allocation error or buffer full). On success, the error is
2420 * copied in the output buffer.
2421*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002422static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002423{
2424 int rc = http_get_status_idx(h1c->errcode);
2425 int ret = 0;
2426
2427 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2428
2429 /* Verify if the error is mapped on /dev/null or any empty file */
2430 /// XXX: do a function !
2431 if (h1c->px->replies[rc] &&
2432 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2433 h1c->px->replies[rc]->body.errmsg &&
2434 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2435 /* Empty error, so claim a success */
2436 ret = 1;
2437 goto out;
2438 }
2439
2440 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2441 h1c->flags |= H1C_F_ERR_PENDING;
2442 goto out;
2443 }
2444
2445 if (!h1_get_buf(h1c, &h1c->obuf)) {
2446 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2447 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2448 goto out;
2449 }
2450 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2451 if (unlikely(ret <= 0)) {
2452 if (!ret) {
2453 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2454 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2455 goto out;
2456 }
2457 else {
2458 /* we cannot report this error, so claim a success */
2459 ret = 1;
2460 }
2461 }
2462 h1c->flags &= ~H1C_F_ERR_PENDING;
2463 out:
2464 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2465 return ret;
2466}
2467
2468/* Try to send a 500 internal error. It relies on h1_send_error to send the
2469 * error. This function takes care of incrementing stats and tracked counters.
2470 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002471static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002472{
2473 struct session *sess = h1c->conn->owner;
2474 int ret = 1;
2475
2476 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002477 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002478 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2479 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002480 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002481 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002482
2483 h1c->errcode = 500;
2484 ret = h1_send_error(h1c);
2485 sess_log(sess);
2486 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002487}
2488
Christopher Fauletb3230f72021-09-21 18:38:20 +02002489/* Try to send an error because of a parsing error. By default a 400 bad request
2490 * error is returned. But the status code may be specified by setting
2491 * h1c->errcode. It relies on h1_send_error to send the error. This function
2492 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002493 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002494static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002495{
2496 struct session *sess = h1c->conn->owner;
2497 int ret = 1;
2498
2499 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2500 goto end;
2501
2502 session_inc_http_req_ctr(sess);
2503 session_inc_http_err_ctr(sess);
2504 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002505 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2506 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002507 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002508 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002509
Christopher Fauletb3230f72021-09-21 18:38:20 +02002510 if (!h1c->errcode)
2511 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002512 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002513 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2514 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002515
2516 end:
2517 return ret;
2518}
2519
Christopher Faulet2eed8002020-12-07 11:26:13 +01002520/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2521 * the error. This function takes care of incrementing stats and tracked
2522 * counters.
2523 */
2524static int h1_handle_not_impl_err(struct h1c *h1c)
2525{
2526 struct session *sess = h1c->conn->owner;
2527 int ret = 1;
2528
2529 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2530 goto end;
2531
2532 session_inc_http_req_ctr(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002533 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002534 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2535 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002536 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002537 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002538
2539 h1c->errcode = 501;
2540 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002541 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2542 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002543
2544 end:
2545 return ret;
2546}
2547
Christopher Fauletc18fc232020-10-06 17:41:36 +02002548/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2549 * error. This function takes care of incrementing stats and tracked counters.
2550 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002551static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002552{
2553 struct session *sess = h1c->conn->owner;
2554 int ret = 1;
2555
2556 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2557 goto end;
2558
2559 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002560 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002561 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2562 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002563 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002564 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002565
2566 h1c->errcode = 408;
Christopher Faulet07e10de2021-07-26 09:42:49 +02002567 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2568 ret = h1_send_error(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002569 sess_log(sess);
2570 end:
2571 return ret;
2572}
2573
2574
Christopher Faulet51dbc942018-09-13 09:05:15 +02002575/*
2576 * Attempt to read data, and subscribe if none available
2577 */
2578static int h1_recv(struct h1c *h1c)
2579{
2580 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002581 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002582 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002583
Christopher Faulet6b81df72019-10-01 22:08:43 +02002584 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2585
2586 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2587 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002588 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002589 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002590
Christopher Fauletae635762020-09-21 11:47:16 +02002591 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2592 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002593 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002594 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002595
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002596 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2597 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002598 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002599 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002600 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002601
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002602 /*
2603 * If we only have a small amount of data, realign it,
2604 * it's probably cheaper than doing 2 recv() calls.
2605 */
2606 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002607 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002608
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002609 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002610 if (!h1c->h1s ||
2611 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2612 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002613 flags |= CO_RFL_READ_ONCE;
2614
Willy Tarreau45f2b892018-12-05 07:59:27 +01002615 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002616 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002617 if (h1c->flags & H1C_F_IN_FULL) {
2618 h1c->flags &= ~H1C_F_IN_FULL;
2619 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2620 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002621
2622 if (!b_data(&h1c->ibuf)) {
2623 /* try to pre-align the buffer like the rxbufs will be
2624 * to optimize memory copies.
2625 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002626 h1c->ibuf.head = sizeof(struct htx);
2627 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002628 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002629 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002630 if (max && !ret && h1_recv_allowed(h1c)) {
2631 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2632 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002633 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002634 else {
2635 h1_wake_stream_for_recv(h1c->h1s);
2636 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002637 }
2638
Christopher Faulet51dbc942018-09-13 09:05:15 +02002639 if (!b_data(&h1c->ibuf))
2640 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002641 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002642 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002643 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2644 }
2645
2646 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002647 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002648}
2649
2650
2651/*
2652 * Try to send data if possible
2653 */
2654static int h1_send(struct h1c *h1c)
2655{
2656 struct connection *conn = h1c->conn;
2657 unsigned int flags = 0;
2658 size_t ret;
2659 int sent = 0;
2660
Christopher Faulet6b81df72019-10-01 22:08:43 +02002661 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2662
2663 if (conn->flags & CO_FL_ERROR) {
2664 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002665 b_reset(&h1c->obuf);
2666 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002667 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002668
2669 if (!b_data(&h1c->obuf))
2670 goto end;
2671
Christopher Faulet46230362019-10-17 16:04:20 +02002672 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002673 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002674 if (h1c->flags & H1C_F_CO_STREAMER)
2675 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002676
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002677 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002678 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002679 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002680 if (h1c->flags & H1C_F_OUT_FULL) {
2681 h1c->flags &= ~H1C_F_OUT_FULL;
2682 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2683 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002684 b_del(&h1c->obuf, ret);
2685 sent = 1;
2686 }
2687
Christopher Faulet145aa472018-12-06 10:56:20 +01002688 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002689 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002690 /* error or output closed, nothing to send, clear the buffer to release it */
2691 b_reset(&h1c->obuf);
2692 }
2693
Christopher Faulet51dbc942018-09-13 09:05:15 +02002694 end:
Christopher Faulet10a86702021-04-07 14:18:11 +02002695 if (!(h1c->flags & H1C_F_OUT_FULL))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002696 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002697
Christopher Faulet51dbc942018-09-13 09:05:15 +02002698 /* We're done, no more to send */
2699 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002700 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002701 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002702 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002703 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002704 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002705 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002706 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002707 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2708 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002709 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002710 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002711
Christopher Faulet6b81df72019-10-01 22:08:43 +02002712 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002713 return sent;
2714}
2715
Christopher Faulet51dbc942018-09-13 09:05:15 +02002716/* callback called on any event by the connection handler.
2717 * It applies changes and returns zero, or < 0 if it wants immediate
2718 * destruction of the connection.
2719 */
2720static int h1_process(struct h1c * h1c)
2721{
2722 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002723 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002724
Christopher Faulet6b81df72019-10-01 22:08:43 +02002725 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2726
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002727 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002728 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Fauletab7389d2021-09-16 08:16:23 +02002729 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2730 !(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 +02002731 struct buffer *buf;
2732 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002733
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002734 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2735 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002736 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002737
2738 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002739 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002740 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2741 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002742 /* Try to match H2 preface before parsing the request headers. */
2743 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2744 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002745 if (h1c->flags & H1C_F_ST_ATTACHED) {
2746 /* Force the REOS here to be sure to release the CS.
2747 Here ATTACHED implies !READY, and h1s defined
2748 */
2749 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2750 h1s->flags |= H1S_F_REOS;
2751 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002752 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002753 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002754 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002755 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002756
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002757 /* Create the H1 stream if not already there */
2758 if (!h1s) {
2759 h1s = h1c_frt_stream_new(h1c);
2760 if (!h1s) {
2761 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002762 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002763 goto no_parsing;
2764 }
2765 }
2766
2767 if (h1s->sess->t_idle == -1)
2768 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2769
2770 /* Get the stream rxbuf */
2771 buf = h1_get_buf(h1c, &h1s->rxbuf);
2772 if (!buf) {
2773 h1c->flags |= H1C_F_IN_SALLOC;
2774 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2775 return 0;
2776 }
2777
2778 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01002779 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002780 h1_release_buf(h1c, &h1s->rxbuf);
2781 h1_set_idle_expiration(h1c);
2782
2783 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002784 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002785 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002786 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet26a26432021-01-27 11:27:50 +01002787 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002788 }
2789 else if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02002790 h1_handle_parsing_error(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002791 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002792 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002793 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002794 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2795 h1_handle_not_impl_err(h1c);
2796 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002797 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002798 }
Christopher Fauletae635762020-09-21 11:47:16 +02002799 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002800 h1_send(h1c);
2801
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002802 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002803 if (!(h1c->flags & H1C_F_ST_READY)) {
2804 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002805 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002806 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02002807 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002808 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002809 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002810 }
2811
2812 /* Handle pending error, if any (only possible on frontend connection) */
2813 if (h1c->flags & H1C_F_ERR_PENDING) {
2814 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2815 if (h1_send_error(h1c))
2816 h1_send(h1c);
2817 }
Christopher Fauletae635762020-09-21 11:47:16 +02002818
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002819 /* If there is some pending outgoing data or error, just wait */
2820 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2821 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002822
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002823 /* Otherwise we can release the H1 connection */
2824 goto release;
2825 }
2826 else {
2827 /* Here there is still a H1 stream with a conn-stream.
2828 * Report the connection state at the stream level
2829 */
2830 if (conn_xprt_read0_pending(conn)) {
2831 h1s->flags |= H1S_F_REOS;
2832 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2833 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002834 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002835 h1s->cs->flags |= CS_FL_ERROR;
2836 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002837 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002838 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002839 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002840
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002841 if (!b_data(&h1c->ibuf))
2842 h1_release_buf(h1c, &h1c->ibuf);
2843
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02002844 /* Check if a soft-stop is in progress.
2845 * Release idling front connection if this is the case.
2846 */
2847 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002848 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02002849 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
2850 goto release;
2851 }
2852 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002853
2854 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2855 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2856 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002857 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002858
Christopher Faulet47365272018-10-31 17:40:50 +01002859 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002860 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002861 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002862 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002863
2864 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002865 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05002866 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002867 * attached CS first. Here, the H1C must not be READY */
2868 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2869
2870 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2871 h1s->cs->flags |= CS_FL_EOS;
2872 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2873 h1s->cs->flags |= CS_FL_ERROR;
2874 h1_alert(h1s);
2875 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2876 }
2877 else {
2878 h1_release(h1c);
2879 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2880 }
Christopher Faulet539e0292018-11-19 10:40:09 +01002881 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002882}
2883
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002884struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002885{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002886 struct connection *conn;
2887 struct tasklet *tl = (struct tasklet *)t;
2888 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002889 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002890 int ret = 0;
2891
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002892 if (state & TASK_F_USR1) {
2893 /* the tasklet was idling on an idle connection, it might have
2894 * been stolen, let's be careful!
2895 */
2896 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2897 if (tl->context == NULL) {
2898 /* The connection has been taken over by another thread,
2899 * we're no longer responsible for it, so just free the
2900 * tasklet, and do nothing.
2901 */
2902 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2903 tasklet_free(tl);
2904 return NULL;
2905 }
2906 conn = h1c->conn;
2907 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002908
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002909 /* Remove the connection from the list, to be sure nobody attempts
2910 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002911 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002912 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2913 if (conn_in_list)
2914 conn_delete_from_tree(&conn->hash_node->node);
2915
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002916 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002917 } else {
2918 /* we're certain the connection was not in an idle list */
2919 conn = h1c->conn;
2920 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2921 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002922 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002923
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002924 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002925 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002926 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002927 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002928 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002929 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01002930
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002931 /* If we were in an idle list, we want to add it back into it,
2932 * unless h1_process() returned -1, which mean it has destroyed
2933 * the connection (testing !ret is enough, if h1_process() wasn't
2934 * called then ret will be 0 anyway.
2935 */
Willy Tarreau74163142021-03-13 11:30:19 +01002936 if (ret < 0)
2937 t = NULL;
2938
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002939 if (!ret && conn_in_list) {
2940 struct server *srv = objt_server(conn->target);
2941
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002942 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002943 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01002944 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002945 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01002946 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002947 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002948 }
Willy Tarreau74163142021-03-13 11:30:19 +01002949 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002950}
2951
Christopher Faulet51dbc942018-09-13 09:05:15 +02002952static int h1_wake(struct connection *conn)
2953{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002954 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002955 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002956
Christopher Faulet6b81df72019-10-01 22:08:43 +02002957 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2958
Christopher Faulet539e0292018-11-19 10:40:09 +01002959 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002960 ret = h1_process(h1c);
2961 if (ret == 0) {
2962 struct h1s *h1s = h1c->h1s;
2963
Christopher Fauletad4daf62021-01-21 17:49:01 +01002964 if (h1c->flags & H1C_F_ST_ATTACHED)
2965 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002966 }
2967 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002968}
2969
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002970/* Connection timeout management. The principle is that if there's no receipt
2971 * nor sending for a certain amount of time, the connection is closed.
2972 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002973struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002974{
2975 struct h1c *h1c = context;
2976 int expired = tick_is_expired(t->expire, now_ms);
2977
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002978 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002979
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002980 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002981 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002982 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002983
2984 /* Somebody already stole the connection from us, so we should not
2985 * free it, we just have to free the task.
2986 */
2987 if (!t->context) {
2988 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002989 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002990 goto do_leave;
2991 }
2992
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002993 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002994 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002995 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002996 return t;
2997 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002998
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002999 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003000 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003001 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003002 if (h1c->flags & H1C_F_ST_READY) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003003 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003004 t->expire = TICK_ETERNITY;
3005 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
3006 return t;
3007 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003008
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003009 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003010 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
3011 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003012 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003013 if (h1_handle_req_tout(h1c))
3014 h1_send(h1c);
3015 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
3016 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003017 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003018 return t;
3019 }
3020 }
3021
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003022 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003023 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003024 * attached CS first. Here, the H1C must not be READY */
3025 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
3026 h1_alert(h1c->h1s);
3027 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003028 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003029 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
3030 return t;
3031 }
3032
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003033 /* We're about to destroy the connection, so make sure nobody attempts
3034 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003035 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003036 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003037 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003038
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003039 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003040 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003041
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003042 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003043 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003044
3045 if (!h1c) {
3046 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003047 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003048 return NULL;
3049 }
3050
3051 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003052 h1_release(h1c);
3053 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003054 return NULL;
3055}
3056
Christopher Faulet51dbc942018-09-13 09:05:15 +02003057/*******************************************/
3058/* functions below are used by the streams */
3059/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003060
Christopher Faulet51dbc942018-09-13 09:05:15 +02003061/*
3062 * Attach a new stream to a connection
3063 * (Used for outgoing connections)
3064 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003065static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003066{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003067 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003068 struct conn_stream *cs = NULL;
3069 struct h1s *h1s;
3070
Christopher Faulet6b81df72019-10-01 22:08:43 +02003071 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003072 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003073 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3074 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003075 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003076
Christopher Faulet236c93b2020-07-02 09:19:54 +02003077 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003078 if (!cs) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003079 TRACE_ERROR("CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3080 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003081 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003082
Christopher Faulet2f0ec662020-09-24 10:30:15 +02003083 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003084 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003085 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3086 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003087 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003088
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003089 /* the connection is not idle anymore, let's mark this */
3090 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003091 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003092
Christopher Faulet6b81df72019-10-01 22:08:43 +02003093 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003094 return cs;
Christopher Faulet26a26432021-01-27 11:27:50 +01003095 err:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003096 cs_free(cs);
Christopher Faulet26a26432021-01-27 11:27:50 +01003097 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003098 return NULL;
3099}
3100
3101/* Retrieves a valid conn_stream from this connection, or returns NULL. For
3102 * this mux, it's easy as we can only store a single conn_stream.
3103 */
3104static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
3105{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003106 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003107 struct h1s *h1s = h1c->h1s;
3108
3109 if (h1s)
3110 return h1s->cs;
3111
3112 return NULL;
3113}
3114
Christopher Faulet73c12072019-04-08 11:23:22 +02003115static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003116{
Christopher Faulet73c12072019-04-08 11:23:22 +02003117 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003118
Christopher Faulet6b81df72019-10-01 22:08:43 +02003119 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003120 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003121 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003122}
3123
3124/*
3125 * Detach the stream from the connection and possibly release the connection.
3126 */
3127static void h1_detach(struct conn_stream *cs)
3128{
3129 struct h1s *h1s = cs->ctx;
3130 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003131 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003132 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003133
Christopher Faulet6b81df72019-10-01 22:08:43 +02003134 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3135
Christopher Faulet51dbc942018-09-13 09:05:15 +02003136 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003137 if (!h1s) {
3138 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003139 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003140 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003141
Olivier Houchardf502aca2018-12-14 19:42:40 +01003142 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003143 h1c = h1s->h1c;
3144 h1s->cs = NULL;
3145
Christopher Faulet42849b02020-10-05 11:35:17 +02003146 sess->accept_date = date;
3147 sess->tv_accept = now;
3148 sess->t_handshake = 0;
3149 sess->t_idle = -1;
3150
Olivier Houchard8a786902018-12-15 16:05:40 +01003151 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3152 h1s_destroy(h1s);
3153
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003154 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 +02003155 /* If there are any excess server data in the input buffer,
3156 * release it and close the connection ASAP (some data may
3157 * remain in the output buffer). This happens if a server sends
3158 * invalid responses. So in such case, we don't want to reuse
3159 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003160 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003161 if (b_data(&h1c->ibuf)) {
3162 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003163 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003164 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003165 goto release;
3166 }
Christopher Faulet03627242019-07-19 11:34:08 +02003167
Christopher Faulet08016ab2020-07-01 16:10:06 +02003168 if (h1c->conn->flags & CO_FL_PRIVATE) {
3169 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003170 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3171 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003172 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003173 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003174 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003175 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003176 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003177 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003178 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3179 goto end;
3180 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003181 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003182 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003183 if (h1c->conn->owner == sess)
3184 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003185
3186 /* mark that the tasklet may lose its context to another thread and
3187 * that the handler needs to check it under the idle conns lock.
3188 */
3189 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003190 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003191 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3192
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003193 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003194 /* The server doesn't want it, let's kill the connection right away */
3195 h1c->conn->mux->destroy(h1c);
3196 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3197 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003198 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003199 /* At this point, the connection has been added to the
3200 * server idle list, so another thread may already have
3201 * hijacked it, so we can't do anything with it.
3202 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003203 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003204 }
3205 }
3206
Christopher Fauletf1204b82019-07-19 14:51:06 +02003207 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003208 /* 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 +01003209 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02003210 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003211 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003212 !h1c->conn->owner) {
3213 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003214 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003215 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003216 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003217 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003218 /* If we have a new request, process it immediately or
3219 * subscribe for reads waiting for new data
3220 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003221 if (unlikely(b_data(&h1c->ibuf))) {
3222 if (h1_process(h1c) == -1)
3223 goto end;
3224 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003225 else
3226 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3227 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003228 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003229 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003230 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003231 end:
3232 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003233}
3234
3235
3236static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3237{
3238 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02003239 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003240
3241 if (!h1s)
3242 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003243 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003244
Christopher Faulet6b81df72019-10-01 22:08:43 +02003245 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3246
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003247 if (cs->flags & CS_FL_SHR)
3248 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003249 if (cs->flags & CS_FL_KILL_CONN) {
3250 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
3251 goto do_shutr;
3252 }
3253 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3254 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003255 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003256 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003257
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003258 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3259 /* Here attached is implicit because there is CS */
3260 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3261 goto end;
3262 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003263 if (h1s->flags & H1S_F_WANT_KAL) {
3264 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003265 goto end;
3266 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003267
Christopher Faulet7f366362019-04-08 10:51:20 +02003268 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003269 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
3270 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003271 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003272 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003273 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02003274 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003275 end:
3276 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003277}
3278
3279static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3280{
3281 struct h1s *h1s = cs->ctx;
3282 struct h1c *h1c;
3283
3284 if (!h1s)
3285 return;
3286 h1c = h1s->h1c;
3287
Christopher Faulet6b81df72019-10-01 22:08:43 +02003288 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3289
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003290 if (cs->flags & CS_FL_SHW)
3291 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003292 if (cs->flags & CS_FL_KILL_CONN) {
3293 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003294 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003295 }
3296 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3297 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3298 goto do_shutw;
3299 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003300
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003301 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3302 /* Here attached is implicit because there is CS */
3303 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3304 goto end;
3305 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003306 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3307 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003308 goto end;
3309 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003310
Christopher Faulet7f366362019-04-08 10:51:20 +02003311 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003312 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Fauleta85c5222021-10-27 15:36:38 +02003313 if (mode != CS_SHW_NORMAL)
3314 h1c->flags |= H1C_F_ST_SILENT_SHUT;
3315
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003316 if (!b_data(&h1c->obuf))
Christopher Fauleta85c5222021-10-27 15:36:38 +02003317 h1_shutw_conn(cs->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003318 end:
3319 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003320}
3321
Christopher Fauleta85c5222021-10-27 15:36:38 +02003322static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003323{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003324 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003325
Willy Tarreau62592ad2021-03-26 09:09:42 +01003326 if (conn->flags & CO_FL_SOCK_WR_SH)
3327 return;
3328
Christopher Fauleta85c5222021-10-27 15:36:38 +02003329 TRACE_ENTER(H1_EV_H1C_END, conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003330 conn_xprt_shutw(conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02003331 conn_sock_shutw(conn, (h1c && !(h1c->flags & H1C_F_ST_SILENT_SHUT)));
3332 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003333}
3334
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003335/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3336 * The <es> pointer is not allowed to differ from the one passed to the
3337 * subscribe() call. It always returns zero.
3338 */
3339static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003340{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003341 struct h1s *h1s = cs->ctx;
3342
3343 if (!h1s)
3344 return 0;
3345
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003346 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003347 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003348
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003349 es->events &= ~event_type;
3350 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003351 h1s->subs = NULL;
3352
3353 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003354 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003355
3356 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003357 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003358
Christopher Faulet51dbc942018-09-13 09:05:15 +02003359 return 0;
3360}
3361
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003362/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3363 * event subscriber <es> is not allowed to change from a previous call as long
3364 * as at least one event is still subscribed. The <event_type> must only be a
3365 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3366 * the conn_stream <cs> was already detached, in which case it will return -1.
3367 */
3368static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003369{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003370 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003371 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003372
3373 if (!h1s)
3374 return -1;
3375
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003376 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003377 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003378
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003379 es->events |= event_type;
3380 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003381
3382 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003383 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003384
3385
Christopher Faulet6b81df72019-10-01 22:08:43 +02003386 if (event_type & SUB_RETRY_SEND) {
3387 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003388 /*
3389 * If the conn_stream attempt to subscribe, and the
3390 * mux isn't subscribed to the connection, then it
3391 * probably means the connection wasn't established
3392 * yet, so we have to subscribe.
3393 */
3394 h1c = h1s->h1c;
3395 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3396 h1c->conn->xprt->subscribe(h1c->conn,
3397 h1c->conn->xprt_ctx,
3398 SUB_RETRY_SEND,
3399 &h1c->wait_event);
3400 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003401 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003402}
3403
Christopher Faulet564e39c2021-09-21 15:50:55 +02003404/* Called from the upper layer, to receive data.
3405 *
3406 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3407 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3408 * means the caller wants to flush input data (from the mux buffer and the
3409 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3410 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3411 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3412 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3413 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3414 * copy as much data as possible.
3415 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003416static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3417{
3418 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003419 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003420 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003421 size_t ret = 0;
3422
Willy Tarreau022e5e52020-09-10 09:33:15 +02003423 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003424
3425 /* Do nothing for now if not READY */
3426 if (!(h1c->flags & H1C_F_ST_READY)) {
3427 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3428 goto end;
3429 }
3430
Christopher Faulet539e0292018-11-19 10:40:09 +01003431 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003432 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003433 else
3434 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003435
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003436 if ((flags & CO_RFL_BUF_FLUSH) && (cs->flags & CS_FL_MAY_SPLICE)) {
3437 h1c->flags |= H1C_F_WANT_SPLICE;
3438 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 +02003439 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003440 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003441 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 +01003442 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003443 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003444
3445 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003446 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003447 return ret;
3448}
3449
3450
3451/* Called from the upper layer, to send data */
3452static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3453{
3454 struct h1s *h1s = cs->ctx;
3455 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003456 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003457
3458 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003459 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003460 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003461
Willy Tarreau022e5e52020-09-10 09:33:15 +02003462 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003463
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003464 /* If we're not connected yet, or we're waiting for a handshake, stop
3465 * now, as we don't want to remove everything from the channel buffer
3466 * before we're sure we can send it.
3467 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003468 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003469 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003470 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003471 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003472
Christopher Fauletdea24742021-01-22 15:12:30 +01003473 if (h1c->flags & H1C_F_ST_ERROR) {
3474 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003475 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 +01003476 return 0;
3477 }
3478
Christopher Faulet46230362019-10-17 16:04:20 +02003479 /* Inherit some flags from the upper layer */
3480 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3481 if (flags & CO_SFL_MSG_MORE)
3482 h1c->flags |= H1C_F_CO_MSG_MORE;
3483 if (flags & CO_SFL_STREAMER)
3484 h1c->flags |= H1C_F_CO_STREAMER;
3485
Christopher Fauletc31872f2019-06-04 22:09:36 +02003486 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003487 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003488
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003489 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003490 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003491 else
3492 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003493
3494 if ((count - ret) > 0)
3495 h1c->flags |= H1C_F_CO_MSG_MORE;
3496
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003497 if (!ret)
3498 break;
3499 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003500 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003501 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003502 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003503 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003504
3505 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletdea24742021-01-22 15:12:30 +01003506 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003507 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 +01003508 }
3509
Christopher Faulet7a145d62020-08-05 11:31:16 +02003510 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003511 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003512 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003513}
3514
Willy Tarreaue5733232019-05-22 19:24:06 +02003515#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003516/* Send and get, using splicing */
3517static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3518{
3519 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003520 struct h1c *h1c = h1s->h1c;
3521 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003522 int ret = 0;
3523
Willy Tarreau022e5e52020-09-10 09:33:15 +02003524 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003525
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003526 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003527 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003528 TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulete18777b2019-04-16 16:46:36 +02003529 goto end;
3530 }
3531
Christopher Fauletcf307562021-07-26 10:49:39 +02003532 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003533 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003534 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003535 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003536 }
3537
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003538 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003539 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3540 goto end;
3541 }
3542
Christopher Faulet1be55f92018-10-02 15:59:23 +02003543 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3544 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003545 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003546 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003547 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003548 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003549 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003550 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003551 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003552 }
3553
Christopher Faulet1be55f92018-10-02 15:59:23 +02003554 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003555 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003556 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003557 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003558 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003559 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003560
Christopher Faulet94d35102021-04-09 11:58:49 +02003561 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003562 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003563 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet94d35102021-04-09 11:58:49 +02003564 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
3565 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
3566 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3567 }
Christopher Faulet27182292020-07-03 15:08:49 +02003568 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003569
Willy Tarreau022e5e52020-09-10 09:33:15 +02003570 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003571 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003572}
3573
3574static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3575{
3576 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003577 int ret = 0;
3578
Willy Tarreau022e5e52020-09-10 09:33:15 +02003579 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003580
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003581 if (b_data(&h1s->h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003582 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3583 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003584 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003585 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003586 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003587 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003588
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003589 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
3590
3591 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003592 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003593 return ret;
3594}
3595#endif
3596
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003597static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3598{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003599 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003600 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003601
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003602 switch (mux_ctl) {
3603 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003604 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003605 ret |= MUX_STATUS_READY;
3606 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003607 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003608 if (output)
3609 *((int *)output) = h1c->errcode;
3610 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3611 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3612 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3613 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003614 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003615 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003616 default:
3617 return -1;
3618 }
3619}
3620
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003621/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003622static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003623{
3624 struct h1c *h1c = conn->ctx;
3625 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003626 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003627
Christopher Fauletf376a312019-01-04 15:16:06 +01003628 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3629 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003630 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3631 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3632 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3633 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3634
3635 if (h1s) {
3636 char *method;
3637
3638 if (h1s->meth < HTTP_METH_OTHER)
3639 method = http_known_methods[h1s->meth].ptr;
3640 else
3641 method = "UNKNOWN";
3642 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3643 " .meth=%s status=%d",
3644 h1s, h1s->flags,
3645 h1m_state_str(h1s->req.state),
3646 h1m_state_str(h1s->res.state), method, h1s->status);
3647 if (h1s->cs)
3648 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3649 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003650
3651 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3652 if (h1s->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01003653 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3654 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3655 h1s->subs->tasklet->calls,
3656 h1s->subs->tasklet->context);
3657 if (h1s->subs->tasklet->calls >= 1000000)
3658 ret = 1;
3659 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3660 chunk_appendf(&trash, ")");
Willy Tarreau150c4f82021-01-20 17:05:58 +01003661 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003662 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003663 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003664}
3665
3666
3667/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3668static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3669{
3670 struct h1_hdr_entry *entry;
3671
3672 /* Be sure there is a non-empty <to> */
3673 if (!strlen(to)) {
3674 memprintf(err, "expect <to>");
3675 return -1;
3676 }
3677
3678 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003679 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003680 memprintf(err, "<from> and <to> must not differ execpt the case");
3681 return -1;
3682 }
3683
3684 /* Be sure <from> does not already existsin the tree */
3685 if (ebis_lookup(&hdrs_map.map, from)) {
3686 memprintf(err, "duplicate entry '%s'", from);
3687 return -1;
3688 }
3689
3690 /* Create the entry and insert it in the tree */
3691 entry = malloc(sizeof(*entry));
3692 if (!entry) {
3693 memprintf(err, "out of memory");
3694 return -1;
3695 }
3696
3697 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01003698 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003699 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003700 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003701 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003702 free(entry);
3703 memprintf(err, "out of memory");
3704 return -1;
3705 }
3706 ebis_insert(&hdrs_map.map, &entry->node);
3707 return 0;
3708}
3709
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003710/* Migrate the the connection to the current thread.
3711 * Return 0 if successful, non-zero otherwise.
3712 * Expected to be called with the old thread lock held.
3713 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003714static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003715{
3716 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003717 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003718
3719 if (fd_takeover(conn->handle.fd, conn) != 0)
3720 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003721
3722 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3723 /* We failed to takeover the xprt, even if the connection may
3724 * still be valid, flag it as error'd, as we have already
3725 * taken over the fd, and wake the tasklet, so that it will
3726 * destroy it.
3727 */
3728 conn->flags |= CO_FL_ERROR;
3729 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3730 return -1;
3731 }
3732
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003733 if (h1c->wait_event.events)
3734 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3735 h1c->wait_event.events, &h1c->wait_event);
3736 /* To let the tasklet know it should free itself, and do nothing else,
3737 * set its context to NULL.
3738 */
3739 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003740 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003741
3742 task = h1c->task;
3743 if (task) {
3744 task->context = NULL;
3745 h1c->task = NULL;
3746 __ha_barrier_store();
3747 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003748
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003749 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003750 if (!h1c->task) {
3751 h1_release(h1c);
3752 return -1;
3753 }
3754 h1c->task->process = h1_timeout_task;
3755 h1c->task->context = h1c;
3756 }
3757 h1c->wait_event.tasklet = tasklet_new();
3758 if (!h1c->wait_event.tasklet) {
3759 h1_release(h1c);
3760 return -1;
3761 }
3762 h1c->wait_event.tasklet->process = h1_io_cb;
3763 h1c->wait_event.tasklet->context = h1c;
3764 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3765 SUB_RETRY_RECV, &h1c->wait_event);
3766
3767 return 0;
3768}
3769
3770
Christopher Faulet98fbe952019-07-22 16:18:24 +02003771static void h1_hdeaders_case_adjust_deinit()
3772{
3773 struct ebpt_node *node, *next;
3774 struct h1_hdr_entry *entry;
3775
3776 node = ebpt_first(&hdrs_map.map);
3777 while (node) {
3778 next = ebpt_next(node);
3779 ebpt_delete(node);
3780 entry = container_of(node, struct h1_hdr_entry, node);
3781 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003782 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003783 free(entry);
3784 node = next;
3785 }
3786 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003787}
3788
Christopher Faulet98fbe952019-07-22 16:18:24 +02003789static int cfg_h1_headers_case_adjust_postparser()
3790{
3791 FILE *file = NULL;
3792 char *c, *key_beg, *key_end, *value_beg, *value_end;
3793 char *err;
3794 int rc, line = 0, err_code = 0;
3795
3796 if (!hdrs_map.name)
3797 goto end;
3798
3799 file = fopen(hdrs_map.name, "r");
3800 if (!file) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003801 ha_alert("h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02003802 hdrs_map.name);
3803 err_code |= ERR_ALERT | ERR_FATAL;
3804 goto end;
3805 }
3806
3807 /* now parse all lines. The file may contain only two header name per
3808 * line, separated by spaces. All heading and trailing spaces will be
3809 * ignored. Lines starting with a # are ignored.
3810 */
3811 while (fgets(trash.area, trash.size, file) != NULL) {
3812 line++;
3813 c = trash.area;
3814
3815 /* strip leading spaces and tabs */
3816 while (*c == ' ' || *c == '\t')
3817 c++;
3818
3819 /* ignore emptu lines, or lines beginning with a dash */
3820 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3821 continue;
3822
3823 /* look for the end of the key */
3824 key_beg = c;
3825 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3826 c++;
3827 key_end = c;
3828
3829 /* strip middle spaces and tabs */
3830 while (*c == ' ' || *c == '\t')
3831 c++;
3832
3833 /* look for the end of the value, it is the end of the line */
3834 value_beg = c;
3835 while (*c && *c != '\n' && *c != '\r')
3836 c++;
3837 value_end = c;
3838
3839 /* trim possibly trailing spaces and tabs */
3840 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3841 value_end--;
3842
3843 /* set final \0 and check entries */
3844 *key_end = '\0';
3845 *value_end = '\0';
3846
3847 err = NULL;
3848 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3849 if (rc < 0) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003850 ha_alert("h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02003851 hdrs_map.name, err, line);
3852 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003853 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003854 goto end;
3855 }
3856 if (rc > 0) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003857 ha_warning("h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02003858 hdrs_map.name, err, line);
3859 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003860 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003861 }
3862 }
3863
3864 end:
3865 if (file)
3866 fclose(file);
3867 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3868 return err_code;
3869}
3870
3871
3872/* config parser for global "h1-outgoing-header-case-adjust" */
3873static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01003874 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02003875 char **err)
3876{
3877 if (too_many_args(2, args, err, NULL))
3878 return -1;
3879 if (!*(args[1]) || !*(args[2])) {
3880 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3881 return -1;
3882 }
3883 return add_hdr_case_adjust(args[1], args[2], err);
3884}
3885
3886/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3887static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01003888 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02003889 char **err)
3890{
3891 if (too_many_args(1, args, err, NULL))
3892 return -1;
3893 if (!*(args[1])) {
3894 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3895 return -1;
3896 }
3897 free(hdrs_map.name);
3898 hdrs_map.name = strdup(args[1]);
3899 return 0;
3900}
3901
3902
3903/* config keyword parsers */
3904static struct cfg_kw_list cfg_kws = {{ }, {
3905 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3906 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3907 { 0, NULL, NULL },
3908 }
3909};
3910
3911INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3912REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3913
3914
Christopher Faulet51dbc942018-09-13 09:05:15 +02003915/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003916/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003917/****************************************/
3918
3919/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01003920static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003921 .init = h1_init,
3922 .wake = h1_wake,
3923 .attach = h1_attach,
3924 .get_first_cs = h1_get_first_cs,
3925 .detach = h1_detach,
3926 .destroy = h1_destroy,
3927 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003928 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003929 .rcv_buf = h1_rcv_buf,
3930 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003931#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003932 .rcv_pipe = h1_rcv_pipe,
3933 .snd_pipe = h1_snd_pipe,
3934#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003935 .subscribe = h1_subscribe,
3936 .unsubscribe = h1_unsubscribe,
3937 .shutr = h1_shutr,
3938 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003939 .show_fd = h1_show_fd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003940 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003941 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003942 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003943 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003944};
3945
Christopher Faulet3f612f72021-02-05 16:44:21 +01003946static const struct mux_ops mux_h1_ops = {
3947 .init = h1_init,
3948 .wake = h1_wake,
3949 .attach = h1_attach,
3950 .get_first_cs = h1_get_first_cs,
3951 .detach = h1_detach,
3952 .destroy = h1_destroy,
3953 .avail_streams = h1_avail_streams,
3954 .used_streams = h1_used_streams,
3955 .rcv_buf = h1_rcv_buf,
3956 .snd_buf = h1_snd_buf,
3957#if defined(USE_LINUX_SPLICE)
3958 .rcv_pipe = h1_rcv_pipe,
3959 .snd_pipe = h1_snd_pipe,
3960#endif
3961 .subscribe = h1_subscribe,
3962 .unsubscribe = h1_unsubscribe,
3963 .shutr = h1_shutr,
3964 .shutw = h1_shutw,
3965 .show_fd = h1_show_fd,
3966 .ctl = h1_ctl,
3967 .takeover = h1_takeover,
3968 .flags = MX_FL_HTX|MX_FL_NO_UPG,
3969 .name = "H1",
3970};
Christopher Faulet51dbc942018-09-13 09:05:15 +02003971
Christopher Faulet3f612f72021-02-05 16:44:21 +01003972/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
3973static struct mux_proto_list mux_proto_h1 =
3974 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
3975static struct mux_proto_list mux_proto_http =
3976 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003977
Christopher Faulet3f612f72021-02-05 16:44:21 +01003978INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
3979INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01003980
Christopher Faulet51dbc942018-09-13 09:05:15 +02003981/*
3982 * Local variables:
3983 * c-indent-level: 8
3984 * c-basic-offset: 8
3985 * End:
3986 */