blob: 332a3c347a273e8dbc5427086d535b909f557a51 [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>
13
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020016#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020017#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020018#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020019#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020021#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/istbuf.h>
23#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020024#include <haproxy/pipe-t.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020025#include <haproxy/proxy-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020026#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020029#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030
31/*
32 * H1 Connection flags (32 bits)
33 */
34#define H1C_F_NONE 0x00000000
35
36/* Flags indicating why writing output data are blocked */
37#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
38#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
39/* 0x00000004 - 0x00000008 unused */
40
41/* Flags indicating why reading input data are blocked. */
42#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
43#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletd17ad822020-09-24 15:14:29 +020044#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010045/* 0x00000080 unused */
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 Faulet39c7b6b2021-01-21 17:44:35 +010057/* 0x00004000 - 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020058
Christopher Fauletb385b502021-01-13 18:47:57 +010059#define H1C_F_WANT_SPLICE 0x00010000 /* Don't read into a buffer because we want to use or we are using splicing */
60#define H1C_F_ERR_PENDING 0x00020000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
61#define H1C_F_WAIT_NEXT_REQ 0x00040000 /* waiting for the next request to start, use keep-alive timeout */
62#define H1C_F_UPG_H2C 0x00080000 /* set if an upgrade to h2 should be done */
63#define H1C_F_CO_MSG_MORE 0x00100000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
64#define H1C_F_CO_STREAMER 0x00200000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
65#define H1C_F_WAIT_OUTPUT 0x00400000 /* Don't read more data for now, waiting sync with output side */
66#define H1C_F_WAIT_INPUT 0x00800000 /* Don't send more data for now, waiting sync with input side */
Christopher Faulet129817b2018-09-20 16:14:40 +020067
Christopher Fauletb385b502021-01-13 18:47:57 +010068/* 0x01000000 - 0x40000000 unusued*/
Christopher Faulet3ced1d12020-11-27 16:44:01 +010069#define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */
Christopher Faulet46230362019-10-17 16:04:20 +020070
Christopher Faulet51dbc942018-09-13 09:05:15 +020071/*
72 * H1 Stream flags (32 bits)
73 */
Christopher Faulet129817b2018-09-20 16:14:40 +020074#define H1S_F_NONE 0x00000000
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050075/* 0x00000001..0x00000004 unused */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020076#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020077#define H1S_F_WANT_KAL 0x00000010
78#define H1S_F_WANT_TUN 0x00000020
79#define H1S_F_WANT_CLO 0x00000040
80#define H1S_F_WANT_MSK 0x00000070
81#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020082
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010083/* 0x00000200 unsued */
Christopher Faulet2eed8002020-12-07 11:26:13 +010084#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 +020085#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
86#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
87#define H1S_F_ERROR 0x00001800 /* stream error mask */
88
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020089#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 +020090#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020091
92/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020093struct h1c {
94 struct connection *conn;
95 struct proxy *px;
96 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +020097 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098 struct buffer ibuf; /* Input buffer to store data before parsing */
99 struct buffer obuf; /* Output buffer to store data after reformatting */
100
101 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
102 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
103
104 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100105 struct task *task; /* timeout management task */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200106 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
107 int timeout; /* client/server timeout duration */
108 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200109};
110
111/* H1 stream descriptor */
112struct h1s {
113 struct h1c *h1c;
114 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100115 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200116
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100117 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200118
Olivier Houchardf502aca2018-12-14 19:42:40 +0100119 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200120 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200121 struct h1m req;
122 struct h1m res;
123
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500124 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200125 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200126};
127
Christopher Faulet98fbe952019-07-22 16:18:24 +0200128/* Map of headers used to convert outgoing headers */
129struct h1_hdrs_map {
130 char *name;
131 struct eb_root map;
132};
133
134/* An entry in a headers map */
135struct h1_hdr_entry {
136 struct ist name;
137 struct ebpt_node node;
138};
139
140/* Declare the headers map */
141static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
142
143
Christopher Faulet6b81df72019-10-01 22:08:43 +0200144/* trace source and events */
145static void h1_trace(enum trace_level level, uint64_t mask,
146 const struct trace_source *src,
147 const struct ist where, const struct ist func,
148 const void *a1, const void *a2, const void *a3, const void *a4);
149
150/* The event representation is split like this :
151 * h1c - internal H1 connection
152 * h1s - internal H1 stream
153 * strm - application layer
154 * rx - data receipt
155 * tx - data transmission
156 *
157 */
158static const struct trace_event h1_trace_events[] = {
159#define H1_EV_H1C_NEW (1ULL << 0)
160 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
161#define H1_EV_H1C_RECV (1ULL << 1)
162 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
163#define H1_EV_H1C_SEND (1ULL << 2)
164 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
165#define H1_EV_H1C_BLK (1ULL << 3)
166 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
167#define H1_EV_H1C_WAKE (1ULL << 4)
168 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
169#define H1_EV_H1C_END (1ULL << 5)
170 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
171#define H1_EV_H1C_ERR (1ULL << 6)
172 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
173
174#define H1_EV_RX_DATA (1ULL << 7)
175 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
176#define H1_EV_RX_EOI (1ULL << 8)
177 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
178#define H1_EV_RX_HDRS (1ULL << 9)
179 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
180#define H1_EV_RX_BODY (1ULL << 10)
181 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
182#define H1_EV_RX_TLRS (1ULL << 11)
183 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
184
185#define H1_EV_TX_DATA (1ULL << 12)
186 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
187#define H1_EV_TX_EOI (1ULL << 13)
188 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
189#define H1_EV_TX_HDRS (1ULL << 14)
190 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
191#define H1_EV_TX_BODY (1ULL << 15)
192 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
193#define H1_EV_TX_TLRS (1ULL << 16)
194 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
195
196#define H1_EV_H1S_NEW (1ULL << 17)
197 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
198#define H1_EV_H1S_BLK (1ULL << 18)
199 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
200#define H1_EV_H1S_END (1ULL << 19)
201 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
202#define H1_EV_H1S_ERR (1ULL << 20)
203 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
204
205#define H1_EV_STRM_NEW (1ULL << 21)
206 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
207#define H1_EV_STRM_RECV (1ULL << 22)
208 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
209#define H1_EV_STRM_SEND (1ULL << 23)
210 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
211#define H1_EV_STRM_WAKE (1ULL << 24)
212 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
213#define H1_EV_STRM_SHUT (1ULL << 25)
214 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
215#define H1_EV_STRM_END (1ULL << 26)
216 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
217#define H1_EV_STRM_ERR (1ULL << 27)
218 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
219
220 { }
221};
222
223static const struct name_desc h1_trace_lockon_args[4] = {
224 /* arg1 */ { /* already used by the connection */ },
225 /* arg2 */ { .name="h1s", .desc="H1 stream" },
226 /* arg3 */ { },
227 /* arg4 */ { }
228};
229
230static const struct name_desc h1_trace_decoding[] = {
231#define H1_VERB_CLEAN 1
232 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
233#define H1_VERB_MINIMAL 2
234 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
235#define H1_VERB_SIMPLE 3
236 { .name="simple", .desc="add request/response status line or htx info when available" },
237#define H1_VERB_ADVANCED 4
238 { .name="advanced", .desc="add header fields or frame decoding when available" },
239#define H1_VERB_COMPLETE 5
240 { .name="complete", .desc="add full data dump when available" },
241 { /* end */ }
242};
243
244static struct trace_source trace_h1 = {
245 .name = IST("h1"),
246 .desc = "HTTP/1 multiplexer",
247 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
248 .default_cb = h1_trace,
249 .known_events = h1_trace_events,
250 .lockon_args = h1_trace_lockon_args,
251 .decoding = h1_trace_decoding,
252 .report_events = ~0, // report everything by default
253};
254
255#define TRACE_SOURCE &trace_h1
256INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
257
Christopher Faulet51dbc942018-09-13 09:05:15 +0200258/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100259DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
260DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200261
Christopher Faulet51dbc942018-09-13 09:05:15 +0200262static int h1_recv(struct h1c *h1c);
263static int h1_send(struct h1c *h1c);
264static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100265/* h1_io_cb is exported to see it resolved in "show fd" */
266struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100267static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200268static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200269static void h1_wake_stream_for_recv(struct h1s *h1s);
270static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200271
Christopher Faulet6b81df72019-10-01 22:08:43 +0200272/* the H1 traces always expect that arg1, if non-null, is of type connection
273 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
274 * that arg3, if non-null, is a htx for rx/tx headers.
275 */
276static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
277 const struct ist where, const struct ist func,
278 const void *a1, const void *a2, const void *a3, const void *a4)
279{
280 const struct connection *conn = a1;
281 const struct h1c *h1c = conn ? conn->ctx : NULL;
282 const struct h1s *h1s = a2;
283 const struct htx *htx = a3;
284 const size_t *val = a4;
285
286 if (!h1c)
287 h1c = (h1s ? h1s->h1c : NULL);
288
289 if (!h1c || src->verbosity < H1_VERB_CLEAN)
290 return;
291
292 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200293 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200294
295 /* Display request and response states if h1s is defined */
296 if (h1s)
297 chunk_appendf(&trace_buf, " [%s, %s]",
298 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
299
300 if (src->verbosity == H1_VERB_CLEAN)
301 return;
302
303 /* Display the value to the 4th argument (level > STATE) */
304 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100305 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200306
307 /* Display status-line if possible (verbosity > MINIMAL) */
308 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
309 const struct htx_blk *blk = htx_get_head_blk(htx);
310 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
311 enum htx_blk_type type = htx_get_blk_type(blk);
312
313 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
314 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
315 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
316 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
317 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
318 }
319
320 /* Display h1c info and, if defined, h1s info (pointer + flags) */
321 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
322 if (h1s)
323 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
324
325 if (src->verbosity == H1_VERB_MINIMAL)
326 return;
327
328 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
329 if (src->level > TRACE_LEVEL_USER) {
330 if (src->verbosity == H1_VERB_COMPLETE ||
331 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
332 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
333 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
334 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
335 if (src->verbosity == H1_VERB_COMPLETE ||
336 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
337 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
338 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
339 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
340 }
341
342 /* Display htx info if defined (level > USER) */
343 if (src->level > TRACE_LEVEL_USER && htx) {
344 int full = 0;
345
346 /* Full htx info (level > STATE && verbosity > SIMPLE) */
347 if (src->level > TRACE_LEVEL_STATE) {
348 if (src->verbosity == H1_VERB_COMPLETE)
349 full = 1;
350 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
351 full = 1;
352 }
353
354 chunk_memcat(&trace_buf, "\n\t", 2);
355 htx_dump(&trace_buf, htx, full);
356 }
357}
358
359
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360/*****************************************************/
361/* functions below are for dynamic buffer management */
362/*****************************************************/
363/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100364 * Indicates whether or not we may receive data. The rules are the following :
365 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200366 * must not attempt to receive
367 * - if we are waiting for the connection establishment, we must not attempt
368 * to receive
369 * - if an error was detected on the stream we must not attempt to receive
370 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100371 * - if the input buffer failed to be allocated or is full , we must not try
372 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200373 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374 * - otherwise must may not attempt to receive
375 */
376static inline int h1_recv_allowed(const struct h1c *h1c)
377{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100378 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100379 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 +0200380 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200381 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200382
Willy Tarreau2febb842020-07-31 09:15:43 +0200383 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
384 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 +0100385 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200386 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100387
Christopher Faulet119ac872020-09-30 17:33:22 +0200388 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
389 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
390 return 0;
391 }
392
Christopher Fauletb385b502021-01-13 18:47:57 +0100393 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
394 TRACE_DEVEL("recv not allowed (wait_output)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200395 return 0;
396 }
397
Christopher Fauletd17ad822020-09-24 15:14:29 +0200398 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200399 return 1;
400
Christopher Faulet6b81df72019-10-01 22:08:43 +0200401 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 +0200402 return 0;
403}
404
405/*
406 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
407 * flags are used to figure what buffer was requested. It returns 1 if the
408 * allocation succeeds, in which case the connection is woken up, or 0 if it's
409 * impossible to wake up and we prefer to be woken up later.
410 */
411static int h1_buf_available(void *target)
412{
413 struct h1c *h1c = target;
414
415 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200416 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 +0200417 h1c->flags &= ~H1C_F_IN_ALLOC;
418 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200419 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420 return 1;
421 }
422
423 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200424 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 +0200425 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200426 if (h1c->h1s)
427 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200428 return 1;
429 }
430
Christopher Fauletd17ad822020-09-24 15:14:29 +0200431 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
432 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
433 h1c->flags &= ~H1C_F_IN_SALLOC;
434 tasklet_wakeup(h1c->wait_event.tasklet);
435 return 1;
436 }
437
Christopher Faulet51dbc942018-09-13 09:05:15 +0200438 return 0;
439}
440
441/*
442 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
443 */
444static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
445{
446 struct buffer *buf = NULL;
447
Willy Tarreau21046592020-02-26 10:39:36 +0100448 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
450 h1c->buf_wait.target = h1c;
451 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200452 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200453 }
454 return buf;
455}
456
457/*
458 * Release a buffer, if any, and try to wake up entities waiting in the buffer
459 * wait queue.
460 */
461static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
462{
463 if (bptr->size) {
464 b_free(bptr);
465 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
466 }
467}
468
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100469/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100470 * 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 +0100471 * not. This flag is only set when no H1S is attached and when the previous
472 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100473 */
474static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200475{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100476 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200477
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100478 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200479}
480
Willy Tarreau00f18a32019-01-26 12:19:01 +0100481/* returns the number of streams still available on a connection */
482static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100483{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100484 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100485}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200486
Christopher Faulet7a145d62020-08-05 11:31:16 +0200487/* Refresh the h1c task timeout if necessary */
488static void h1_refresh_timeout(struct h1c *h1c)
489{
490 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100491 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200492 /* half-closed or dead connections : switch to clientfin/serverfin
493 * timeouts so that we don't hang too long on clients that have
494 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200495 */
496 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200497 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
498 }
499 else if (b_data(&h1c->obuf)) {
500 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200501 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200502 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
503 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100504 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
505 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200506 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100507 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 +0200508 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200509 else {
510 /* alive back connections of front connections with a conn-stream attached */
511 h1c->task->expire = TICK_ETERNITY;
512 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
513 }
514
Christopher Fauletdbe57792020-10-05 17:50:58 +0200515 /* Finally set the idle expiration date if shorter */
516 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200517 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
518 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200519 }
520}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200521
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200522static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200523{
524 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
525 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
526 h1c->idle_exp = TICK_ETERNITY;
527 return;
528 }
529
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100530 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200531 if (!tick_isset(h1c->idle_exp)) {
532 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
533 !b_data(&h1c->ibuf) && /* No input data */
534 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
535 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
536 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
537 }
538 else {
539 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
540 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
541 }
542 }
543 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100544 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200545 if (!tick_isset(h1c->idle_exp)) {
546 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
547 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
548 }
549 }
550 else { // CS_ATTACHED or SHUTDOWN
551 h1c->idle_exp = TICK_ETERNITY;
552 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
553 }
554}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200555/*****************************************************************/
556/* functions below are dedicated to the mux setup and management */
557/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200558
559/* returns non-zero if there are input data pending for stream h1s. */
560static inline size_t h1s_data_pending(const struct h1s *h1s)
561{
562 const struct h1m *h1m;
563
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200564 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100565 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200566}
567
Christopher Faulet16df1782020-12-04 16:47:41 +0100568/* Creates a new conn-stream and the associate stream. <input> is used as input
569 * buffer for the stream. On success, it is transferred to the stream and the
570 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
571 * mux must still take care of it. However, there is nothing special to do
572 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
573 * b_free() on it is always safe. This function returns the conn-stream on
574 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200575static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100576{
577 struct conn_stream *cs;
578
Christopher Faulet6b81df72019-10-01 22:08:43 +0200579 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200580 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200581 if (!cs) {
582 TRACE_DEVEL("leaving on 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 +0100583 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200584 }
Christopher Faulet47365272018-10-31 17:40:50 +0100585 h1s->cs = cs;
586 cs->ctx = h1s;
587
588 if (h1s->flags & H1S_F_NOT_FIRST)
589 cs->flags |= CS_FL_NOT_FIRST;
590
Christopher Faulet27182292020-07-03 15:08:49 +0200591 if (global.tune.options & GTUNE_USE_SPLICE) {
592 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100593 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200594 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100595
Christopher Faulet26256f82020-09-14 11:40:13 +0200596 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200597 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 +0100598 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200599 }
600
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100601 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 +0200602 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100603 return cs;
604
605 err:
606 cs_free(cs);
607 h1s->cs = NULL;
608 return NULL;
609}
610
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100611static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
612{
613 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
614
615 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
616 TRACE_DEVEL("leaving on stream upgrade failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
617 goto err;
618 }
619
620 if (global.tune.options & GTUNE_USE_SPLICE) {
621 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
622 h1s->cs->flags |= CS_FL_MAY_SPLICE;
623 }
624
625 h1s->h1c->flags |= H1C_F_ST_READY;
626 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
627 return h1s->cs;
628
629 err:
630 return NULL;
631}
632
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200633static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200634{
635 struct h1s *h1s;
636
Christopher Faulet6b81df72019-10-01 22:08:43 +0200637 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
638
Christopher Faulet51dbc942018-09-13 09:05:15 +0200639 h1s = pool_alloc(pool_head_h1s);
640 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100641 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200642 h1s->h1c = h1c;
643 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200644 h1s->sess = NULL;
645 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100646 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100647 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200648 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200649
650 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100651 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200652
Christopher Faulet129817b2018-09-20 16:14:40 +0200653 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100654 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200655
656 h1s->status = 0;
657 h1s->meth = HTTP_METH_OTHER;
658
Christopher Faulet47365272018-10-31 17:40:50 +0100659 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
660 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100661 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100662
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200663 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
664 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200665
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200666 fail:
667 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
668 return NULL;
669}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100670
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200671static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
672{
673 struct session *sess = h1c->conn->owner;
674 struct h1s *h1s;
675
676 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
677
678 h1s = h1s_new(h1c);
679 if (!h1s)
680 goto fail;
681
682 h1s->sess = sess;
683
684 if (h1c->px->options2 & PR_O2_REQBUG_OK)
685 h1s->req.err_pos = -1;
686
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200687 h1c->idle_exp = TICK_ETERNITY;
688 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200689 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200690 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100691
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200692 fail:
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200693 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
694 return NULL;
695}
696
697static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
698{
699 struct h1s *h1s;
700
701 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
702
703 h1s = h1s_new(h1c);
704 if (!h1s)
705 goto fail;
706
707 h1s->cs = cs;
708 h1s->sess = sess;
709 cs->ctx = h1s;
710
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100711 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200712
713 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
714 h1s->res.err_pos = -1;
715
716 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
717 return h1s;
718
719 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200720 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100721 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200722}
723
724static void h1s_destroy(struct h1s *h1s)
725{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200726 if (h1s) {
727 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200728
Christopher Faulet6b81df72019-10-01 22:08:43 +0200729 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200730 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200731
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100732 if (h1s->subs)
733 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200734
Christopher Fauletd17ad822020-09-24 15:14:29 +0200735 h1_release_buf(h1c, &h1s->rxbuf);
736
Christopher Fauletb385b502021-01-13 18:47:57 +0100737 h1c->flags &= ~(H1C_F_WAIT_INPUT|H1C_F_WAIT_OUTPUT|H1C_F_WANT_SPLICE|
738 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200739 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
740 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200741 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100742 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200743 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
744 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100745
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100746 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100747 !(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 +0100748 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100749 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100750 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100751 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
752 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200753 else {
754 TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100755 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200756 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200757 pool_free(pool_head_h1s, h1s);
758 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200759}
760
761/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200762 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500763 * to the existing conn_stream (for outgoing connections or for incoming ones
764 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200765 * establishment). <input> is always used as Input buffer and may contain
766 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
767 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200768 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200769static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
770 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200771{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200772 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100773 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200774 void *conn_ctx = conn->ctx;
775
776 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200777
778 h1c = pool_alloc(pool_head_h1c);
779 if (!h1c)
780 goto fail_h1c;
781 h1c->conn = conn;
782 h1c->px = proxy;
783
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100784 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200785 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200786 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200787 h1c->obuf = BUF_NULL;
788 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200789 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200790
Willy Tarreau21046592020-02-26 10:39:36 +0100791 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200792 h1c->wait_event.tasklet = tasklet_new();
793 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200794 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200795 h1c->wait_event.tasklet->process = h1_io_cb;
796 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100797 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200798 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200799
Christopher Faulete9b70722019-04-08 10:46:02 +0200800 if (conn_is_back(conn)) {
Christopher Fauletb385b502021-01-13 18:47:57 +0100801 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OUTPUT);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100802 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
803 if (tick_isset(proxy->timeout.serverfin))
804 h1c->shut_timeout = proxy->timeout.serverfin;
805 } else {
806 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
807 if (tick_isset(proxy->timeout.clientfin))
808 h1c->shut_timeout = proxy->timeout.clientfin;
809 }
810 if (tick_isset(h1c->timeout)) {
811 t = task_new(tid_bit);
812 if (!t)
813 goto fail;
814
815 h1c->task = t;
816 t->process = h1_timeout_task;
817 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200818
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100819 t->expire = tick_add(now_ms, h1c->timeout);
820 }
821
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100822 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200823
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200824 if (h1c->flags & H1C_F_IS_BACK) {
825 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200826 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
827 goto fail;
828 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100829 else if (conn_ctx) {
830 /* Upgraded frontend connection (from TCP) */
831 struct conn_stream *cs = conn_ctx;
832
833 if (!h1c_frt_stream_new(h1c))
834 goto fail;
835
836 h1c->h1s->cs = cs;
837 cs->ctx = h1c->h1s;
838
839 /* Attach the CS but Not ready yet */
840 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
841 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
842 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
843 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100844
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200845 if (t) {
846 h1_set_idle_expiration(h1c);
847 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100848 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200849 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100850
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200851 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100852 if (b_data(&h1c->ibuf))
853 tasklet_wakeup(h1c->wait_event.tasklet);
854 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200855 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200856
857 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200858 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200859 return 0;
860
861 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200862 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200863 if (h1c->wait_event.tasklet)
864 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200865 pool_free(pool_head_h1c, h1c);
866 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200867 conn->ctx = conn_ctx; // restore saved context
868 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200869 return -1;
870}
871
Christopher Faulet73c12072019-04-08 11:23:22 +0200872/* release function. This one should be called to free all resources allocated
873 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200874 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200875static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200876{
Christopher Faulet61840e72019-04-15 09:33:32 +0200877 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200878
Christopher Faulet6b81df72019-10-01 22:08:43 +0200879 TRACE_POINT(H1_EV_H1C_END);
880
Christopher Faulet51dbc942018-09-13 09:05:15 +0200881 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200882 /* The connection must be aattached to this mux to be released */
883 if (h1c->conn && h1c->conn->ctx == h1c)
884 conn = h1c->conn;
885
Christopher Faulet6b81df72019-10-01 22:08:43 +0200886 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
887
Christopher Faulet61840e72019-04-15 09:33:32 +0200888 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200889 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200890 /* Make sure we're no longer subscribed to anything */
891 if (h1c->wait_event.events)
892 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
893 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200894 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200895 /* connection successfully upgraded to H2, this
896 * mux was already released */
897 return;
898 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200899 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200900 sess_log(conn->owner); /* Log if the upgrade failed */
901 }
902
Olivier Houchard45c44372019-06-11 14:06:23 +0200903
Willy Tarreau21046592020-02-26 10:39:36 +0100904 if (MT_LIST_ADDED(&h1c->buf_wait.list))
905 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200906
907 h1_release_buf(h1c, &h1c->ibuf);
908 h1_release_buf(h1c, &h1c->obuf);
909
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100910 if (h1c->task) {
911 h1c->task->context = NULL;
912 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
913 h1c->task = NULL;
914 }
915
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200916 if (h1c->wait_event.tasklet)
917 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200918
Christopher Fauletf2824e62018-10-01 12:12:37 +0200919 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200920 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100921 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200922 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200923 pool_free(pool_head_h1c, h1c);
924 }
925
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200926 if (conn) {
927 conn->mux = NULL;
928 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200929 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200930
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200931 conn_stop_tracking(conn);
932 conn_full_close(conn);
933 if (conn->destroy_cb)
934 conn->destroy_cb(conn);
935 conn_free(conn);
936 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200937}
938
939/******************************************************/
940/* functions below are for the H1 protocol processing */
941/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200942/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
943 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200944 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100945static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200946{
Christopher Faulet570d1612018-11-26 11:13:57 +0100947 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200948
Christopher Faulet570d1612018-11-26 11:13:57 +0100949 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200950 (*(p + 5) > '1' ||
951 (*(p + 5) == '1' && *(p + 7) >= '1')))
952 h1m->flags |= H1_MF_VER_11;
953}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200954
Christopher Faulet9768c262018-10-22 09:34:31 +0200955/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
956 * greater or equal to 1.1
957 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100958static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200959{
Christopher Faulet570d1612018-11-26 11:13:57 +0100960 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200961
Christopher Faulet570d1612018-11-26 11:13:57 +0100962 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200963 (*(p + 5) > '1' ||
964 (*(p + 5) == '1' && *(p + 7) >= '1')))
965 h1m->flags |= H1_MF_VER_11;
966}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200967
Christopher Fauletf2824e62018-10-01 12:12:37 +0200968/* Deduce the connection mode of the client connection, depending on the
969 * configuration and the H1 message flags. This function is called twice, the
970 * first time when the request is parsed and the second time when the response
971 * is parsed.
972 */
973static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
974{
975 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200976
977 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100978 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100979 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100980 h1s->status == 101) {
981 /* Either we've established an explicit tunnel, or we're
982 * switching the protocol. In both cases, we're very unlikely to
983 * understand the next protocols. We have to switch to tunnel
984 * mode, so that we transfer the request and responses then let
985 * this protocol pass unmodified. When we later implement
986 * specific parsers for such protocols, we'll want to check the
987 * Upgrade header which contains information about that protocol
988 * for responses with status 101 (eg: see RFC2817 about TLS).
989 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200990 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200991 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 +0100992 }
993 else if (h1s->flags & H1S_F_WANT_KAL) {
994 /* By default the client is in KAL mode. CLOSE mode mean
995 * it is imposed by the client itself. So only change
996 * KAL mode here. */
997 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
998 /* no length known or explicit close => close */
999 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001000 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 +01001001 }
1002 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1003 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001004 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001005 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001006 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 +01001007 }
1008 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001009 }
1010 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001011 /* Input direction: first pass */
1012 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1013 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001014 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001015 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 +01001016 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001017 }
1018
1019 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001020 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001021 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001022 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);
1023 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001024}
1025
1026/* Deduce the connection mode of the client connection, depending on the
1027 * configuration and the H1 message flags. This function is called twice, the
1028 * first time when the request is parsed and the second time when the response
1029 * is parsed.
1030 */
1031static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1032{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001033 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001034 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001035 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001036
Christopher Fauletf2824e62018-10-01 12:12:37 +02001037 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001038 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001039 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001040 h1s->status == 101) {
1041 /* Either we've established an explicit tunnel, or we're
1042 * switching the protocol. In both cases, we're very unlikely to
1043 * understand the next protocols. We have to switch to tunnel
1044 * mode, so that we transfer the request and responses then let
1045 * this protocol pass unmodified. When we later implement
1046 * specific parsers for such protocols, we'll want to check the
1047 * Upgrade header which contains information about that protocol
1048 * for responses with status 101 (eg: see RFC2817 about TLS).
1049 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001050 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001051 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 +01001052 }
1053 else if (h1s->flags & H1S_F_WANT_KAL) {
1054 /* By default the server is in KAL mode. CLOSE mode mean
1055 * it is imposed by haproxy itself. So only change KAL
1056 * mode here. */
1057 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1058 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1059 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1060 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001061 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 +01001062 }
1063 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001064 }
Christopher Faulet70033782018-12-05 13:50:11 +01001065 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001066 /* Output direction: first pass */
1067 if (h1m->flags & H1_MF_CONN_CLO) {
1068 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001069 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001070 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 +01001071 }
1072 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1073 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1074 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1075 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1076 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1077 /* no explicit keep-alive option httpclose/server-close => close */
1078 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001079 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 +01001080 }
Christopher Faulet70033782018-12-05 13:50:11 +01001081 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001082
1083 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001084 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001085 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001086 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);
1087 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001088}
1089
Christopher Fauletb992af02019-03-28 15:42:24 +01001090static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001091{
1092 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001093
1094 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1095 * token is found
1096 */
1097 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001098 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001099
1100 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001101 if (!(h1m->flags & H1_MF_VER_11)) {
1102 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 +01001103 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001104 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001105 }
1106 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001107 if (h1m->flags & H1_MF_VER_11) {
1108 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001109 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001110 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001111 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001112}
1113
Christopher Fauletb992af02019-03-28 15:42:24 +01001114static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001115{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1117 * token is found
1118 */
1119 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001120 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001121
1122 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001123 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001124 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1125 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 +01001126 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001127 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001128 }
1129 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001130 if (h1m->flags & H1_MF_VER_11) {
1131 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001132 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001133 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001134 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001135}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001136
Christopher Fauletb992af02019-03-28 15:42:24 +01001137static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001138{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001139 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001140 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001141 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001142 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001143}
1144
Christopher Fauletb992af02019-03-28 15:42:24 +01001145static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1146{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001147 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001148 h1_set_cli_conn_mode(h1s, h1m);
1149 else
1150 h1_set_srv_conn_mode(h1s, h1m);
1151
1152 if (!(h1m->flags & H1_MF_RESP))
1153 h1_update_req_conn_value(h1s, h1m, conn_val);
1154 else
1155 h1_update_res_conn_value(h1s, h1m, conn_val);
1156}
Christopher Faulete44769b2018-11-29 23:01:45 +01001157
Christopher Faulet98fbe952019-07-22 16:18:24 +02001158/* Try to adjust the case of the message header name using the global map
1159 * <hdrs_map>.
1160 */
1161static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1162{
1163 struct ebpt_node *node;
1164 struct h1_hdr_entry *entry;
1165
1166 /* No entry in the map, do nothing */
1167 if (eb_is_empty(&hdrs_map.map))
1168 return;
1169
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001170 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001171 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1172 return;
1173
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001174 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001175 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1176 return;
1177
1178 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1179 if (!node)
1180 return;
1181 entry = container_of(node, struct h1_hdr_entry, node);
1182 name->ptr = entry->name.ptr;
1183 name->len = entry->name.len;
1184}
1185
Christopher Faulete44769b2018-11-29 23:01:45 +01001186/* Append the description of what is present in error snapshot <es> into <out>.
1187 * The description must be small enough to always fit in a buffer. The output
1188 * buffer may be the trash so the trash must not be used inside this function.
1189 */
1190static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1191{
1192 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001193 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1194 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1195 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1196 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1197 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1198 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001199}
1200/*
1201 * Capture a bad request or response and archive it in the proxy's structure.
1202 * By default it tries to report the error position as h1m->err_pos. However if
1203 * this one is not set, it will then report h1m->next, which is the last known
1204 * parsing point. The function is able to deal with wrapping buffers. It always
1205 * displays buffers as a contiguous area starting at buf->p. The direction is
1206 * determined thanks to the h1m's flags.
1207 */
1208static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1209 struct h1m *h1m, struct buffer *buf)
1210{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001211 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001212 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001213 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001214 union error_snapshot_ctx ctx;
1215
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001216 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001217 if (sess == NULL)
1218 sess = si_strm(h1s->cs->data)->sess;
1219 if (!(h1m->flags & H1_MF_RESP))
1220 other_end = si_strm(h1s->cs->data)->be;
1221 else
1222 other_end = sess->fe;
1223 } else
1224 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001225
1226 /* http-specific part now */
1227 ctx.h1.state = h1m->state;
1228 ctx.h1.c_flags = h1c->flags;
1229 ctx.h1.s_flags = h1s->flags;
1230 ctx.h1.m_flags = h1m->flags;
1231 ctx.h1.m_clen = h1m->curr_len;
1232 ctx.h1.m_blen = h1m->body_len;
1233
1234 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1235 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001236 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1237 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001238}
1239
Christopher Fauletadb22202018-12-12 10:32:09 +01001240/* Emit the chunksize followed by a CRLF in front of data of the buffer
1241 * <buf>. It goes backwards and starts with the byte before the buffer's
1242 * head. The caller is responsible for ensuring there is enough room left before
1243 * the buffer's head for the string.
1244 */
1245static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1246{
1247 char *beg, *end;
1248
1249 beg = end = b_head(buf);
1250 *--beg = '\n';
1251 *--beg = '\r';
1252 do {
1253 *--beg = hextab[chksz & 0xF];
1254 } while (chksz >>= 4);
1255 buf->head -= (end - beg);
1256 b_add(buf, end - beg);
1257}
1258
1259/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1260 * ensuring there is enough room left in the buffer for the string. */
1261static void h1_emit_chunk_crlf(struct buffer *buf)
1262{
1263 *(b_peek(buf, b_data(buf))) = '\r';
1264 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1265 b_add(buf, 2);
1266}
1267
Christopher Faulet129817b2018-09-20 16:14:40 +02001268/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001269 * Switch the stream to tunnel mode. This function must only be called on 2xx
1270 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001271 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001272static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001273{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001274 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001275
Christopher Faulet5be651d2021-01-22 15:28:03 +01001276 h1s->req.state = H1_MSG_TUNNEL;
1277 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001278
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001279 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001280 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001281
Christopher Faulet5be651d2021-01-22 15:28:03 +01001282 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 +01001283
Christopher Faulet5be651d2021-01-22 15:28:03 +01001284 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1285 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1286 if (b_data(&h1c->ibuf))
1287 h1_wake_stream_for_recv(h1s);
1288 tasklet_wakeup(h1c->wait_event.tasklet);
1289 TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001290 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01001291 if (h1c->flags & H1C_F_WAIT_INPUT) {
1292 h1c->flags &= ~H1C_F_WAIT_INPUT;
1293 h1_wake_stream_for_send(h1s);
1294 if (b_data(&h1c->obuf))
1295 tasklet_wakeup(h1c->wait_event.tasklet);
1296 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001297 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001298}
1299
1300/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001301 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001302 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001303 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001304 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001305static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001306 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001307{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001308 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001309 int ret = 0;
1310
Willy Tarreau022e5e52020-09-10 09:33:15 +02001311 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 +02001312
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001313 if (h1s->meth == HTTP_METH_CONNECT)
1314 h1m->flags |= H1_MF_METH_CONNECT;
1315 if (h1s->meth == HTTP_METH_HEAD)
1316 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001317
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001318 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1319 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001320 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001321 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001322 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001323 TRACE_USER("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 +02001324 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1325 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001326 goto end;
1327 }
1328
Christopher Faulet486498c2019-10-11 14:22:00 +02001329 if (h1m->err_pos >= 0) {
1330 /* Maybe we found an error during the parsing while we were
1331 * configured not to block on that, so we have to capture it
1332 * now.
1333 */
1334 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1335 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1336 }
1337
Christopher Faulet5be651d2021-01-22 15:28:03 +01001338 if (!(h1m->flags & H1_MF_RESP))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001339 h1s->meth = h1sl.rq.meth;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001340 else
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001341 h1s->status = h1sl.st.status;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001342
Christopher Fauletb992af02019-03-28 15:42:24 +01001343 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001344 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001345
Christopher Faulet129817b2018-09-20 16:14:40 +02001346 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001347 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 +02001348 return ret;
1349}
1350
1351/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001352 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001353 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1354 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001355 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001356static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001357 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001358 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001359{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001360 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001361
Willy Tarreau022e5e52020-09-10 09:33:15 +02001362 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 +02001363 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001364 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001365 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 +01001366 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001367 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001368 TRACE_USER("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 +02001369 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001370 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001371 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001372 }
1373
Christopher Faulet02a02532019-11-15 09:36:28 +01001374 *ofs += ret;
1375
1376 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001377 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 +02001378 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001379}
1380
1381/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001382 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1383 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1384 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1385 * responsible to update the parser state <h1m>.
1386 */
1387static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1388 struct buffer *buf, size_t *ofs, size_t max)
1389{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001390 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001391
Willy Tarreau022e5e52020-09-10 09:33:15 +02001392 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 +02001393 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001394 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001395 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 +01001396 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001397 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001398 TRACE_USER("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 +02001399 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1400 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001401 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001402 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001403
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001404 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001405
1406 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001407 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 +02001408 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001409}
1410
1411/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001412 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001413 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1414 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001415 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001416static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001417{
Christopher Faulet539e0292018-11-19 10:40:09 +01001418 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001419 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001420 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001421 size_t data;
1422 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001423 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001424
Christopher Faulet539e0292018-11-19 10:40:09 +01001425 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001426 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001427
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001428 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001429 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001430
Christopher Faulet2eed8002020-12-07 11:26:13 +01001431 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001432 goto end;
1433
Christopher Faulet5be651d2021-01-22 15:28:03 +01001434 if (h1c->flags & H1C_F_WAIT_OUTPUT)
1435 goto end;
1436
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001437 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001438 size_t used = htx_used_space(htx);
1439
Christopher Faulet129817b2018-09-20 16:14:40 +02001440 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001441 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001442 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001443 if (!ret)
1444 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001445
1446 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1447 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1448
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001449 /* Reject Protocol upgrade request with payload */
1450 if ((h1m->flags & (H1_MF_RESP|H1_MF_CONN_UPG)) == H1_MF_CONN_UPG && h1m->state != H1_MSG_DONE) {
1451 h1s->flags |= H1S_F_NOT_IMPL_ERROR;
1452 TRACE_USER("Upgrade with body not implemented, reject H1 message",
1453 H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1454 break;
1455 }
1456
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001457 if ((h1m->flags & H1_MF_RESP) &&
1458 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1459 h1m_init_res(&h1s->res);
1460 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001461 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001462 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001463 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001464 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001465 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001466 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001467 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001468 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001469
1470 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1471 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001472 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001473 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001474 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1475 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1476 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001477 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001478
Christopher Faulet76014fd2019-12-10 11:47:22 +01001479 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1480 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001481 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001482 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001483 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1484 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001485
Christopher Faulet5be651d2021-01-22 15:28:03 +01001486 if ((h1m->flags & H1_MF_RESP) &&
1487 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1488 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001489 else {
1490 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1491 /* Unfinished transaction: block this input side waiting the end of the output side */
1492 h1c->flags |= H1C_F_WAIT_OUTPUT;
1493 TRACE_STATE("Disable read on h1c (wait_output)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1494 }
1495 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1496 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1497 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1498 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1499 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001500 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001501 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001502 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001503 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001504 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001505 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001506 if (!ret)
1507 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001508
1509 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1510 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001511 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001512 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001513 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001514 break;
1515 }
1516
Christopher Faulet30db3d72019-05-17 15:35:33 +02001517 count -= htx_used_space(htx) - used;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001518 } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) && !(h1c->flags & H1C_F_WAIT_OUTPUT));
1519
Christopher Faulet129817b2018-09-20 16:14:40 +02001520
Christopher Faulet2eed8002020-12-07 11:26:13 +01001521 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
1522 TRACE_PROTO("parsing or not-implemented error", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001523 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001524 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001525
Christopher Faulet539e0292018-11-19 10:40:09 +01001526 b_del(&h1c->ibuf, total);
1527
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001528 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001529 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1530
Christopher Faulet30db3d72019-05-17 15:35:33 +02001531 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001532 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001533 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001534 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 +01001535 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001536 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001537
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001538 if (!b_data(&h1c->ibuf))
1539 h1_release_buf(h1c, &h1c->ibuf);
1540
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001541 if (!(h1c->flags & H1C_F_ST_READY)) {
1542 /* The H1 connection is not ready. Most of time, there is no CS
1543 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1544 * cases, it is only possible on the client side.
1545 */
1546 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1547
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001548 if (h1m->state <= H1_MSG_LAST_LF) {
1549 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1550 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1551 goto end;
1552 }
1553
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001554 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1555 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1556 BUG_ON(h1s->cs);
1557 if (!h1s_new_cs(h1s, buf)) {
1558 h1c->flags |= H1C_F_ST_ERROR;
1559 goto err;
1560 }
1561 }
1562 else {
1563 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1564 BUG_ON(h1s->cs == NULL);
1565 if (!h1s_upgrade_cs(h1s, buf)) {
1566 h1c->flags |= H1C_F_ST_ERROR;
1567 goto err;
1568 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001569 }
1570 }
1571
1572 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001573 if (!(h1m->flags & H1_MF_CHNK) &&
1574 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1575 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1576 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1577 }
1578 else {
1579 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1580 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1581 }
1582
Christopher Faulet5be651d2021-01-22 15:28:03 +01001583 /* Don't set EOI on the conn-stream for protocol upgrade or connect
1584 * requests, wait the response to do so or not depending on the status
1585 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001586 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001587 if ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG))
Christopher Fauleta583af62020-09-24 15:35:37 +02001588 h1s->cs->flags |= CS_FL_EOI;
1589
Christopher Faulet6716cc22019-12-20 17:33:24 +01001590 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001591 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001592 else {
1593 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1594 if (h1s->flags & H1S_F_REOS) {
1595 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001596 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1597 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1598 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001599 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001600 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001601 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
1602 h1s->cs->flags |= CS_FL_ERROR;
Christopher Fauletb385b502021-01-13 18:47:57 +01001603
1604 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1605 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1606 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1607 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1608 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001609 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001610 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001611
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001612 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001613 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001614 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001615
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001616 err:
Christopher Faulet47365272018-10-31 17:40:50 +01001617 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001618 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001619 if (h1s->cs)
1620 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001621 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001622 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001623}
1624
Christopher Faulet129817b2018-09-20 16:14:40 +02001625/*
1626 * Process outgoing data. It parses data and transfer them from the channel buffer into
1627 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1628 * 0 if it couldn't proceed.
1629 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001630static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1631{
Christopher Faulet129817b2018-09-20 16:14:40 +02001632 struct h1s *h1s = h1c->h1s;
1633 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001634 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001635 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001636 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001637 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001638 int last_data = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001639
Christopher Faulet47365272018-10-31 17:40:50 +01001640 if (!count)
1641 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001642
Christopher Faulet94b2c762019-05-24 15:28:57 +02001643 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001644 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1645
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001646 if (htx_is_empty(chn_htx))
1647 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001648
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001649 if (h1s->flags & H1S_F_PROCESSING_ERROR)
1650 goto end;
1651
Christopher Fauletdea24742021-01-22 15:12:30 +01001652 if (h1c->flags & H1C_F_WAIT_INPUT)
1653 goto end;
1654
Christopher Faulet51dbc942018-09-13 09:05:15 +02001655 if (!h1_get_buf(h1c, &h1c->obuf)) {
1656 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001657 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 +02001658 goto end;
1659 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001660
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001661 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001662
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001663 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001664 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001665
Willy Tarreau3815b222018-12-11 19:50:43 +01001666 /* Perform some optimizations to reduce the number of buffer copies.
1667 * First, if the mux's buffer is empty and the htx area contains
1668 * exactly one data block of the same size as the requested count,
1669 * then it's possible to simply swap the caller's buffer with the
1670 * mux's output buffer and adjust offsets and length to match the
1671 * entire DATA HTX block in the middle. In this case we perform a
1672 * true zero-copy operation from end-to-end. This is the situation
1673 * that happens all the time with large files. Second, if this is not
1674 * possible, but the mux's output buffer is empty, we still have an
1675 * opportunity to avoid the copy to the intermediary buffer, by making
1676 * the intermediary buffer's area point to the output buffer's area.
1677 * In this case we want to skip the HTX header to make sure that copies
1678 * remain aligned and that this operation remains possible all the
1679 * time. This goes for headers, data blocks and any data extracted from
1680 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001681 */
1682 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001683 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
1684 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001685 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001686 htx_get_blk_value(chn_htx, blk).len == count) {
1687 void *old_area = h1c->obuf.area;
1688
Christopher Faulet6b81df72019-10-01 22:08:43 +02001689 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 +01001690 if (h1m->state == H1_MSG_DATA && chn_htx->flags & HTX_FL_EOM) {
1691 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1692 last_data = 1;
1693 }
1694
Willy Tarreau3815b222018-12-11 19:50:43 +01001695 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001696 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001697 h1c->obuf.data = count;
1698
1699 buf->area = old_area;
1700 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001701
Christopher Faulet6b81df72019-10-01 22:08:43 +02001702 chn_htx = (struct htx *)buf->area;
1703 htx_reset(chn_htx);
1704
Christopher Fauletadb22202018-12-12 10:32:09 +01001705 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001706 * size and eventually the last chunk. We have at least
1707 * the size of the struct htx to write the chunk
1708 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01001709 */
1710 if (h1m->flags & H1_MF_CHNK) {
1711 h1_emit_chunk_size(&h1c->obuf, count);
1712 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001713 if (last_data) {
1714 /* Emit the last chunk too at the buffer's end */
1715 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
1716 }
Christopher Fauletadb22202018-12-12 10:32:09 +01001717 }
1718
Willy Tarreau3815b222018-12-11 19:50:43 +01001719 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001720 if (h1m->state == H1_MSG_DATA)
1721 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001722 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001723 else
1724 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001725 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001726
1727 if (last_data) {
1728 h1m->state = H1_MSG_DONE;
1729 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
1730 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1731 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1732 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1733 }
1734
1735 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1736 H1_EV_TX_DATA, h1c->conn, h1s);
1737 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001738 goto out;
1739 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001740 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001741 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001742 else
1743 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001744
Christopher Fauletc2518a52019-06-25 21:41:02 +02001745 tmp.data = 0;
1746 tmp.size = b_room(&h1c->obuf);
Christopher Fauletdea24742021-01-22 15:12:30 +01001747 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && !(h1c->flags & H1C_F_WAIT_INPUT) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001748 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001749 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001750 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001751 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001752 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001753
Christopher Fauletb2e84162018-12-06 11:39:49 +01001754 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001755 if (type != HTX_BLK_DATA && vlen > count)
1756 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001757
Christopher Faulet94b2c762019-05-24 15:28:57 +02001758 if (type == HTX_BLK_UNUSED)
1759 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001760
Christopher Faulet94b2c762019-05-24 15:28:57 +02001761 switch (h1m->state) {
1762 case H1_MSG_RQBEFORE:
1763 if (type != HTX_BLK_REQ_SL)
1764 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001765 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 +02001766 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001767 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001768 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001769 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001770 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001771 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001772 if (sl->flags & HTX_SL_F_BODYLESS)
1773 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001774 h1m->state = H1_MSG_HDR_FIRST;
Christopher Fauletb385b502021-01-13 18:47:57 +01001775 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1776 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Christopher Faulet089acd52020-09-21 10:57:52 +02001777 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1778 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1779 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001780 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001781
Christopher Faulet94b2c762019-05-24 15:28:57 +02001782 case H1_MSG_RPBEFORE:
1783 if (type != HTX_BLK_RES_SL)
1784 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001785 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 +02001786 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001787 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001788 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001789 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001790 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001791 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001792 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001793 if (sl->info.res.status < 200 &&
1794 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001795 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001796 h1m->state = H1_MSG_HDR_FIRST;
1797 break;
1798
Christopher Faulet94b2c762019-05-24 15:28:57 +02001799 case H1_MSG_HDR_FIRST:
1800 case H1_MSG_HDR_NAME:
1801 case H1_MSG_HDR_L2_LWS:
1802 if (type == HTX_BLK_EOH)
1803 goto last_lf;
1804 if (type != HTX_BLK_HDR)
1805 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001806
Christopher Faulet9768c262018-10-22 09:34:31 +02001807 h1m->state = H1_MSG_HDR_NAME;
1808 n = htx_get_blk_name(chn_htx, blk);
1809 v = htx_get_blk_value(chn_htx, blk);
1810
Christopher Faulet86d144c2019-08-14 16:32:25 +02001811 /* Skip all pseudo-headers */
1812 if (*(n.ptr) == ':')
1813 goto skip_hdr;
1814
Christopher Fauletb045bb22020-02-28 10:42:20 +01001815 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001816 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001817 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001818 /* Only skip C-L header with invalid value. */
1819 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001820 goto skip_hdr;
1821 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001822 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001823 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001824 if (!v.len)
1825 goto skip_hdr;
1826 }
1827
Christopher Faulet67d58092019-10-02 10:51:38 +02001828 /* Skip header if same name is used to add the server name */
1829 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1830 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1831 goto skip_hdr;
1832
Christopher Faulet98fbe952019-07-22 16:18:24 +02001833 /* Try to adjust the case of the header name */
1834 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1835 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001836 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001837 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001838 skip_hdr:
1839 h1m->state = H1_MSG_HDR_L2_LWS;
1840 break;
1841
Christopher Faulet94b2c762019-05-24 15:28:57 +02001842 case H1_MSG_LAST_LF:
1843 if (type != HTX_BLK_EOH)
1844 goto error;
1845 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001846 h1m->state = H1_MSG_LAST_LF;
1847 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001848 /* If the reply comes from haproxy while the request is
1849 * not finished, we force the connection close. */
1850 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1851 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1852 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1853 }
1854
1855 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001856 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001857 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001858 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001859 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001860 /* Try to adjust the case of the header name */
1861 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1862 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001863 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001864 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001865 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001866 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001867 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001868
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001869 if ((h1s->meth != HTTP_METH_CONNECT &&
1870 (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 +01001871 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Fauletc75668e2020-12-07 18:10:32 +01001872 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && h1s->meth != HTTP_METH_HEAD &&
1873 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001874 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1875 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001876 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001877 n = ist("transfer-encoding");
1878 v = ist("chunked");
1879 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1880 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001881 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001882 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001883 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001884 h1m->flags |= H1_MF_CHNK;
1885 }
1886
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001887 /* Now add the server name to a header (if requested) */
1888 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1889 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1890 struct server *srv = objt_server(h1c->conn->target);
1891
1892 if (srv) {
1893 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1894 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001895
1896 /* Try to adjust the case of the header name */
1897 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1898 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001899 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001900 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001901 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001902 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001903 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1904 }
1905
Christopher Faulet6b81df72019-10-01 22:08:43 +02001906 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1907 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1908
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001909 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001910 if (!chunk_memcat(&tmp, "\r\n", 2))
1911 goto full;
1912 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001913 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001914 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001915 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001916 if (!chunk_memcat(&tmp, "\r\n", 2))
1917 goto full;
1918 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001919 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001920 else if ((h1m->flags & H1_MF_RESP) &&
1921 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001922 if (!chunk_memcat(&tmp, "\r\n", 2))
1923 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001924 h1m_init_res(&h1s->res);
1925 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001926 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001927 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001928 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001929 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001930 if (!chunk_memcat(&tmp, "\r\n", 2))
1931 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001932 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001933 goto done;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001934 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001935 else {
1936 /* EOM flag is set and it is the last block */
1937 if (htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
1938 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
1939 goto full;
1940 else if (!chunk_memcat(&tmp, "\r\n", 2))
1941 goto full;
1942 goto done;
1943 }
1944 else if (!chunk_memcat(&tmp, "\r\n", 2))
1945 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001946 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001947 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001948 break;
1949
Christopher Faulet94b2c762019-05-24 15:28:57 +02001950 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001951 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001952 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001953 /* If the message is not chunked, never
1954 * add the last chunk. */
1955 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001956 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001957 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 +02001958 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001959 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001960 else if (type != HTX_BLK_DATA)
1961 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001962
1963 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 +02001964
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001965 /* It is the last block of this message. After this one,
1966 * only tunneled data may be forwarded. */
1967 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
1968 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1969 last_data = 1;
1970 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02001971
1972 if (vlen > count) {
1973 /* Get the maximum amount of data we can xferred */
1974 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001975 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001976 }
1977
1978 chklen = 0;
1979 if (h1m->flags & H1_MF_CHNK) {
1980 chklen = b_room(&tmp);
1981 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1982 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1983 (chklen < 1048576) ? 5 : 8);
1984 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001985
1986 /* If it is the end of the chunked message (without EOT), reserve the
1987 * last chunk size */
1988 if (last_data)
1989 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001990 }
1991
1992 if (vlen + chklen > b_room(&tmp)) {
1993 /* too large for the buffer */
1994 if (chklen >= b_room(&tmp))
1995 goto full;
1996 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001997 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001998 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001999 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002000 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002001 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002002 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002003
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002004 /* Space already reserved, so it must succeed */
2005 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2006 goto error;
2007
Christopher Faulet6b81df72019-10-01 22:08:43 +02002008 if (h1m->state == H1_MSG_DATA)
2009 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002010 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002011 else
2012 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002013 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002014 if (last_data)
2015 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002016 break;
2017
Christopher Faulet94b2c762019-05-24 15:28:57 +02002018 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002019 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002020 goto error;
2021 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002022 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002023
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002024 /* If the message is not chunked, ignore
2025 * trailers. It may happen with H2 messages. */
2026 if (!(h1m->flags & H1_MF_CHNK))
2027 break;
2028
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002029 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002030 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002031 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002032 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2033 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002034 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002035 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002036 else { // HTX_BLK_TLR
2037 n = htx_get_blk_name(chn_htx, blk);
2038 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002039
2040 /* Try to adjust the case of the header name */
2041 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2042 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002043 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002044 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002045 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002046 break;
2047
Christopher Faulet94b2c762019-05-24 15:28:57 +02002048 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002049 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2050 goto error; /* For now return an error */
2051
Christopher Faulet94b2c762019-05-24 15:28:57 +02002052 done:
2053 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002054 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
2055 h1c->flags |= H1C_F_WAIT_INPUT;
2056 TRACE_STATE("Disable send on h1c (wait_input)", H1_EV_TX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
2057 }
2058 else if ((h1m->flags & H1_MF_RESP) &&
2059 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2060 /* a successful reply to a CONNECT or a protocol switching is sent
2061 * to the client. Switch the response to tunnel mode.
2062 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002063 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002064 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 +01002065 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002066
2067 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
Christopher Fauletb385b502021-01-13 18:47:57 +01002068 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002069 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02002070 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02002071 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002072
2073 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2074 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002075 break;
2076
Christopher Faulet9768c262018-10-22 09:34:31 +02002077 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002078 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002079 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002080 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002081 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002082 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002083 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002084 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2085 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002086 break;
2087 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002088
2089 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002090 total += vlen;
2091 count -= vlen;
2092 if (sz == vlen)
2093 blk = htx_remove_blk(chn_htx, blk);
2094 else {
2095 htx_cut_data_blk(chn_htx, blk, vlen);
2096 break;
2097 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002098 }
2099
Christopher Faulet9768c262018-10-22 09:34:31 +02002100 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002101 /* when the output buffer is empty, tmp shares the same area so that we
2102 * only have to update pointers and lengths.
2103 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002104 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2105 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002106 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002107 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002108
Willy Tarreau3815b222018-12-11 19:50:43 +01002109 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002110 out:
2111 if (!buf_room_for_htx_data(&h1c->obuf)) {
2112 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002113 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002114 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002115 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002116 /* Both the request and the response reached the DONE state. So set EOI
2117 * flag on the conn-stream. Most of time, the flag will already be set,
2118 * except for protocol upgrades. Report an error if data remains blocked
2119 * in the output buffer.
2120 */
2121 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2122 if (!htx_is_empty(chn_htx)) {
2123 h1c->flags |= H1C_F_ST_ERROR;
2124 TRACE_STATE("txn done but data waiting to be sent, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
2125 }
2126 h1s->cs->flags |= CS_FL_EOI;
2127 }
2128
Christopher Faulet6b81df72019-10-01 22:08:43 +02002129 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002130 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002131
2132 full:
2133 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2134 h1c->flags |= H1C_F_OUT_FULL;
2135 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002136}
2137
Christopher Faulet51dbc942018-09-13 09:05:15 +02002138/*********************************************************/
2139/* functions below are I/O callbacks from the connection */
2140/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002141static void h1_wake_stream_for_recv(struct h1s *h1s)
2142{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002143 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002144 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002145 tasklet_wakeup(h1s->subs->tasklet);
2146 h1s->subs->events &= ~SUB_RETRY_RECV;
2147 if (!h1s->subs->events)
2148 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002149 }
2150}
2151static void h1_wake_stream_for_send(struct h1s *h1s)
2152{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002153 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002154 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002155 tasklet_wakeup(h1s->subs->tasklet);
2156 h1s->subs->events &= ~SUB_RETRY_SEND;
2157 if (!h1s->subs->events)
2158 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002159 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002160}
2161
Christopher Fauletad4daf62021-01-21 17:49:01 +01002162/* alerts the data layer following this sequence :
2163 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2164 * - if its subscribed to send, then it's woken up for send
2165 * - if it was subscribed to neither, its ->wake() callback is called
2166 */
2167static void h1_alert(struct h1s *h1s)
2168{
2169 if (h1s->subs) {
2170 h1_wake_stream_for_recv(h1s);
2171 h1_wake_stream_for_send(h1s);
2172 }
2173 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2174 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2175 h1s->cs->data_cb->wake(h1s->cs);
2176 }
2177}
2178
Christopher Fauletc18fc232020-10-06 17:41:36 +02002179/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2180 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2181 * retryable errors (allocation error or buffer full). On success, the error is
2182 * copied in the output buffer.
2183*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002184static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002185{
2186 int rc = http_get_status_idx(h1c->errcode);
2187 int ret = 0;
2188
2189 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2190
2191 /* Verify if the error is mapped on /dev/null or any empty file */
2192 /// XXX: do a function !
2193 if (h1c->px->replies[rc] &&
2194 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2195 h1c->px->replies[rc]->body.errmsg &&
2196 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2197 /* Empty error, so claim a success */
2198 ret = 1;
2199 goto out;
2200 }
2201
2202 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2203 h1c->flags |= H1C_F_ERR_PENDING;
2204 goto out;
2205 }
2206
2207 if (!h1_get_buf(h1c, &h1c->obuf)) {
2208 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2209 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2210 goto out;
2211 }
2212 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2213 if (unlikely(ret <= 0)) {
2214 if (!ret) {
2215 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2216 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2217 goto out;
2218 }
2219 else {
2220 /* we cannot report this error, so claim a success */
2221 ret = 1;
2222 }
2223 }
2224 h1c->flags &= ~H1C_F_ERR_PENDING;
2225 out:
2226 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2227 return ret;
2228}
2229
2230/* Try to send a 500 internal error. It relies on h1_send_error to send the
2231 * error. This function takes care of incrementing stats and tracked counters.
2232 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002233static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002234{
2235 struct session *sess = h1c->conn->owner;
2236 int ret = 1;
2237
2238 session_inc_http_req_ctr(sess);
2239 session_inc_http_err_ctr(sess);
2240 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2241 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[5], 1);
2242 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
2243 if (sess->listener->counters)
2244 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
2245
2246 h1c->errcode = 500;
2247 ret = h1_send_error(h1c);
2248 sess_log(sess);
2249 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002250}
2251
Christopher Fauletc18fc232020-10-06 17:41:36 +02002252/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2253 * error. This function takes care of incrementing stats and tracked counters.
2254 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002255static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002256{
2257 struct session *sess = h1c->conn->owner;
2258 int ret = 1;
2259
2260 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2261 goto end;
2262
2263 session_inc_http_req_ctr(sess);
2264 session_inc_http_err_ctr(sess);
2265 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2266 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2267 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2268 if (sess->listener->counters)
2269 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2270
2271 h1c->errcode = 400;
2272 ret = h1_send_error(h1c);
2273 sess_log(sess);
2274
2275 end:
2276 return ret;
2277}
2278
Christopher Faulet2eed8002020-12-07 11:26:13 +01002279/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2280 * the error. This function takes care of incrementing stats and tracked
2281 * counters.
2282 */
2283static int h1_handle_not_impl_err(struct h1c *h1c)
2284{
2285 struct session *sess = h1c->conn->owner;
2286 int ret = 1;
2287
2288 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2289 goto end;
2290
2291 session_inc_http_req_ctr(sess);
2292 session_inc_http_err_ctr(sess);
2293 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2294 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2295 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2296 if (sess->listener->counters)
2297 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2298
2299 h1c->errcode = 501;
2300 ret = h1_send_error(h1c);
2301 sess_log(sess);
2302
2303 end:
2304 return ret;
2305}
2306
Christopher Fauletc18fc232020-10-06 17:41:36 +02002307/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2308 * error. This function takes care of incrementing stats and tracked counters.
2309 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002310static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002311{
2312 struct session *sess = h1c->conn->owner;
2313 int ret = 1;
2314
2315 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2316 goto end;
2317
2318 session_inc_http_req_ctr(sess);
2319 session_inc_http_err_ctr(sess);
2320 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2321 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2322 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2323 if (sess->listener->counters)
2324 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2325
2326 h1c->errcode = 408;
2327 ret = h1_send_error(h1c);
2328 sess_log(sess);
2329 end:
2330 return ret;
2331}
2332
2333
Christopher Faulet51dbc942018-09-13 09:05:15 +02002334/*
2335 * Attempt to read data, and subscribe if none available
2336 */
2337static int h1_recv(struct h1c *h1c)
2338{
2339 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002340 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002341 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002342
Christopher Faulet6b81df72019-10-01 22:08:43 +02002343 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2344
2345 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2346 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002347 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002348 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002349
Christopher Fauletae635762020-09-21 11:47:16 +02002350 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2351 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002352 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002353 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002355 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2356 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002357 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002358 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002359 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002360
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002361 /*
2362 * If we only have a small amount of data, realign it,
2363 * it's probably cheaper than doing 2 recv() calls.
2364 */
2365 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2366 b_slow_realign(&h1c->ibuf, trash.area, 0);
2367
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002368 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002369 if (!h1c->h1s ||
2370 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2371 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002372 flags |= CO_RFL_READ_ONCE;
2373
Willy Tarreau45f2b892018-12-05 07:59:27 +01002374 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002375 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002376 if (h1c->flags & H1C_F_IN_FULL) {
2377 h1c->flags &= ~H1C_F_IN_FULL;
2378 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2379 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002380
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002381 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002382 if (!b_data(&h1c->ibuf)) {
2383 /* try to pre-align the buffer like the rxbufs will be
2384 * to optimize memory copies.
2385 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002386 h1c->ibuf.head = sizeof(struct htx);
2387 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002388 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002389 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002390 if (max && !ret && h1_recv_allowed(h1c)) {
2391 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2392 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002393 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002394 else {
2395 h1_wake_stream_for_recv(h1c->h1s);
2396 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002397 }
2398
Christopher Faulet51dbc942018-09-13 09:05:15 +02002399 if (!b_data(&h1c->ibuf))
2400 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002401 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002402 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002403 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2404 }
2405
2406 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002407 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002408}
2409
2410
2411/*
2412 * Try to send data if possible
2413 */
2414static int h1_send(struct h1c *h1c)
2415{
2416 struct connection *conn = h1c->conn;
2417 unsigned int flags = 0;
2418 size_t ret;
2419 int sent = 0;
2420
Christopher Faulet6b81df72019-10-01 22:08:43 +02002421 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2422
2423 if (conn->flags & CO_FL_ERROR) {
2424 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002425 b_reset(&h1c->obuf);
2426 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002427 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002428
2429 if (!b_data(&h1c->obuf))
2430 goto end;
2431
Christopher Faulet46230362019-10-17 16:04:20 +02002432 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002433 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002434 if (h1c->flags & H1C_F_CO_STREAMER)
2435 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002436
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002437 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002438 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002439 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002440 if (h1c->flags & H1C_F_OUT_FULL) {
2441 h1c->flags &= ~H1C_F_OUT_FULL;
2442 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2443 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002444 b_del(&h1c->obuf, ret);
2445 sent = 1;
2446 }
2447
Christopher Faulet145aa472018-12-06 10:56:20 +01002448 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002449 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002450 /* error or output closed, nothing to send, clear the buffer to release it */
2451 b_reset(&h1c->obuf);
2452 }
2453
Christopher Faulet51dbc942018-09-13 09:05:15 +02002454 end:
Christopher Fauletb385b502021-01-13 18:47:57 +01002455 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_WAIT_INPUT)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002456 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002457
Christopher Faulet51dbc942018-09-13 09:05:15 +02002458 /* We're done, no more to send */
2459 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002460 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002461 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002462 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002463 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002464 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002465 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002466 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002467 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2468 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002469 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002470 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002471
Christopher Faulet6b81df72019-10-01 22:08:43 +02002472 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002473 return sent;
2474}
2475
Christopher Faulet51dbc942018-09-13 09:05:15 +02002476/* callback called on any event by the connection handler.
2477 * It applies changes and returns zero, or < 0 if it wants immediate
2478 * destruction of the connection.
2479 */
2480static int h1_process(struct h1c * h1c)
2481{
2482 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002483 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002484
Christopher Faulet6b81df72019-10-01 22:08:43 +02002485 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2486
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002487 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002488 if (b_data(&h1c->ibuf) && /* Input data to be processed */
2489 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2490 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002491 struct buffer *buf;
2492 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002493
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002494 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2495 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002496 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002497
2498 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002499 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
2500 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE)) { /* H2 upgrade supported by the proxy */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002501 /* Try to match H2 preface before parsing the request headers. */
2502 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2503 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002504 if (h1c->flags & H1C_F_ST_ATTACHED) {
2505 /* Force the REOS here to be sure to release the CS.
2506 Here ATTACHED implies !READY, and h1s defined
2507 */
2508 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2509 h1s->flags |= H1S_F_REOS;
2510 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002511 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002512 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002513 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002514 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002515
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002516 /* Create the H1 stream if not already there */
2517 if (!h1s) {
2518 h1s = h1c_frt_stream_new(h1c);
2519 if (!h1s) {
2520 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002521 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002522 goto no_parsing;
2523 }
2524 }
2525
2526 if (h1s->sess->t_idle == -1)
2527 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2528
2529 /* Get the stream rxbuf */
2530 buf = h1_get_buf(h1c, &h1s->rxbuf);
2531 if (!buf) {
2532 h1c->flags |= H1C_F_IN_SALLOC;
2533 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2534 return 0;
2535 }
2536
2537 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2538 h1_process_input(h1c, buf, count);
2539 h1_release_buf(h1c, &h1s->rxbuf);
2540 h1_set_idle_expiration(h1c);
2541
2542 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002543 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002544 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002545 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002546 }
2547 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2548 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002549 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002550 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002551 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2552 h1_handle_not_impl_err(h1c);
2553 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
2554 }
Christopher Fauletae635762020-09-21 11:47:16 +02002555 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002556 h1_send(h1c);
2557
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002558 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002559 if (!(h1c->flags & H1C_F_ST_READY)) {
2560 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002561 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002562 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002563 if (h1_handle_bad_req(h1c))
2564 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002565 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002566 }
2567
2568 /* Handle pending error, if any (only possible on frontend connection) */
2569 if (h1c->flags & H1C_F_ERR_PENDING) {
2570 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2571 if (h1_send_error(h1c))
2572 h1_send(h1c);
2573 }
Christopher Fauletae635762020-09-21 11:47:16 +02002574
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002575 /* If there is some pending outgoing data or error, just wait */
2576 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2577 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002578
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002579 /* Otherwise we can release the H1 connection */
2580 goto release;
2581 }
2582 else {
2583 /* Here there is still a H1 stream with a conn-stream.
2584 * Report the connection state at the stream level
2585 */
2586 if (conn_xprt_read0_pending(conn)) {
2587 h1s->flags |= H1S_F_REOS;
2588 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2589 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002590 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002591 h1s->cs->flags |= CS_FL_ERROR;
2592 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002593 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002594 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002595 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002596
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002597 if (!b_data(&h1c->ibuf))
2598 h1_release_buf(h1c, &h1c->ibuf);
2599
2600
2601 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2602 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2603 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002604 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002605
Christopher Faulet47365272018-10-31 17:40:50 +01002606 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002607 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002608 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002609 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002610
2611 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002612 if (h1c->flags & H1C_F_ST_ATTACHED) {
2613 /* Don't release the H1 connetion right now, we must destroy the
2614 * attached CS first. Here, the H1C must not be READY */
2615 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2616
2617 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2618 h1s->cs->flags |= CS_FL_EOS;
2619 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2620 h1s->cs->flags |= CS_FL_ERROR;
2621 h1_alert(h1s);
2622 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2623 }
2624 else {
2625 h1_release(h1c);
2626 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2627 }
Christopher Faulet539e0292018-11-19 10:40:09 +01002628 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002629}
2630
Willy Tarreau691d5032021-01-20 14:55:01 +01002631struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002632{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002633 struct connection *conn;
2634 struct tasklet *tl = (struct tasklet *)t;
2635 int conn_in_list;
2636 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002637 int ret = 0;
2638
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002639
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002640 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002641 if (tl->context == NULL) {
2642 /* The connection has been taken over by another thread,
2643 * we're no longer responsible for it, so just free the
2644 * tasklet, and do nothing.
2645 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002646 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002647 tasklet_free(tl);
2648 return NULL;
2649 }
2650 h1c = ctx;
2651 conn = h1c->conn;
2652
2653 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2654
2655 /* Remove the connection from the list, to be sure nobody attempts
2656 * to use it while we handle the I/O events
2657 */
2658 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2659 if (conn_in_list)
2660 MT_LIST_DEL(&conn->list);
2661
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002662 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002663
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002664 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002665 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002666 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002667 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002668 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002669 ret = h1_process(h1c);
2670 /* If we were in an idle list, we want to add it back into it,
2671 * unless h1_process() returned -1, which mean it has destroyed
2672 * the connection (testing !ret is enough, if h1_process() wasn't
2673 * called then ret will be 0 anyway.
2674 */
2675 if (!ret && conn_in_list) {
2676 struct server *srv = objt_server(conn->target);
2677
2678 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002679 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002680 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002681 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002682 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002683 return NULL;
2684}
2685
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002686static void h1_reset(struct connection *conn)
2687{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002688
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002689}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002690
2691static int h1_wake(struct connection *conn)
2692{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002693 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002694 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002695
Christopher Faulet6b81df72019-10-01 22:08:43 +02002696 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2697
Christopher Faulet539e0292018-11-19 10:40:09 +01002698 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002699 ret = h1_process(h1c);
2700 if (ret == 0) {
2701 struct h1s *h1s = h1c->h1s;
2702
Christopher Fauletad4daf62021-01-21 17:49:01 +01002703 if (h1c->flags & H1C_F_ST_ATTACHED)
2704 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002705 }
2706 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002707}
2708
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002709/* Connection timeout management. The principle is that if there's no receipt
2710 * nor sending for a certain amount of time, the connection is closed.
2711 */
2712static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2713{
2714 struct h1c *h1c = context;
2715 int expired = tick_is_expired(t->expire, now_ms);
2716
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002717 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002718
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002719 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002720 /* Make sure nobody stole the connection from us */
2721 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2722
2723 /* Somebody already stole the connection from us, so we should not
2724 * free it, we just have to free the task.
2725 */
2726 if (!t->context) {
2727 h1c = NULL;
2728 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2729 goto do_leave;
2730 }
2731
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002732 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002733 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2734 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002735 return t;
2736 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002737
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002738 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002739 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002740 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002741 if (h1c->flags & H1C_F_ST_READY) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002742 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2743 t->expire = TICK_ETERNITY;
2744 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2745 return t;
2746 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002747
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002748 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002749 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2750 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002751 if (h1_handle_req_tout(h1c))
2752 h1_send(h1c);
2753 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2754 h1_refresh_timeout(h1c);
Christopher Fauletcc043f62020-12-14 10:06:12 +01002755 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002756 return t;
2757 }
2758 }
2759
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002760 if (h1c->flags & H1C_F_ST_ATTACHED) {
2761 /* Don't release the H1 connetion right now, we must destroy the
2762 * attached CS first. Here, the H1C must not be READY */
2763 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
2764 h1_alert(h1c->h1s);
2765 h1_refresh_timeout(h1c);
2766 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2767 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2768 return t;
2769 }
2770
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002771 /* We're about to destroy the connection, so make sure nobody attempts
2772 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002773 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002774 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002775 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002776
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002777 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002778 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002779
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002780 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002781 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002782
2783 if (!h1c) {
2784 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002785 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002786 return NULL;
2787 }
2788
2789 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002790 h1_release(h1c);
2791 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002792 return NULL;
2793}
2794
Christopher Faulet51dbc942018-09-13 09:05:15 +02002795/*******************************************/
2796/* functions below are used by the streams */
2797/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002798
Christopher Faulet51dbc942018-09-13 09:05:15 +02002799/*
2800 * Attach a new stream to a connection
2801 * (Used for outgoing connections)
2802 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002803static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002804{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002805 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002806 struct conn_stream *cs = NULL;
2807 struct h1s *h1s;
2808
Christopher Faulet6b81df72019-10-01 22:08:43 +02002809 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002810 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002811 TRACE_DEVEL("leaving on h1c error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002812 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002813 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002814
Christopher Faulet236c93b2020-07-02 09:19:54 +02002815 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002816 if (!cs) {
2817 TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002818 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002819 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002820
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002821 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002822 if (h1s == NULL) {
2823 TRACE_DEVEL("leaving on h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002824 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002825 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002826
Christopher Faulet6b81df72019-10-01 22:08:43 +02002827 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002828 return cs;
2829 end:
2830 cs_free(cs);
2831 return NULL;
2832}
2833
2834/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2835 * this mux, it's easy as we can only store a single conn_stream.
2836 */
2837static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2838{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002839 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002840 struct h1s *h1s = h1c->h1s;
2841
2842 if (h1s)
2843 return h1s->cs;
2844
2845 return NULL;
2846}
2847
Christopher Faulet73c12072019-04-08 11:23:22 +02002848static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002849{
Christopher Faulet73c12072019-04-08 11:23:22 +02002850 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002851
Christopher Faulet6b81df72019-10-01 22:08:43 +02002852 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002853 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002854 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002855}
2856
2857/*
2858 * Detach the stream from the connection and possibly release the connection.
2859 */
2860static void h1_detach(struct conn_stream *cs)
2861{
2862 struct h1s *h1s = cs->ctx;
2863 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002864 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002865 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002866
Christopher Faulet6b81df72019-10-01 22:08:43 +02002867 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2868
Christopher Faulet51dbc942018-09-13 09:05:15 +02002869 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002870 if (!h1s) {
2871 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002872 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002873 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002874
Olivier Houchardf502aca2018-12-14 19:42:40 +01002875 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002876 h1c = h1s->h1c;
2877 h1s->cs = NULL;
2878
Christopher Faulet42849b02020-10-05 11:35:17 +02002879 sess->accept_date = date;
2880 sess->tv_accept = now;
2881 sess->t_handshake = 0;
2882 sess->t_idle = -1;
2883
Olivier Houchard8a786902018-12-15 16:05:40 +01002884 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2885 h1s_destroy(h1s);
2886
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002887 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 +02002888 /* If there are any excess server data in the input buffer,
2889 * release it and close the connection ASAP (some data may
2890 * remain in the output buffer). This happens if a server sends
2891 * invalid responses. So in such case, we don't want to reuse
2892 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002893 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002894 if (b_data(&h1c->ibuf)) {
2895 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002896 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002897 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002898 goto release;
2899 }
Christopher Faulet03627242019-07-19 11:34:08 +02002900
Christopher Faulet08016ab2020-07-01 16:10:06 +02002901 if (h1c->conn->flags & CO_FL_PRIVATE) {
2902 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002903 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2904 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002905 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002906 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002907 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002908 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002909 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002910 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002911 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2912 goto end;
2913 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002914 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002915 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002916 if (h1c->conn->owner == sess)
2917 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002918 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002919 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002920 /* The server doesn't want it, let's kill the connection right away */
2921 h1c->conn->mux->destroy(h1c);
2922 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2923 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002924 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002925 /* At this point, the connection has been added to the
2926 * server idle list, so another thread may already have
2927 * hijacked it, so we can't do anything with it.
2928 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002929 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002930 }
2931 }
2932
Christopher Fauletf1204b82019-07-19 14:51:06 +02002933 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002934 /* 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 +01002935 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002936 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002937 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002938 !h1c->conn->owner) {
2939 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002940 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002941 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002942 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002943 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002944 /* If we have a new request, process it immediately or
2945 * subscribe for reads waiting for new data
2946 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01002947 if (unlikely(b_data(&h1c->ibuf))) {
2948 if (h1_process(h1c) == -1)
2949 goto end;
2950 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002951 else
2952 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2953 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002954 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002955 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002956 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002957 end:
2958 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002959}
2960
2961
2962static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2963{
2964 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002965 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002966
2967 if (!h1s)
2968 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002969 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002970
Christopher Faulet6b81df72019-10-01 22:08:43 +02002971 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2972
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002973 if (cs->flags & CS_FL_SHR)
2974 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002975 if (cs->flags & CS_FL_KILL_CONN) {
2976 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2977 goto do_shutr;
2978 }
2979 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2980 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002981 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002982 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002983
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002984 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
2985 /* Here attached is implicit because there is CS */
2986 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2987 goto end;
2988 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002989 if (h1s->flags & H1S_F_WANT_KAL) {
2990 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002991 goto end;
2992 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002993
Christopher Faulet7f366362019-04-08 10:51:20 +02002994 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002995 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2996 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002997 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002998 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002999 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02003000 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003001 end:
3002 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003003}
3004
3005static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3006{
3007 struct h1s *h1s = cs->ctx;
3008 struct h1c *h1c;
3009
3010 if (!h1s)
3011 return;
3012 h1c = h1s->h1c;
3013
Christopher Faulet6b81df72019-10-01 22:08:43 +02003014 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3015
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003016 if (cs->flags & CS_FL_SHW)
3017 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003018 if (cs->flags & CS_FL_KILL_CONN) {
3019 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003020 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003021 }
3022 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3023 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3024 goto do_shutw;
3025 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003026
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003027 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3028 /* Here attached is implicit because there is CS */
3029 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3030 goto end;
3031 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003032 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3033 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003034 goto end;
3035 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003036
Christopher Faulet7f366362019-04-08 10:51:20 +02003037 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003038 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003039 if (!b_data(&h1c->obuf))
3040 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003041 end:
3042 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003043}
3044
Christopher Faulet666a0c42019-01-08 11:12:04 +01003045static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003046{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003047 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003048
Christopher Faulet6b81df72019-10-01 22:08:43 +02003049 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003050 conn_xprt_shutw(conn);
3051 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003052 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003053}
3054
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003055/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3056 * The <es> pointer is not allowed to differ from the one passed to the
3057 * subscribe() call. It always returns zero.
3058 */
3059static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003060{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003061 struct h1s *h1s = cs->ctx;
3062
3063 if (!h1s)
3064 return 0;
3065
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003066 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003067 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003068
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003069 es->events &= ~event_type;
3070 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003071 h1s->subs = NULL;
3072
3073 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003074 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003075
3076 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003077 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003078
Christopher Faulet51dbc942018-09-13 09:05:15 +02003079 return 0;
3080}
3081
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003082/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3083 * event subscriber <es> is not allowed to change from a previous call as long
3084 * as at least one event is still subscribed. The <event_type> must only be a
3085 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3086 * the conn_stream <cs> was already detached, in which case it will return -1.
3087 */
3088static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003089{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003090 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003091 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003092
3093 if (!h1s)
3094 return -1;
3095
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003096 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003097 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003098
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003099 es->events |= event_type;
3100 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003101
3102 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003103 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003104
3105
Christopher Faulet6b81df72019-10-01 22:08:43 +02003106 if (event_type & SUB_RETRY_SEND) {
3107 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003108 /*
3109 * If the conn_stream attempt to subscribe, and the
3110 * mux isn't subscribed to the connection, then it
3111 * probably means the connection wasn't established
3112 * yet, so we have to subscribe.
3113 */
3114 h1c = h1s->h1c;
3115 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3116 h1c->conn->xprt->subscribe(h1c->conn,
3117 h1c->conn->xprt_ctx,
3118 SUB_RETRY_SEND,
3119 &h1c->wait_event);
3120 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003121 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003122}
3123
3124/* Called from the upper layer, to receive data */
3125static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3126{
3127 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003128 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003129 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003130 size_t ret = 0;
3131
Willy Tarreau022e5e52020-09-10 09:33:15 +02003132 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003133
3134 /* Do nothing for now if not READY */
3135 if (!(h1c->flags & H1C_F_ST_READY)) {
3136 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3137 goto end;
3138 }
3139
Christopher Faulet539e0292018-11-19 10:40:09 +01003140 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02003141 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003142 else
3143 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003144
Christopher Faulete18777b2019-04-16 16:46:36 +02003145 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02003146 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02003147 h1c->flags |= H1C_F_WANT_SPLICE;
3148 TRACE_STATE("Block xprt rcv_buf to flush stream's buffer (want_splice)", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003149 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003150 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003151 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003152 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003153 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003154 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003155
3156 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003157 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003158 return ret;
3159}
3160
3161
3162/* Called from the upper layer, to send data */
3163static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3164{
3165 struct h1s *h1s = cs->ctx;
3166 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003167 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003168
3169 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003170 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003171 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003172
Willy Tarreau022e5e52020-09-10 09:33:15 +02003173 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003174
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003175 /* If we're not connected yet, or we're waiting for a handshake, stop
3176 * now, as we don't want to remove everything from the channel buffer
3177 * before we're sure we can send it.
3178 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003179 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003180 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003181 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003182 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003183
Christopher Fauletdea24742021-01-22 15:12:30 +01003184 if (h1c->flags & H1C_F_ST_ERROR) {
3185 cs->flags |= CS_FL_ERROR;
3186 TRACE_DEVEL("H1 connection is in error, leaving in error", H1_EV_STRM_SEND|H1_EV_H1C_ERR|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
3187 return 0;
3188 }
3189
Christopher Faulet46230362019-10-17 16:04:20 +02003190 /* Inherit some flags from the upper layer */
3191 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3192 if (flags & CO_SFL_MSG_MORE)
3193 h1c->flags |= H1C_F_CO_MSG_MORE;
3194 if (flags & CO_SFL_STREAMER)
3195 h1c->flags |= H1C_F_CO_STREAMER;
3196
Christopher Fauletc31872f2019-06-04 22:09:36 +02003197 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003198 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003199
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003200 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3201 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003202 else
3203 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003204
3205 if ((count - ret) > 0)
3206 h1c->flags |= H1C_F_CO_MSG_MORE;
3207
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003208 if (!ret)
3209 break;
3210 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003211 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003212 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003213 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003214 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003215
3216 if (h1c->flags & H1C_F_ST_ERROR) {
3217 TRACE_DEVEL("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
3218 cs->flags |= CS_FL_ERROR;
3219 }
3220
Christopher Faulet7a145d62020-08-05 11:31:16 +02003221 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003222 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003223 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003224}
3225
Willy Tarreaue5733232019-05-22 19:24:06 +02003226#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003227/* Send and get, using splicing */
3228static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3229{
3230 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003231 struct h1c *h1c = h1s->h1c;
3232 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003233 int ret = 0;
3234
Willy Tarreau022e5e52020-09-10 09:33:15 +02003235 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003236
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003237 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003238 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003239 TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003240 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003241 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003242 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003243 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003244 goto end;
3245 }
3246
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003247 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3248 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003249 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3250 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003251 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003252 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003253 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003254 }
3255
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003256 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003257 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3258 goto end;
3259 }
3260
Christopher Faulet1be55f92018-10-02 15:59:23 +02003261 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3262 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003263 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003264 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003265 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003266 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003267 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003268 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003269 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003270 }
3271
Christopher Faulet1be55f92018-10-02 15:59:23 +02003272 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003273 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003274 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003275 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003276 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003277 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003278
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003279 if ((h1s->flags & H1S_F_REOS) ||
3280 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003281 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003282 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003283 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003284 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003285
Willy Tarreau022e5e52020-09-10 09:33:15 +02003286 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003287 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003288}
3289
3290static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3291{
3292 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003293 int ret = 0;
3294
Willy Tarreau022e5e52020-09-10 09:33:15 +02003295 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003296
Christopher Faulet1be55f92018-10-02 15:59:23 +02003297 if (b_data(&h1s->h1c->obuf))
3298 goto end;
3299
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003300 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003301 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003302 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003303 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3304 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003305 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003306 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003307 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003308
Willy Tarreau022e5e52020-09-10 09:33:15 +02003309 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003310 return ret;
3311}
3312#endif
3313
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003314static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3315{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003316 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003317 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003318
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003319 switch (mux_ctl) {
3320 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003321 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003322 ret |= MUX_STATUS_READY;
3323 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003324 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003325 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3326 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003327 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3328 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3329 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003330 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003331 default:
3332 return -1;
3333 }
3334}
3335
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003336/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003337static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003338{
3339 struct h1c *h1c = conn->ctx;
3340 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003341 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003342
Christopher Fauletf376a312019-01-04 15:16:06 +01003343 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3344 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003345 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3346 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3347 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3348 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3349
3350 if (h1s) {
3351 char *method;
3352
3353 if (h1s->meth < HTTP_METH_OTHER)
3354 method = http_known_methods[h1s->meth].ptr;
3355 else
3356 method = "UNKNOWN";
3357 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3358 " .meth=%s status=%d",
3359 h1s, h1s->flags,
3360 h1m_state_str(h1s->req.state),
3361 h1m_state_str(h1s->res.state), method, h1s->status);
3362 if (h1s->cs)
3363 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3364 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003365
3366 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3367 if (h1s->subs) {
3368 if (h1s->subs) {
3369 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3370 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3371 h1s->subs->tasklet->calls,
3372 h1s->subs->tasklet->context);
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003373 if (h1s->subs->tasklet->calls >= 1000000)
3374 ret = 1;
Willy Tarreau150c4f82021-01-20 17:05:58 +01003375 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3376 chunk_appendf(&trash, ")");
3377 }
3378 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003379 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003380 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003381}
3382
3383
3384/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3385static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3386{
3387 struct h1_hdr_entry *entry;
3388
3389 /* Be sure there is a non-empty <to> */
3390 if (!strlen(to)) {
3391 memprintf(err, "expect <to>");
3392 return -1;
3393 }
3394
3395 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003396 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003397 memprintf(err, "<from> and <to> must not differ execpt the case");
3398 return -1;
3399 }
3400
3401 /* Be sure <from> does not already existsin the tree */
3402 if (ebis_lookup(&hdrs_map.map, from)) {
3403 memprintf(err, "duplicate entry '%s'", from);
3404 return -1;
3405 }
3406
3407 /* Create the entry and insert it in the tree */
3408 entry = malloc(sizeof(*entry));
3409 if (!entry) {
3410 memprintf(err, "out of memory");
3411 return -1;
3412 }
3413
3414 entry->node.key = strdup(from);
3415 entry->name.ptr = strdup(to);
3416 entry->name.len = strlen(to);
3417 if (!entry->node.key || !entry->name.ptr) {
3418 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003419 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003420 free(entry);
3421 memprintf(err, "out of memory");
3422 return -1;
3423 }
3424 ebis_insert(&hdrs_map.map, &entry->node);
3425 return 0;
3426}
3427
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003428/* Migrate the the connection to the current thread.
3429 * Return 0 if successful, non-zero otherwise.
3430 * Expected to be called with the old thread lock held.
3431 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003432static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003433{
3434 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003435 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003436
3437 if (fd_takeover(conn->handle.fd, conn) != 0)
3438 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003439
3440 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3441 /* We failed to takeover the xprt, even if the connection may
3442 * still be valid, flag it as error'd, as we have already
3443 * taken over the fd, and wake the tasklet, so that it will
3444 * destroy it.
3445 */
3446 conn->flags |= CO_FL_ERROR;
3447 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3448 return -1;
3449 }
3450
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003451 if (h1c->wait_event.events)
3452 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3453 h1c->wait_event.events, &h1c->wait_event);
3454 /* To let the tasklet know it should free itself, and do nothing else,
3455 * set its context to NULL.
3456 */
3457 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003458 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003459
3460 task = h1c->task;
3461 if (task) {
3462 task->context = NULL;
3463 h1c->task = NULL;
3464 __ha_barrier_store();
3465 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003466
3467 h1c->task = task_new(tid_bit);
3468 if (!h1c->task) {
3469 h1_release(h1c);
3470 return -1;
3471 }
3472 h1c->task->process = h1_timeout_task;
3473 h1c->task->context = h1c;
3474 }
3475 h1c->wait_event.tasklet = tasklet_new();
3476 if (!h1c->wait_event.tasklet) {
3477 h1_release(h1c);
3478 return -1;
3479 }
3480 h1c->wait_event.tasklet->process = h1_io_cb;
3481 h1c->wait_event.tasklet->context = h1c;
3482 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3483 SUB_RETRY_RECV, &h1c->wait_event);
3484
3485 return 0;
3486}
3487
3488
Christopher Faulet98fbe952019-07-22 16:18:24 +02003489static void h1_hdeaders_case_adjust_deinit()
3490{
3491 struct ebpt_node *node, *next;
3492 struct h1_hdr_entry *entry;
3493
3494 node = ebpt_first(&hdrs_map.map);
3495 while (node) {
3496 next = ebpt_next(node);
3497 ebpt_delete(node);
3498 entry = container_of(node, struct h1_hdr_entry, node);
3499 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003500 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003501 free(entry);
3502 node = next;
3503 }
3504 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003505}
3506
Christopher Faulet98fbe952019-07-22 16:18:24 +02003507static int cfg_h1_headers_case_adjust_postparser()
3508{
3509 FILE *file = NULL;
3510 char *c, *key_beg, *key_end, *value_beg, *value_end;
3511 char *err;
3512 int rc, line = 0, err_code = 0;
3513
3514 if (!hdrs_map.name)
3515 goto end;
3516
3517 file = fopen(hdrs_map.name, "r");
3518 if (!file) {
3519 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3520 hdrs_map.name);
3521 err_code |= ERR_ALERT | ERR_FATAL;
3522 goto end;
3523 }
3524
3525 /* now parse all lines. The file may contain only two header name per
3526 * line, separated by spaces. All heading and trailing spaces will be
3527 * ignored. Lines starting with a # are ignored.
3528 */
3529 while (fgets(trash.area, trash.size, file) != NULL) {
3530 line++;
3531 c = trash.area;
3532
3533 /* strip leading spaces and tabs */
3534 while (*c == ' ' || *c == '\t')
3535 c++;
3536
3537 /* ignore emptu lines, or lines beginning with a dash */
3538 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3539 continue;
3540
3541 /* look for the end of the key */
3542 key_beg = c;
3543 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3544 c++;
3545 key_end = c;
3546
3547 /* strip middle spaces and tabs */
3548 while (*c == ' ' || *c == '\t')
3549 c++;
3550
3551 /* look for the end of the value, it is the end of the line */
3552 value_beg = c;
3553 while (*c && *c != '\n' && *c != '\r')
3554 c++;
3555 value_end = c;
3556
3557 /* trim possibly trailing spaces and tabs */
3558 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3559 value_end--;
3560
3561 /* set final \0 and check entries */
3562 *key_end = '\0';
3563 *value_end = '\0';
3564
3565 err = NULL;
3566 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3567 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003568 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3569 hdrs_map.name, err, line);
3570 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003571 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003572 goto end;
3573 }
3574 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003575 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3576 hdrs_map.name, err, line);
3577 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003578 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003579 }
3580 }
3581
3582 end:
3583 if (file)
3584 fclose(file);
3585 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3586 return err_code;
3587}
3588
3589
3590/* config parser for global "h1-outgoing-header-case-adjust" */
3591static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3592 struct proxy *defpx, const char *file, int line,
3593 char **err)
3594{
3595 if (too_many_args(2, args, err, NULL))
3596 return -1;
3597 if (!*(args[1]) || !*(args[2])) {
3598 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3599 return -1;
3600 }
3601 return add_hdr_case_adjust(args[1], args[2], err);
3602}
3603
3604/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3605static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3606 struct proxy *defpx, const char *file, int line,
3607 char **err)
3608{
3609 if (too_many_args(1, args, err, NULL))
3610 return -1;
3611 if (!*(args[1])) {
3612 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3613 return -1;
3614 }
3615 free(hdrs_map.name);
3616 hdrs_map.name = strdup(args[1]);
3617 return 0;
3618}
3619
3620
3621/* config keyword parsers */
3622static struct cfg_kw_list cfg_kws = {{ }, {
3623 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3624 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3625 { 0, NULL, NULL },
3626 }
3627};
3628
3629INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3630REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3631
3632
Christopher Faulet51dbc942018-09-13 09:05:15 +02003633/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003634/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003635/****************************************/
3636
3637/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003638static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003639 .init = h1_init,
3640 .wake = h1_wake,
3641 .attach = h1_attach,
3642 .get_first_cs = h1_get_first_cs,
3643 .detach = h1_detach,
3644 .destroy = h1_destroy,
3645 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003646 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003647 .rcv_buf = h1_rcv_buf,
3648 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003649#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003650 .rcv_pipe = h1_rcv_pipe,
3651 .snd_pipe = h1_snd_pipe,
3652#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003653 .subscribe = h1_subscribe,
3654 .unsubscribe = h1_unsubscribe,
3655 .shutr = h1_shutr,
3656 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003657 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003658 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003659 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003660 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003661 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003662 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003663};
3664
3665
3666/* this mux registers default HTX proto */
3667static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003668{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003669
Willy Tarreau0108d902018-11-25 19:14:37 +01003670INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3671
Christopher Faulet51dbc942018-09-13 09:05:15 +02003672/*
3673 * Local variables:
3674 * c-indent-level: 8
3675 * c-basic-offset: 8
3676 * End:
3677 */