blob: 0c5fc4473ffd0d88dd6ea0f1307f1fdef0457c78 [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 */
49#define H1C_F_ST_ATTACHED 0x00000200 /* Set when a H1 stream with a conn-stream is attached to the connection */
50#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) */
54#define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED)
55/* 0x00002000 - 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020056
Christopher Faulet3ced1d12020-11-27 16:44:01 +010057#define H1C_F_WAIT_OPPOSITE 0x00010000 /* Don't read more data for now, waiting sync with opposite side */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050058#define H1C_F_WANT_SPLICE 0x00020000 /* Don't read into a buffer because we want to use or we are using splicing */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010059#define H1C_F_ERR_PENDING 0x00040000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
60#define H1C_F_WAIT_NEXT_REQ 0x00080000 /* waiting for the next request to start, use keep-alive timeout */
61#define H1C_F_UPG_H2C 0x00100000 /* set if an upgrade to h2 should be done */
62#define H1C_F_CO_MSG_MORE 0x00200000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
63#define H1C_F_CO_STREAMER 0x00400000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050064/* 0x00800000 - 0x40000000 unused */
Christopher Faulet129817b2018-09-20 16:14:40 +020065
Christopher Faulet3ced1d12020-11-27 16:44:01 +010066#define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */
Christopher Faulet46230362019-10-17 16:04:20 +020067
Christopher Faulet51dbc942018-09-13 09:05:15 +020068/*
69 * H1 Stream flags (32 bits)
70 */
Christopher Faulet129817b2018-09-20 16:14:40 +020071#define H1S_F_NONE 0x00000000
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050072/* 0x00000001..0x00000004 unused */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020073#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020074#define H1S_F_WANT_KAL 0x00000010
75#define H1S_F_WANT_TUN 0x00000020
76#define H1S_F_WANT_CLO 0x00000040
77#define H1S_F_WANT_MSK 0x00000070
78#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020079
Christopher Faulet2eed8002020-12-07 11:26:13 +010080#define H1S_F_PARSING_DONE 0x00000200 /* Set when incoming message parsing is finished (EOM added) */
81
82#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 +020083#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
84#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
85#define H1S_F_ERROR 0x00001800 /* stream error mask */
86
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020087#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 +020088#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020089
90/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020091struct h1c {
92 struct connection *conn;
93 struct proxy *px;
94 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +020095 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096 struct buffer ibuf; /* Input buffer to store data before parsing */
97 struct buffer obuf; /* Output buffer to store data after reformatting */
98
99 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
100 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
101
102 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100103 struct task *task; /* timeout management task */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200104 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
105 int timeout; /* client/server timeout duration */
106 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200107};
108
109/* H1 stream descriptor */
110struct h1s {
111 struct h1c *h1c;
112 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100113 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200114
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100115 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200116
Olivier Houchardf502aca2018-12-14 19:42:40 +0100117 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200118 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200119 struct h1m req;
120 struct h1m res;
121
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500122 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200123 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200124};
125
Christopher Faulet98fbe952019-07-22 16:18:24 +0200126/* Map of headers used to convert outgoing headers */
127struct h1_hdrs_map {
128 char *name;
129 struct eb_root map;
130};
131
132/* An entry in a headers map */
133struct h1_hdr_entry {
134 struct ist name;
135 struct ebpt_node node;
136};
137
138/* Declare the headers map */
139static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
140
141
Christopher Faulet6b81df72019-10-01 22:08:43 +0200142/* trace source and events */
143static void h1_trace(enum trace_level level, uint64_t mask,
144 const struct trace_source *src,
145 const struct ist where, const struct ist func,
146 const void *a1, const void *a2, const void *a3, const void *a4);
147
148/* The event representation is split like this :
149 * h1c - internal H1 connection
150 * h1s - internal H1 stream
151 * strm - application layer
152 * rx - data receipt
153 * tx - data transmission
154 *
155 */
156static const struct trace_event h1_trace_events[] = {
157#define H1_EV_H1C_NEW (1ULL << 0)
158 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
159#define H1_EV_H1C_RECV (1ULL << 1)
160 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
161#define H1_EV_H1C_SEND (1ULL << 2)
162 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
163#define H1_EV_H1C_BLK (1ULL << 3)
164 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
165#define H1_EV_H1C_WAKE (1ULL << 4)
166 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
167#define H1_EV_H1C_END (1ULL << 5)
168 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
169#define H1_EV_H1C_ERR (1ULL << 6)
170 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
171
172#define H1_EV_RX_DATA (1ULL << 7)
173 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
174#define H1_EV_RX_EOI (1ULL << 8)
175 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
176#define H1_EV_RX_HDRS (1ULL << 9)
177 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
178#define H1_EV_RX_BODY (1ULL << 10)
179 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
180#define H1_EV_RX_TLRS (1ULL << 11)
181 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
182
183#define H1_EV_TX_DATA (1ULL << 12)
184 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
185#define H1_EV_TX_EOI (1ULL << 13)
186 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
187#define H1_EV_TX_HDRS (1ULL << 14)
188 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
189#define H1_EV_TX_BODY (1ULL << 15)
190 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
191#define H1_EV_TX_TLRS (1ULL << 16)
192 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
193
194#define H1_EV_H1S_NEW (1ULL << 17)
195 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
196#define H1_EV_H1S_BLK (1ULL << 18)
197 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
198#define H1_EV_H1S_END (1ULL << 19)
199 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
200#define H1_EV_H1S_ERR (1ULL << 20)
201 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
202
203#define H1_EV_STRM_NEW (1ULL << 21)
204 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
205#define H1_EV_STRM_RECV (1ULL << 22)
206 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
207#define H1_EV_STRM_SEND (1ULL << 23)
208 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
209#define H1_EV_STRM_WAKE (1ULL << 24)
210 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
211#define H1_EV_STRM_SHUT (1ULL << 25)
212 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
213#define H1_EV_STRM_END (1ULL << 26)
214 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
215#define H1_EV_STRM_ERR (1ULL << 27)
216 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
217
218 { }
219};
220
221static const struct name_desc h1_trace_lockon_args[4] = {
222 /* arg1 */ { /* already used by the connection */ },
223 /* arg2 */ { .name="h1s", .desc="H1 stream" },
224 /* arg3 */ { },
225 /* arg4 */ { }
226};
227
228static const struct name_desc h1_trace_decoding[] = {
229#define H1_VERB_CLEAN 1
230 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
231#define H1_VERB_MINIMAL 2
232 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
233#define H1_VERB_SIMPLE 3
234 { .name="simple", .desc="add request/response status line or htx info when available" },
235#define H1_VERB_ADVANCED 4
236 { .name="advanced", .desc="add header fields or frame decoding when available" },
237#define H1_VERB_COMPLETE 5
238 { .name="complete", .desc="add full data dump when available" },
239 { /* end */ }
240};
241
242static struct trace_source trace_h1 = {
243 .name = IST("h1"),
244 .desc = "HTTP/1 multiplexer",
245 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
246 .default_cb = h1_trace,
247 .known_events = h1_trace_events,
248 .lockon_args = h1_trace_lockon_args,
249 .decoding = h1_trace_decoding,
250 .report_events = ~0, // report everything by default
251};
252
253#define TRACE_SOURCE &trace_h1
254INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
255
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100257DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
258DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200259
Christopher Faulet51dbc942018-09-13 09:05:15 +0200260static int h1_recv(struct h1c *h1c);
261static int h1_send(struct h1c *h1c);
262static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100263/* h1_io_cb is exported to see it resolved in "show fd" */
264struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100265static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200266static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200267static void h1_wake_stream_for_recv(struct h1s *h1s);
268static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200269
Christopher Faulet6b81df72019-10-01 22:08:43 +0200270/* the H1 traces always expect that arg1, if non-null, is of type connection
271 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
272 * that arg3, if non-null, is a htx for rx/tx headers.
273 */
274static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
275 const struct ist where, const struct ist func,
276 const void *a1, const void *a2, const void *a3, const void *a4)
277{
278 const struct connection *conn = a1;
279 const struct h1c *h1c = conn ? conn->ctx : NULL;
280 const struct h1s *h1s = a2;
281 const struct htx *htx = a3;
282 const size_t *val = a4;
283
284 if (!h1c)
285 h1c = (h1s ? h1s->h1c : NULL);
286
287 if (!h1c || src->verbosity < H1_VERB_CLEAN)
288 return;
289
290 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200291 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200292
293 /* Display request and response states if h1s is defined */
294 if (h1s)
295 chunk_appendf(&trace_buf, " [%s, %s]",
296 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
297
298 if (src->verbosity == H1_VERB_CLEAN)
299 return;
300
301 /* Display the value to the 4th argument (level > STATE) */
302 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100303 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200304
305 /* Display status-line if possible (verbosity > MINIMAL) */
306 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
307 const struct htx_blk *blk = htx_get_head_blk(htx);
308 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
309 enum htx_blk_type type = htx_get_blk_type(blk);
310
311 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
312 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
313 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
314 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
315 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
316 }
317
318 /* Display h1c info and, if defined, h1s info (pointer + flags) */
319 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
320 if (h1s)
321 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
322
323 if (src->verbosity == H1_VERB_MINIMAL)
324 return;
325
326 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
327 if (src->level > TRACE_LEVEL_USER) {
328 if (src->verbosity == H1_VERB_COMPLETE ||
329 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
330 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
331 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
332 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
333 if (src->verbosity == H1_VERB_COMPLETE ||
334 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
335 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
336 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
337 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
338 }
339
340 /* Display htx info if defined (level > USER) */
341 if (src->level > TRACE_LEVEL_USER && htx) {
342 int full = 0;
343
344 /* Full htx info (level > STATE && verbosity > SIMPLE) */
345 if (src->level > TRACE_LEVEL_STATE) {
346 if (src->verbosity == H1_VERB_COMPLETE)
347 full = 1;
348 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
349 full = 1;
350 }
351
352 chunk_memcat(&trace_buf, "\n\t", 2);
353 htx_dump(&trace_buf, htx, full);
354 }
355}
356
357
Christopher Faulet51dbc942018-09-13 09:05:15 +0200358/*****************************************************/
359/* functions below are for dynamic buffer management */
360/*****************************************************/
361/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100362 * Indicates whether or not we may receive data. The rules are the following :
363 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200364 * must not attempt to receive
365 * - if we are waiting for the connection establishment, we must not attempt
366 * to receive
367 * - if an error was detected on the stream we must not attempt to receive
368 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100369 * - if the input buffer failed to be allocated or is full , we must not try
370 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200371 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372 * - otherwise must may not attempt to receive
373 */
374static inline int h1_recv_allowed(const struct h1c *h1c)
375{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100376 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100377 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 +0200378 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200379 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380
Willy Tarreau2febb842020-07-31 09:15:43 +0200381 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
382 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 +0100383 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200384 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100385
Christopher Faulet119ac872020-09-30 17:33:22 +0200386 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
387 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
388 return 0;
389 }
390
Christopher Faulet089acd52020-09-21 10:57:52 +0200391 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
392 TRACE_DEVEL("recv not allowed (wait_opposite)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200393 return 0;
394 }
395
Christopher Fauletd17ad822020-09-24 15:14:29 +0200396 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200397 return 1;
398
Christopher Faulet6b81df72019-10-01 22:08:43 +0200399 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 +0200400 return 0;
401}
402
403/*
404 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
405 * flags are used to figure what buffer was requested. It returns 1 if the
406 * allocation succeeds, in which case the connection is woken up, or 0 if it's
407 * impossible to wake up and we prefer to be woken up later.
408 */
409static int h1_buf_available(void *target)
410{
411 struct h1c *h1c = target;
412
413 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200414 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 +0200415 h1c->flags &= ~H1C_F_IN_ALLOC;
416 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200417 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200418 return 1;
419 }
420
421 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200422 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 +0200423 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200424 if (h1c->h1s)
425 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426 return 1;
427 }
428
Christopher Fauletd17ad822020-09-24 15:14:29 +0200429 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
430 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
431 h1c->flags &= ~H1C_F_IN_SALLOC;
432 tasklet_wakeup(h1c->wait_event.tasklet);
433 return 1;
434 }
435
Christopher Faulet51dbc942018-09-13 09:05:15 +0200436 return 0;
437}
438
439/*
440 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
441 */
442static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
443{
444 struct buffer *buf = NULL;
445
Willy Tarreau21046592020-02-26 10:39:36 +0100446 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200447 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
448 h1c->buf_wait.target = h1c;
449 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200450 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200451 }
452 return buf;
453}
454
455/*
456 * Release a buffer, if any, and try to wake up entities waiting in the buffer
457 * wait queue.
458 */
459static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
460{
461 if (bptr->size) {
462 b_free(bptr);
463 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
464 }
465}
466
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100467/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100468 * 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 +0100469 * not. This flag is only set when no H1S is attached and when the previous
470 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100471 */
472static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200473{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100474 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200475
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100476 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200477}
478
Willy Tarreau00f18a32019-01-26 12:19:01 +0100479/* returns the number of streams still available on a connection */
480static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100481{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100482 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100483}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200484
Christopher Faulet7a145d62020-08-05 11:31:16 +0200485/* Refresh the h1c task timeout if necessary */
486static void h1_refresh_timeout(struct h1c *h1c)
487{
488 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100489 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200490 /* half-closed or dead connections : switch to clientfin/serverfin
491 * timeouts so that we don't hang too long on clients that have
492 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200493 */
494 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200495 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
496 }
497 else if (b_data(&h1c->obuf)) {
498 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200499 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200500 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
501 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100502 else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200503 /* front connections waiting for a stream need a timeout. */
504 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200505 TRACE_DEVEL("refreshing connection's timeout (alive front h1c without a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200506 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200507 else {
508 /* alive back connections of front connections with a conn-stream attached */
509 h1c->task->expire = TICK_ETERNITY;
510 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
511 }
512
Christopher Fauletdbe57792020-10-05 17:50:58 +0200513 /* Finally set the idle expiration date if shorter */
514 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200515 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
516 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200517 }
518}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200519
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200520static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200521{
522 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
523 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
524 h1c->idle_exp = TICK_ETERNITY;
525 return;
526 }
527
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100528 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200529 if (!tick_isset(h1c->idle_exp)) {
530 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
531 !b_data(&h1c->ibuf) && /* No input data */
532 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
533 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
534 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
535 }
536 else {
537 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
538 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
539 }
540 }
541 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100542 else if (h1c->flags & H1C_F_ST_EMBRYONIC) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200543 if (!tick_isset(h1c->idle_exp)) {
544 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
545 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
546 }
547 }
548 else { // CS_ATTACHED or SHUTDOWN
549 h1c->idle_exp = TICK_ETERNITY;
550 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
551 }
552}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200553/*****************************************************************/
554/* functions below are dedicated to the mux setup and management */
555/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200556
557/* returns non-zero if there are input data pending for stream h1s. */
558static inline size_t h1s_data_pending(const struct h1s *h1s)
559{
560 const struct h1m *h1m;
561
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200562 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200563 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100564 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200565
566 return b_data(&h1s->h1c->ibuf);
567}
568
Christopher Faulet16df1782020-12-04 16:47:41 +0100569/* Creates a new conn-stream and the associate stream. <input> is used as input
570 * buffer for the stream. On success, it is transferred to the stream and the
571 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
572 * mux must still take care of it. However, there is nothing special to do
573 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
574 * b_free() on it is always safe. This function returns the conn-stream on
575 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200576static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100577{
578 struct conn_stream *cs;
579
Christopher Faulet6b81df72019-10-01 22:08:43 +0200580 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200581 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200582 if (!cs) {
583 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 +0100584 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200585 }
Christopher Faulet47365272018-10-31 17:40:50 +0100586 h1s->cs = cs;
587 cs->ctx = h1s;
588
589 if (h1s->flags & H1S_F_NOT_FIRST)
590 cs->flags |= CS_FL_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100591 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
Christopher Faulet47365272018-10-31 17:40:50 +0100592
Christopher Faulet27182292020-07-03 15:08:49 +0200593 if (global.tune.options & GTUNE_USE_SPLICE) {
594 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100595 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200596 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100597
Christopher Faulet26256f82020-09-14 11:40:13 +0200598 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200599 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 +0100600 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200601 }
602
603 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100604 return cs;
605
606 err:
607 cs_free(cs);
608 h1s->cs = NULL;
609 return NULL;
610}
611
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200612static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200613{
614 struct h1s *h1s;
615
Christopher Faulet6b81df72019-10-01 22:08:43 +0200616 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
617
Christopher Faulet51dbc942018-09-13 09:05:15 +0200618 h1s = pool_alloc(pool_head_h1s);
619 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100620 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200621 h1s->h1c = h1c;
622 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200623 h1s->sess = NULL;
624 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100625 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100626 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200627 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200628
629 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100630 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200631
Christopher Faulet129817b2018-09-20 16:14:40 +0200632 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100633 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200634
635 h1s->status = 0;
636 h1s->meth = HTTP_METH_OTHER;
637
Christopher Faulet47365272018-10-31 17:40:50 +0100638 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
639 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100640 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100641
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200642 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
643 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200644
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200645 fail:
646 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
647 return NULL;
648}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100649
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200650static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
651{
652 struct session *sess = h1c->conn->owner;
653 struct h1s *h1s;
654
655 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
656
657 h1s = h1s_new(h1c);
658 if (!h1s)
659 goto fail;
660
661 h1s->sess = sess;
662
663 if (h1c->px->options2 & PR_O2_REQBUG_OK)
664 h1s->req.err_pos = -1;
665
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200666 h1c->idle_exp = TICK_ETERNITY;
667 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200668 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200669 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100670
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200671 fail:
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200672 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
673 return NULL;
674}
675
676static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
677{
678 struct h1s *h1s;
679
680 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
681
682 h1s = h1s_new(h1c);
683 if (!h1s)
684 goto fail;
685
686 h1s->cs = cs;
687 h1s->sess = sess;
688 cs->ctx = h1s;
689
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100690 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200691
692 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
693 h1s->res.err_pos = -1;
694
695 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
696 return h1s;
697
698 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200699 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 +0100700 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200701}
702
703static void h1s_destroy(struct h1s *h1s)
704{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200705 if (h1s) {
706 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200707
Christopher Faulet6b81df72019-10-01 22:08:43 +0200708 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200709 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200710
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100711 if (h1s->subs)
712 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200713
Christopher Fauletd17ad822020-09-24 15:14:29 +0200714 h1_release_buf(h1c, &h1s->rxbuf);
715
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100716 h1c->flags &= ~(H1C_F_WAIT_OPPOSITE|H1C_F_WANT_SPLICE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200717 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
718 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200719 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100720 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200721 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
722 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100723
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100724 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100725 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100726 (h1s->flags & (H1S_F_WANT_KAL|H1S_F_PARSING_DONE)) == (H1S_F_WANT_KAL|H1S_F_PARSING_DONE) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100727 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100728 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100729 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
730 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200731 else {
732 TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100733 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200734 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200735 pool_free(pool_head_h1s, h1s);
736 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200737}
738
739/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200740 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500741 * to the existing conn_stream (for outgoing connections or for incoming ones
742 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200743 * establishment). <input> is always used as Input buffer and may contain
744 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
745 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200746 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200747static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
748 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200749{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200750 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100751 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200752 void *conn_ctx = conn->ctx;
753
754 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200755
756 h1c = pool_alloc(pool_head_h1c);
757 if (!h1c)
758 goto fail_h1c;
759 h1c->conn = conn;
760 h1c->px = proxy;
761
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100762 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200763 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200764 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200765 h1c->obuf = BUF_NULL;
766 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200767 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200768
Willy Tarreau21046592020-02-26 10:39:36 +0100769 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200770 h1c->wait_event.tasklet = tasklet_new();
771 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200772 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200773 h1c->wait_event.tasklet->process = h1_io_cb;
774 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100775 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200776 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200777
Christopher Faulete9b70722019-04-08 10:46:02 +0200778 if (conn_is_back(conn)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200779 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OPPOSITE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100780 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
781 if (tick_isset(proxy->timeout.serverfin))
782 h1c->shut_timeout = proxy->timeout.serverfin;
783 } else {
784 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
785 if (tick_isset(proxy->timeout.clientfin))
786 h1c->shut_timeout = proxy->timeout.clientfin;
787 }
788 if (tick_isset(h1c->timeout)) {
789 t = task_new(tid_bit);
790 if (!t)
791 goto fail;
792
793 h1c->task = t;
794 t->process = h1_timeout_task;
795 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200796
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100797 t->expire = tick_add(now_ms, h1c->timeout);
798 }
799
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100800 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200801
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200802 if (h1c->flags & H1C_F_IS_BACK) {
803 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200804 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
805 goto fail;
806 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100807
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200808 if (t) {
809 h1_set_idle_expiration(h1c);
810 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100811 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200812 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100813
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200814 /* prepare to read something */
815 if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200816 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200817
818 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200819 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200820 return 0;
821
822 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200823 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200824 if (h1c->wait_event.tasklet)
825 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200826 pool_free(pool_head_h1c, h1c);
827 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200828 conn->ctx = conn_ctx; // restore saved context
829 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200830 return -1;
831}
832
Christopher Faulet73c12072019-04-08 11:23:22 +0200833/* release function. This one should be called to free all resources allocated
834 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200835 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200836static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200837{
Christopher Faulet61840e72019-04-15 09:33:32 +0200838 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200839
Christopher Faulet6b81df72019-10-01 22:08:43 +0200840 TRACE_POINT(H1_EV_H1C_END);
841
Christopher Faulet51dbc942018-09-13 09:05:15 +0200842 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200843 /* The connection must be aattached to this mux to be released */
844 if (h1c->conn && h1c->conn->ctx == h1c)
845 conn = h1c->conn;
846
Christopher Faulet6b81df72019-10-01 22:08:43 +0200847 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
848
Christopher Faulet61840e72019-04-15 09:33:32 +0200849 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200850 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200851 /* Make sure we're no longer subscribed to anything */
852 if (h1c->wait_event.events)
853 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
854 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200855 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200856 /* connection successfully upgraded to H2, this
857 * mux was already released */
858 return;
859 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200860 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200861 sess_log(conn->owner); /* Log if the upgrade failed */
862 }
863
Olivier Houchard45c44372019-06-11 14:06:23 +0200864
Willy Tarreau21046592020-02-26 10:39:36 +0100865 if (MT_LIST_ADDED(&h1c->buf_wait.list))
866 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200867
868 h1_release_buf(h1c, &h1c->ibuf);
869 h1_release_buf(h1c, &h1c->obuf);
870
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100871 if (h1c->task) {
872 h1c->task->context = NULL;
873 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
874 h1c->task = NULL;
875 }
876
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200877 if (h1c->wait_event.tasklet)
878 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200879
Christopher Fauletf2824e62018-10-01 12:12:37 +0200880 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200881 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100882 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200883 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200884 pool_free(pool_head_h1c, h1c);
885 }
886
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200887 if (conn) {
888 conn->mux = NULL;
889 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200890 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200891
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200892 conn_stop_tracking(conn);
893 conn_full_close(conn);
894 if (conn->destroy_cb)
895 conn->destroy_cb(conn);
896 conn_free(conn);
897 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200898}
899
900/******************************************************/
901/* functions below are for the H1 protocol processing */
902/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200903/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
904 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200905 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100906static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200907{
Christopher Faulet570d1612018-11-26 11:13:57 +0100908 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200909
Christopher Faulet570d1612018-11-26 11:13:57 +0100910 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200911 (*(p + 5) > '1' ||
912 (*(p + 5) == '1' && *(p + 7) >= '1')))
913 h1m->flags |= H1_MF_VER_11;
914}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200915
Christopher Faulet9768c262018-10-22 09:34:31 +0200916/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
917 * greater or equal to 1.1
918 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100919static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200920{
Christopher Faulet570d1612018-11-26 11:13:57 +0100921 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200922
Christopher Faulet570d1612018-11-26 11:13:57 +0100923 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200924 (*(p + 5) > '1' ||
925 (*(p + 5) == '1' && *(p + 7) >= '1')))
926 h1m->flags |= H1_MF_VER_11;
927}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200928
Christopher Fauletf2824e62018-10-01 12:12:37 +0200929/* Deduce the connection mode of the client connection, depending on the
930 * configuration and the H1 message flags. This function is called twice, the
931 * first time when the request is parsed and the second time when the response
932 * is parsed.
933 */
934static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
935{
936 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200937
938 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100939 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100940 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100941 h1s->status == 101) {
942 /* Either we've established an explicit tunnel, or we're
943 * switching the protocol. In both cases, we're very unlikely to
944 * understand the next protocols. We have to switch to tunnel
945 * mode, so that we transfer the request and responses then let
946 * this protocol pass unmodified. When we later implement
947 * specific parsers for such protocols, we'll want to check the
948 * Upgrade header which contains information about that protocol
949 * for responses with status 101 (eg: see RFC2817 about TLS).
950 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200951 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200952 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 +0100953 }
954 else if (h1s->flags & H1S_F_WANT_KAL) {
955 /* By default the client is in KAL mode. CLOSE mode mean
956 * it is imposed by the client itself. So only change
957 * KAL mode here. */
958 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
959 /* no length known or explicit close => close */
960 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200961 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 +0100962 }
963 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
964 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500965 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100966 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200967 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 +0100968 }
969 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200970 }
971 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100972 /* Input direction: first pass */
973 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
974 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200975 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200976 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 +0100977 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200978 }
979
980 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200981 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200982 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200983 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);
984 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200985}
986
987/* Deduce the connection mode of the client connection, depending on the
988 * configuration and the H1 message flags. This function is called twice, the
989 * first time when the request is parsed and the second time when the response
990 * is parsed.
991 */
992static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
993{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100994 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100995 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100996 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200997
Christopher Fauletf2824e62018-10-01 12:12:37 +0200998 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100999 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001000 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001001 h1s->status == 101) {
1002 /* Either we've established an explicit tunnel, or we're
1003 * switching the protocol. In both cases, we're very unlikely to
1004 * understand the next protocols. We have to switch to tunnel
1005 * mode, so that we transfer the request and responses then let
1006 * this protocol pass unmodified. When we later implement
1007 * specific parsers for such protocols, we'll want to check the
1008 * Upgrade header which contains information about that protocol
1009 * for responses with status 101 (eg: see RFC2817 about TLS).
1010 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001011 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001012 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 +01001013 }
1014 else if (h1s->flags & H1S_F_WANT_KAL) {
1015 /* By default the server is in KAL mode. CLOSE mode mean
1016 * it is imposed by haproxy itself. So only change KAL
1017 * mode here. */
1018 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1019 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1020 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1021 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001022 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 +01001023 }
1024 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001025 }
Christopher Faulet70033782018-12-05 13:50:11 +01001026 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001027 /* Output direction: first pass */
1028 if (h1m->flags & H1_MF_CONN_CLO) {
1029 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001030 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001031 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 +01001032 }
1033 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1034 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1035 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1036 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1037 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1038 /* no explicit keep-alive option httpclose/server-close => close */
1039 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001040 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 +01001041 }
Christopher Faulet70033782018-12-05 13:50:11 +01001042 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001043
1044 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001045 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001046 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001047 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);
1048 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049}
1050
Christopher Fauletb992af02019-03-28 15:42:24 +01001051static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001052{
1053 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001054
1055 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1056 * token is found
1057 */
1058 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001059 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001060
1061 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001062 if (!(h1m->flags & H1_MF_VER_11)) {
1063 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 +01001064 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001065 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001066 }
1067 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001068 if (h1m->flags & H1_MF_VER_11) {
1069 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001070 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001071 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001072 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001073}
1074
Christopher Fauletb992af02019-03-28 15:42:24 +01001075static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001076{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001077 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1078 * token is found
1079 */
1080 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001081 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001082
1083 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001084 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001085 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1086 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 +01001087 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001088 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001089 }
1090 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001091 if (h1m->flags & H1_MF_VER_11) {
1092 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001093 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001094 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001095 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001096}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001097
Christopher Fauletb992af02019-03-28 15:42:24 +01001098static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001099{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001100 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001101 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001102 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001103 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001104}
1105
Christopher Fauletb992af02019-03-28 15:42:24 +01001106static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1107{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001108 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001109 h1_set_cli_conn_mode(h1s, h1m);
1110 else
1111 h1_set_srv_conn_mode(h1s, h1m);
1112
1113 if (!(h1m->flags & H1_MF_RESP))
1114 h1_update_req_conn_value(h1s, h1m, conn_val);
1115 else
1116 h1_update_res_conn_value(h1s, h1m, conn_val);
1117}
Christopher Faulete44769b2018-11-29 23:01:45 +01001118
Christopher Faulet98fbe952019-07-22 16:18:24 +02001119/* Try to adjust the case of the message header name using the global map
1120 * <hdrs_map>.
1121 */
1122static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1123{
1124 struct ebpt_node *node;
1125 struct h1_hdr_entry *entry;
1126
1127 /* No entry in the map, do nothing */
1128 if (eb_is_empty(&hdrs_map.map))
1129 return;
1130
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001131 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001132 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1133 return;
1134
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001135 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001136 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1137 return;
1138
1139 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1140 if (!node)
1141 return;
1142 entry = container_of(node, struct h1_hdr_entry, node);
1143 name->ptr = entry->name.ptr;
1144 name->len = entry->name.len;
1145}
1146
Christopher Faulete44769b2018-11-29 23:01:45 +01001147/* Append the description of what is present in error snapshot <es> into <out>.
1148 * The description must be small enough to always fit in a buffer. The output
1149 * buffer may be the trash so the trash must not be used inside this function.
1150 */
1151static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1152{
1153 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001154 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1155 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1156 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1157 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1158 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1159 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001160}
1161/*
1162 * Capture a bad request or response and archive it in the proxy's structure.
1163 * By default it tries to report the error position as h1m->err_pos. However if
1164 * this one is not set, it will then report h1m->next, which is the last known
1165 * parsing point. The function is able to deal with wrapping buffers. It always
1166 * displays buffers as a contiguous area starting at buf->p. The direction is
1167 * determined thanks to the h1m's flags.
1168 */
1169static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1170 struct h1m *h1m, struct buffer *buf)
1171{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001172 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001173 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001174 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001175 union error_snapshot_ctx ctx;
1176
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001177 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001178 if (sess == NULL)
1179 sess = si_strm(h1s->cs->data)->sess;
1180 if (!(h1m->flags & H1_MF_RESP))
1181 other_end = si_strm(h1s->cs->data)->be;
1182 else
1183 other_end = sess->fe;
1184 } else
1185 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001186
1187 /* http-specific part now */
1188 ctx.h1.state = h1m->state;
1189 ctx.h1.c_flags = h1c->flags;
1190 ctx.h1.s_flags = h1s->flags;
1191 ctx.h1.m_flags = h1m->flags;
1192 ctx.h1.m_clen = h1m->curr_len;
1193 ctx.h1.m_blen = h1m->body_len;
1194
1195 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1196 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001197 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1198 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001199}
1200
Christopher Fauletadb22202018-12-12 10:32:09 +01001201/* Emit the chunksize followed by a CRLF in front of data of the buffer
1202 * <buf>. It goes backwards and starts with the byte before the buffer's
1203 * head. The caller is responsible for ensuring there is enough room left before
1204 * the buffer's head for the string.
1205 */
1206static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1207{
1208 char *beg, *end;
1209
1210 beg = end = b_head(buf);
1211 *--beg = '\n';
1212 *--beg = '\r';
1213 do {
1214 *--beg = hextab[chksz & 0xF];
1215 } while (chksz >>= 4);
1216 buf->head -= (end - beg);
1217 b_add(buf, end - beg);
1218}
1219
1220/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1221 * ensuring there is enough room left in the buffer for the string. */
1222static void h1_emit_chunk_crlf(struct buffer *buf)
1223{
1224 *(b_peek(buf, b_data(buf))) = '\r';
1225 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1226 b_add(buf, 2);
1227}
1228
Christopher Faulet129817b2018-09-20 16:14:40 +02001229/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001230 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001231 * CONNECT requests. On the client side, if the response is not finished, the
1232 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001233 */
1234static void h1_set_req_tunnel_mode(struct h1s *h1s)
1235{
1236 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1237 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001238 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1239
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001240 if (!(h1s->h1c->flags & H1C_F_IS_BACK)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001241 h1s->flags &= ~H1S_F_PARSING_DONE;
1242 if (h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001243 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1244 TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001245 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001246 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001247 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1248 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001249 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001250 TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001251 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001252}
1253
1254/*
1255 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001256 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001257 * last case, this function takes care to switch the request to tunnel mode if
1258 * possible. On the server side, if the request is not finished, the mux is mark
1259 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001260 */
1261static void h1_set_res_tunnel_mode(struct h1s *h1s)
1262{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001263
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001264 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1265 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001266 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1267
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001268 if (h1s->h1c->flags & H1C_F_IS_BACK) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001269 h1s->flags &= ~H1S_F_PARSING_DONE;
1270 /* On protocol switching, switch the request to tunnel mode if it is in
1271 * DONE state. Otherwise we will wait the end of the request to switch
1272 * it in tunnel mode.
1273 */
1274 if (h1s->req.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001275 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1276 TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001277 }
1278 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1279 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1280 h1s->req.state = H1_MSG_TUNNEL;
1281 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1282 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001283 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001284 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1285 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001286 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001287 TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001288 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001289}
1290
1291/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001292 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001293 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001294 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001296static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001297 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001298{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001299 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 int ret = 0;
1301
Willy Tarreau022e5e52020-09-10 09:33:15 +02001302 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 +02001303
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001304 if (h1s->meth == HTTP_METH_CONNECT)
1305 h1m->flags |= H1_MF_METH_CONNECT;
1306 if (h1s->meth == HTTP_METH_HEAD)
1307 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001308
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001309 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1310 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001311 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 +02001312 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001313 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001314 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 +02001315 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1316 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001317 goto end;
1318 }
1319
Christopher Faulet486498c2019-10-11 14:22:00 +02001320 if (h1m->err_pos >= 0) {
1321 /* Maybe we found an error during the parsing while we were
1322 * configured not to block on that, so we have to capture it
1323 * now.
1324 */
1325 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1326 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1327 }
1328
Christopher Faulet129817b2018-09-20 16:14:40 +02001329 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001330 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001331 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001332 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001333 }
1334 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001335 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001336 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001337 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001338 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001339 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001340 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001341
Christopher Faulet129817b2018-09-20 16:14:40 +02001342 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001343 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 +02001344 return ret;
1345}
1346
1347/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001348 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001349 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1350 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001351 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001352static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001353 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001354 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001355{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001356 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001357
Willy Tarreau022e5e52020-09-10 09:33:15 +02001358 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 +02001359 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001360 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001361 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 +01001362 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001363 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001364 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 +02001365 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001366 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001367 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001368 }
1369
Christopher Faulet02a02532019-11-15 09:36:28 +01001370 *ofs += ret;
1371
1372 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001373 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 +02001374 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001375}
1376
1377/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001378 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1379 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1380 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1381 * responsible to update the parser state <h1m>.
1382 */
1383static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1384 struct buffer *buf, size_t *ofs, size_t max)
1385{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001386 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001387
Willy Tarreau022e5e52020-09-10 09:33:15 +02001388 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 +02001389 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001390 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001391 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 +01001392 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001393 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001394 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 +02001395 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1396 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001397 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001398 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001399
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001400 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001401
1402 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001403 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 +02001404 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001405}
1406
1407/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001408 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
Christopher Fauleta583af62020-09-24 15:35:37 +02001409 * proceed. This functions is responsible to update the parser state <h1m>.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001410 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001411static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1412 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001413{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001414 int ret;
1415
Willy Tarreau022e5e52020-09-10 09:33:15 +02001416 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet76014fd2019-12-10 11:47:22 +01001417 ret = h1_parse_msg_eom(h1m, htx, max);
1418 if (!ret) {
1419 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1420 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001421 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001422 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001423 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1424 }
1425 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001426 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001427
Christopher Faulet76014fd2019-12-10 11:47:22 +01001428 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001429 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001430 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet76014fd2019-12-10 11:47:22 +01001431 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001432}
1433
1434/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001435 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001436 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1437 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001438 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001439static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001440{
Christopher Faulet539e0292018-11-19 10:40:09 +01001441 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001442 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001443 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001444 size_t data;
1445 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001446 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001447
Christopher Faulet539e0292018-11-19 10:40:09 +01001448 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001449 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001450
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001451 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001452 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001453
Christopher Faulet2eed8002020-12-07 11:26:13 +01001454 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001455 goto end;
1456
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001457 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001458 size_t used = htx_used_space(htx);
1459
Christopher Faulet129817b2018-09-20 16:14:40 +02001460 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001461 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001462 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001463 if (!ret)
1464 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001465
1466 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1467 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1468
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001469 /* Reject Protocol upgrade request with payload */
1470 if ((h1m->flags & (H1_MF_RESP|H1_MF_CONN_UPG)) == H1_MF_CONN_UPG && h1m->state != H1_MSG_DONE) {
1471 h1s->flags |= H1S_F_NOT_IMPL_ERROR;
1472 TRACE_USER("Upgrade with body not implemented, reject H1 message",
1473 H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1474 break;
1475 }
1476
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001477 if ((h1m->flags & H1_MF_RESP) &&
1478 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1479 h1m_init_res(&h1s->res);
1480 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001481 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001482 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001483 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001484 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001485 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001486 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001487 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001488 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001489
1490 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1491 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001492 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001493 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001494 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1495 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1496 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001497 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001498
Christopher Faulet76014fd2019-12-10 11:47:22 +01001499 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1500 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001501 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001502 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001503 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1504 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1505 break;
1506
1507 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1508 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1509 }
1510
Christopher Fauletf3158e92019-11-15 11:14:23 +01001511 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1512 h1_set_req_tunnel_mode(h1s);
1513 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001514 h1c->flags |= H1C_F_WAIT_OPPOSITE;
1515 TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
Christopher Faulet23021ad2020-07-10 10:01:26 +02001516 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001517 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001518 else
1519 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001520 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001521 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001522 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001523 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001524 if (!ret)
1525 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001526
1527 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1528 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001529 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001530 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001531 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001532 break;
1533 }
1534
Christopher Faulet30db3d72019-05-17 15:35:33 +02001535 count -= htx_used_space(htx) - used;
Christopher Faulet2eed8002020-12-07 11:26:13 +01001536 } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)));
Christopher Faulet129817b2018-09-20 16:14:40 +02001537
Christopher Faulet2eed8002020-12-07 11:26:13 +01001538 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
1539 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 +02001540 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001541 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001542
Christopher Faulet539e0292018-11-19 10:40:09 +01001543 b_del(&h1c->ibuf, total);
1544
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001545 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001546 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1547
Christopher Faulet30db3d72019-05-17 15:35:33 +02001548 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001549 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001550 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001551 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 +01001552 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001553 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001554
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001555 if (!b_data(&h1c->ibuf))
1556 h1_release_buf(h1c, &h1c->ibuf);
1557
1558 if (!h1s->cs) {
1559 if (h1m->state <= H1_MSG_LAST_LF) {
1560 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1561 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1562 goto end;
1563 }
1564
1565 if (!h1s_new_cs(h1s, buf)) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001566 h1c->flags |= H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001567 goto err;
1568 }
1569 }
1570
1571 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001572 if (!(h1m->flags & H1_MF_CHNK) &&
1573 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1574 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1575 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1576 }
1577 else {
1578 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1579 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1580 }
1581
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001582 /* Don't set EOI on the conn-stream for protocol upgrade requests, wait
1583 * the response to do so or not depending on the status code.
1584 */
1585 if ((h1s->flags & H1S_F_PARSING_DONE) && !(h1m->flags & H1_MF_CONN_UPG))
Christopher Fauleta583af62020-09-24 15:35:37 +02001586 h1s->cs->flags |= CS_FL_EOI;
1587
Christopher Faulet6716cc22019-12-20 17:33:24 +01001588 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001589 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001590 else {
1591 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1592 if (h1s->flags & H1S_F_REOS) {
1593 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001594 if (h1m->state >= H1_MSG_DONE) {
1595 /* DONE or TUNNEL, set EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001596 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001597 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001598 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
1599 h1s->cs->flags |= CS_FL_ERROR;
1600 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001601 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001602
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001603 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001604 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001605 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001606
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001607 err:
Christopher Faulet47365272018-10-31 17:40:50 +01001608 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001609 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001610 if (h1s->cs)
1611 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001612 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001613 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001614}
1615
Christopher Faulet129817b2018-09-20 16:14:40 +02001616/*
1617 * Process outgoing data. It parses data and transfer them from the channel buffer into
1618 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1619 * 0 if it couldn't proceed.
1620 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001621static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1622{
Christopher Faulet129817b2018-09-20 16:14:40 +02001623 struct h1s *h1s = h1c->h1s;
1624 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001625 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001626 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001627 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001628 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001629
Christopher Faulet47365272018-10-31 17:40:50 +01001630 if (!count)
1631 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001632
Christopher Faulet94b2c762019-05-24 15:28:57 +02001633 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001634 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1635
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001636 if (htx_is_empty(chn_htx))
1637 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001638
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001639 if (h1s->flags & H1S_F_PROCESSING_ERROR)
1640 goto end;
1641
Christopher Faulet51dbc942018-09-13 09:05:15 +02001642 if (!h1_get_buf(h1c, &h1c->obuf)) {
1643 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001644 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 +02001645 goto end;
1646 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001647
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001648 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001649
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001650 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001651 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001652
Willy Tarreau3815b222018-12-11 19:50:43 +01001653 /* Perform some optimizations to reduce the number of buffer copies.
1654 * First, if the mux's buffer is empty and the htx area contains
1655 * exactly one data block of the same size as the requested count,
1656 * then it's possible to simply swap the caller's buffer with the
1657 * mux's output buffer and adjust offsets and length to match the
1658 * entire DATA HTX block in the middle. In this case we perform a
1659 * true zero-copy operation from end-to-end. This is the situation
1660 * that happens all the time with large files. Second, if this is not
1661 * possible, but the mux's output buffer is empty, we still have an
1662 * opportunity to avoid the copy to the intermediary buffer, by making
1663 * the intermediary buffer's area point to the output buffer's area.
1664 * In this case we want to skip the HTX header to make sure that copies
1665 * remain aligned and that this operation remains possible all the
1666 * time. This goes for headers, data blocks and any data extracted from
1667 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001668 */
1669 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001670 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001671 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001672 htx_get_blk_value(chn_htx, blk).len == count) {
1673 void *old_area = h1c->obuf.area;
1674
Christopher Faulet6b81df72019-10-01 22:08:43 +02001675 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001676 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001677 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001678 h1c->obuf.data = count;
1679
1680 buf->area = old_area;
1681 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001682
Christopher Faulet6b81df72019-10-01 22:08:43 +02001683 chn_htx = (struct htx *)buf->area;
1684 htx_reset(chn_htx);
1685
Christopher Fauletadb22202018-12-12 10:32:09 +01001686 /* The message is chunked. We need to emit the chunk
1687 * size. We have at least the size of the struct htx to
1688 * write the chunk envelope. It should be enough.
1689 */
1690 if (h1m->flags & H1_MF_CHNK) {
1691 h1_emit_chunk_size(&h1c->obuf, count);
1692 h1_emit_chunk_crlf(&h1c->obuf);
1693 }
1694
Willy Tarreau3815b222018-12-11 19:50:43 +01001695 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001696 if (h1m->state == H1_MSG_DATA)
1697 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001698 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001699 else
1700 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001701 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001702 goto out;
1703 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001704 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001705 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001706 else
1707 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001708
Christopher Fauletc2518a52019-06-25 21:41:02 +02001709 tmp.data = 0;
1710 tmp.size = b_room(&h1c->obuf);
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001711 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001712 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001713 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001714 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001715 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001716 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001717
Christopher Fauletb2e84162018-12-06 11:39:49 +01001718 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001719 if (type != HTX_BLK_DATA && vlen > count)
1720 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001721
Christopher Faulet94b2c762019-05-24 15:28:57 +02001722 if (type == HTX_BLK_UNUSED)
1723 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001724
Christopher Faulet94b2c762019-05-24 15:28:57 +02001725 switch (h1m->state) {
1726 case H1_MSG_RQBEFORE:
1727 if (type != HTX_BLK_REQ_SL)
1728 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001729 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 +02001730 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001731 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001732 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001733 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001734 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001735 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001736 if (sl->flags & HTX_SL_F_BODYLESS)
1737 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001738 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet089acd52020-09-21 10:57:52 +02001739 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
1740 h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
1741 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1742 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1743 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001744 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001745
Christopher Faulet94b2c762019-05-24 15:28:57 +02001746 case H1_MSG_RPBEFORE:
1747 if (type != HTX_BLK_RES_SL)
1748 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001749 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 +02001750 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001751 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001752 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001753 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001754 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001755 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001756 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001757 if (sl->info.res.status < 200 &&
1758 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001759 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001760 h1m->state = H1_MSG_HDR_FIRST;
1761 break;
1762
Christopher Faulet94b2c762019-05-24 15:28:57 +02001763 case H1_MSG_HDR_FIRST:
1764 case H1_MSG_HDR_NAME:
1765 case H1_MSG_HDR_L2_LWS:
1766 if (type == HTX_BLK_EOH)
1767 goto last_lf;
1768 if (type != HTX_BLK_HDR)
1769 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001770
Christopher Faulet9768c262018-10-22 09:34:31 +02001771 h1m->state = H1_MSG_HDR_NAME;
1772 n = htx_get_blk_name(chn_htx, blk);
1773 v = htx_get_blk_value(chn_htx, blk);
1774
Christopher Faulet86d144c2019-08-14 16:32:25 +02001775 /* Skip all pseudo-headers */
1776 if (*(n.ptr) == ':')
1777 goto skip_hdr;
1778
Christopher Fauletb045bb22020-02-28 10:42:20 +01001779 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001780 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001781 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001782 /* Only skip C-L header with invalid value. */
1783 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001784 goto skip_hdr;
1785 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001786 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001787 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001788 if (!v.len)
1789 goto skip_hdr;
1790 }
1791
Christopher Faulet67d58092019-10-02 10:51:38 +02001792 /* Skip header if same name is used to add the server name */
1793 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1794 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1795 goto skip_hdr;
1796
Christopher Faulet98fbe952019-07-22 16:18:24 +02001797 /* Try to adjust the case of the header name */
1798 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1799 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001800 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001801 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001802 skip_hdr:
1803 h1m->state = H1_MSG_HDR_L2_LWS;
1804 break;
1805
Christopher Faulet94b2c762019-05-24 15:28:57 +02001806 case H1_MSG_LAST_LF:
1807 if (type != HTX_BLK_EOH)
1808 goto error;
1809 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001810 h1m->state = H1_MSG_LAST_LF;
1811 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001812 /* If the reply comes from haproxy while the request is
1813 * not finished, we force the connection close. */
1814 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1815 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1816 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1817 }
1818
1819 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001820 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001821 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001822 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001823 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001824 /* Try to adjust the case of the header name */
1825 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1826 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001827 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001828 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001829 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001830 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001831 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001832
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001833 if ((h1s->meth != HTTP_METH_CONNECT &&
1834 (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 +01001835 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Fauletc75668e2020-12-07 18:10:32 +01001836 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && h1s->meth != HTTP_METH_HEAD &&
1837 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001838 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1839 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001840 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001841 n = ist("transfer-encoding");
1842 v = ist("chunked");
1843 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1844 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001845 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001846 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001847 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001848 h1m->flags |= H1_MF_CHNK;
1849 }
1850
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001851 /* Now add the server name to a header (if requested) */
1852 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1853 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1854 struct server *srv = objt_server(h1c->conn->target);
1855
1856 if (srv) {
1857 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1858 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001859
1860 /* 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 Faulet72ba6cd2019-09-24 16:20:05 +02001865 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001866 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001867 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1868 }
1869
Christopher Fauletc2518a52019-06-25 21:41:02 +02001870 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001871 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001872
Christopher Faulet6b81df72019-10-01 22:08:43 +02001873 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1874 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1875
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001876 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1877 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1878 h1_set_req_tunnel_mode(h1s);
1879 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001880 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001881 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001882 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001883 * to the client. Switch the response to tunnel mode.
1884 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001885 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001886 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001887 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001888 else if ((h1m->flags & H1_MF_RESP) &&
1889 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1890 h1m_init_res(&h1s->res);
1891 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001892 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001893 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001894 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001895 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001896 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001897 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1898 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001899 else
1900 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001901 break;
1902
Christopher Faulet94b2c762019-05-24 15:28:57 +02001903 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001904 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001905 if (type == HTX_BLK_EOM) {
1906 /* Chunked message without explicit trailers */
1907 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001908 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001909 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001910 }
1911 goto done;
1912 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001913 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001914 /* If the message is not chunked, never
1915 * add the last chunk. */
1916 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001917 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001918 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 +02001919 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001920 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001921 else if (type != HTX_BLK_DATA)
1922 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001923
1924 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 +02001925
1926
1927 if (vlen > count) {
1928 /* Get the maximum amount of data we can xferred */
1929 vlen = count;
1930 }
1931
1932 chklen = 0;
1933 if (h1m->flags & H1_MF_CHNK) {
1934 chklen = b_room(&tmp);
1935 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1936 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1937 (chklen < 1048576) ? 5 : 8);
1938 chklen += 4; /* 2 x CRLF */
1939 }
1940
1941 if (vlen + chklen > b_room(&tmp)) {
1942 /* too large for the buffer */
1943 if (chklen >= b_room(&tmp))
1944 goto full;
1945 vlen = b_room(&tmp) - chklen;
1946 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001947 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001948 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001949 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001950 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001951
1952 if (h1m->state == H1_MSG_DATA)
1953 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001954 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001955 else
1956 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001957 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001958 break;
1959
Christopher Faulet94b2c762019-05-24 15:28:57 +02001960 case H1_MSG_TRAILERS:
1961 if (type == HTX_BLK_EOM)
1962 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001963 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001964 goto error;
1965 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001966 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001967 /* If the message is not chunked, ignore
1968 * trailers. It may happen with H2 messages. */
1969 if (!(h1m->flags & H1_MF_CHNK))
1970 break;
1971
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001972 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001973 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001974 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001975 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1976 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001977 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001978 else { // HTX_BLK_TLR
1979 n = htx_get_blk_name(chn_htx, blk);
1980 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001981
1982 /* Try to adjust the case of the header name */
1983 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1984 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001985 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001986 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001987 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001988 break;
1989
Christopher Faulet94b2c762019-05-24 15:28:57 +02001990 case H1_MSG_DONE:
1991 if (type != HTX_BLK_EOM)
1992 goto error;
1993 done:
1994 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001995 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1996 h1_set_req_tunnel_mode(h1s);
1997 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1998 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001999 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
2000 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002001 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02002002 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 +02002003 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002004
2005 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2006 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002007 break;
2008
Christopher Faulet9768c262018-10-22 09:34:31 +02002009 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002010 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002011 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002012 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002013 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002014 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002015 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2016 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002017 break;
2018 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002019
2020 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002021 total += vlen;
2022 count -= vlen;
2023 if (sz == vlen)
2024 blk = htx_remove_blk(chn_htx, blk);
2025 else {
2026 htx_cut_data_blk(chn_htx, blk, vlen);
2027 break;
2028 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002029 }
2030
Christopher Faulet9768c262018-10-22 09:34:31 +02002031 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002032 /* when the output buffer is empty, tmp shares the same area so that we
2033 * only have to update pointers and lengths.
2034 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002035 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2036 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002037 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002038 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002039
Willy Tarreau3815b222018-12-11 19:50:43 +01002040 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002041 out:
Christopher Faulet3e1748b2020-12-07 18:21:27 +01002042 /* Both the request and the response reached the DONE state. So set EOI
2043 * flag on the conn-stream. Most of time, the flag will already be set,
2044 * except for protocol upgrades.
2045 */
2046 if (h1s->cs && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2047 h1s->cs->flags |= CS_FL_EOI;
2048
Christopher Faulet6b81df72019-10-01 22:08:43 +02002049 if (!buf_room_for_htx_data(&h1c->obuf)) {
2050 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002051 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002052 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002053 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002054 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002055 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002056
2057 full:
2058 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2059 h1c->flags |= H1C_F_OUT_FULL;
2060 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002061}
2062
Christopher Faulet51dbc942018-09-13 09:05:15 +02002063/*********************************************************/
2064/* functions below are I/O callbacks from the connection */
2065/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002066static void h1_wake_stream_for_recv(struct h1s *h1s)
2067{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002068 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002069 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002070 tasklet_wakeup(h1s->subs->tasklet);
2071 h1s->subs->events &= ~SUB_RETRY_RECV;
2072 if (!h1s->subs->events)
2073 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002074 }
2075}
2076static void h1_wake_stream_for_send(struct h1s *h1s)
2077{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002078 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002079 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002080 tasklet_wakeup(h1s->subs->tasklet);
2081 h1s->subs->events &= ~SUB_RETRY_SEND;
2082 if (!h1s->subs->events)
2083 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002084 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002085}
2086
2087/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2088 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2089 * retryable errors (allocation error or buffer full). On success, the error is
2090 * copied in the output buffer.
2091*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002092static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002093{
2094 int rc = http_get_status_idx(h1c->errcode);
2095 int ret = 0;
2096
2097 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2098
2099 /* Verify if the error is mapped on /dev/null or any empty file */
2100 /// XXX: do a function !
2101 if (h1c->px->replies[rc] &&
2102 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2103 h1c->px->replies[rc]->body.errmsg &&
2104 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2105 /* Empty error, so claim a success */
2106 ret = 1;
2107 goto out;
2108 }
2109
2110 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2111 h1c->flags |= H1C_F_ERR_PENDING;
2112 goto out;
2113 }
2114
2115 if (!h1_get_buf(h1c, &h1c->obuf)) {
2116 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2117 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2118 goto out;
2119 }
2120 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2121 if (unlikely(ret <= 0)) {
2122 if (!ret) {
2123 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2124 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2125 goto out;
2126 }
2127 else {
2128 /* we cannot report this error, so claim a success */
2129 ret = 1;
2130 }
2131 }
2132 h1c->flags &= ~H1C_F_ERR_PENDING;
2133 out:
2134 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2135 return ret;
2136}
2137
2138/* Try to send a 500 internal error. It relies on h1_send_error to send the
2139 * error. This function takes care of incrementing stats and tracked counters.
2140 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002141static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002142{
2143 struct session *sess = h1c->conn->owner;
2144 int ret = 1;
2145
2146 session_inc_http_req_ctr(sess);
2147 session_inc_http_err_ctr(sess);
2148 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2149 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[5], 1);
2150 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
2151 if (sess->listener->counters)
2152 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
2153
2154 h1c->errcode = 500;
2155 ret = h1_send_error(h1c);
2156 sess_log(sess);
2157 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002158}
2159
Christopher Fauletc18fc232020-10-06 17:41:36 +02002160/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2161 * error. This function takes care of incrementing stats and tracked counters.
2162 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002163static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002164{
2165 struct session *sess = h1c->conn->owner;
2166 int ret = 1;
2167
2168 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2169 goto end;
2170
2171 session_inc_http_req_ctr(sess);
2172 session_inc_http_err_ctr(sess);
2173 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2174 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2175 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2176 if (sess->listener->counters)
2177 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2178
2179 h1c->errcode = 400;
2180 ret = h1_send_error(h1c);
2181 sess_log(sess);
2182
2183 end:
2184 return ret;
2185}
2186
Christopher Faulet2eed8002020-12-07 11:26:13 +01002187/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2188 * the error. This function takes care of incrementing stats and tracked
2189 * counters.
2190 */
2191static int h1_handle_not_impl_err(struct h1c *h1c)
2192{
2193 struct session *sess = h1c->conn->owner;
2194 int ret = 1;
2195
2196 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2197 goto end;
2198
2199 session_inc_http_req_ctr(sess);
2200 session_inc_http_err_ctr(sess);
2201 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2202 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2203 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2204 if (sess->listener->counters)
2205 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2206
2207 h1c->errcode = 501;
2208 ret = h1_send_error(h1c);
2209 sess_log(sess);
2210
2211 end:
2212 return ret;
2213}
2214
Christopher Fauletc18fc232020-10-06 17:41:36 +02002215/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2216 * error. This function takes care of incrementing stats and tracked counters.
2217 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002218static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002219{
2220 struct session *sess = h1c->conn->owner;
2221 int ret = 1;
2222
2223 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2224 goto end;
2225
2226 session_inc_http_req_ctr(sess);
2227 session_inc_http_err_ctr(sess);
2228 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2229 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2230 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2231 if (sess->listener->counters)
2232 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2233
2234 h1c->errcode = 408;
2235 ret = h1_send_error(h1c);
2236 sess_log(sess);
2237 end:
2238 return ret;
2239}
2240
2241
Christopher Faulet51dbc942018-09-13 09:05:15 +02002242/*
2243 * Attempt to read data, and subscribe if none available
2244 */
2245static int h1_recv(struct h1c *h1c)
2246{
2247 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002248 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002249 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002250
Christopher Faulet6b81df72019-10-01 22:08:43 +02002251 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2252
2253 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2254 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002255 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002256 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002257
Christopher Fauletae635762020-09-21 11:47:16 +02002258 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2259 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002260 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002261 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002262
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002263 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2264 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002265 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002266 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002267 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002268
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002269 /*
2270 * If we only have a small amount of data, realign it,
2271 * it's probably cheaper than doing 2 recv() calls.
2272 */
2273 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2274 b_slow_realign(&h1c->ibuf, trash.area, 0);
2275
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002276 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002277 if (!h1c->h1s ||
2278 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2279 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002280 flags |= CO_RFL_READ_ONCE;
2281
Willy Tarreau45f2b892018-12-05 07:59:27 +01002282 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002283 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002284 if (h1c->flags & H1C_F_IN_FULL) {
2285 h1c->flags &= ~H1C_F_IN_FULL;
2286 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2287 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002288
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002289 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002290 if (!b_data(&h1c->ibuf)) {
2291 /* try to pre-align the buffer like the rxbufs will be
2292 * to optimize memory copies.
2293 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002294 h1c->ibuf.head = sizeof(struct htx);
2295 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002296 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002297 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002298 if (max && !ret && h1_recv_allowed(h1c)) {
2299 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2300 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002301 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002302 else {
2303 h1_wake_stream_for_recv(h1c->h1s);
2304 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002305 }
2306
Christopher Faulet51dbc942018-09-13 09:05:15 +02002307 if (!b_data(&h1c->ibuf))
2308 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002309 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002310 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002311 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2312 }
2313
2314 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002315 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002316}
2317
2318
2319/*
2320 * Try to send data if possible
2321 */
2322static int h1_send(struct h1c *h1c)
2323{
2324 struct connection *conn = h1c->conn;
2325 unsigned int flags = 0;
2326 size_t ret;
2327 int sent = 0;
2328
Christopher Faulet6b81df72019-10-01 22:08:43 +02002329 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2330
2331 if (conn->flags & CO_FL_ERROR) {
2332 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002333 b_reset(&h1c->obuf);
2334 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002335 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002336
2337 if (!b_data(&h1c->obuf))
2338 goto end;
2339
Christopher Faulet46230362019-10-17 16:04:20 +02002340 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002341 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002342 if (h1c->flags & H1C_F_CO_STREAMER)
2343 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002344
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002345 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002346 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002347 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002348 if (h1c->flags & H1C_F_OUT_FULL) {
2349 h1c->flags &= ~H1C_F_OUT_FULL;
2350 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2351 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002352 b_del(&h1c->obuf, ret);
2353 sent = 1;
2354 }
2355
Christopher Faulet145aa472018-12-06 10:56:20 +01002356 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002357 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002358 /* error or output closed, nothing to send, clear the buffer to release it */
2359 b_reset(&h1c->obuf);
2360 }
2361
Christopher Faulet51dbc942018-09-13 09:05:15 +02002362 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002363 if (!(h1c->flags & H1C_F_OUT_FULL))
2364 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002365
Christopher Faulet51dbc942018-09-13 09:05:15 +02002366 /* We're done, no more to send */
2367 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002368 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002370 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002371 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002372 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002373 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002375 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2376 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002377 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002378 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002379
Christopher Faulet6b81df72019-10-01 22:08:43 +02002380 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002381 return sent;
2382}
2383
Christopher Faulet51dbc942018-09-13 09:05:15 +02002384/* callback called on any event by the connection handler.
2385 * It applies changes and returns zero, or < 0 if it wants immediate
2386 * destruction of the connection.
2387 */
2388static int h1_process(struct h1c * h1c)
2389{
2390 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002391 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002392
Christopher Faulet6b81df72019-10-01 22:08:43 +02002393 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2394
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002395 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
2396 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002397 (h1c->flags & (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC)) && /* IDLE h1 connection or no CS attached to the h1 stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002398 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
2399 struct buffer *buf;
2400 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002401
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002402 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2403 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002404 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002405
2406 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002407 if (((h1c->flags & (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) == H1C_F_ST_IDLE) && /* First request with no h1s */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002408 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE)) { /* H2 upgrade supported by the proxy */
2409 /* Try to match H2 preface before parsing the request headers. */
2410 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2411 h1c->flags |= H1C_F_UPG_H2C;
2412 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002413 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002414 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002415 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002416
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002417 /* Create the H1 stream if not already there */
2418 if (!h1s) {
2419 h1s = h1c_frt_stream_new(h1c);
2420 if (!h1s) {
2421 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002422 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002423 goto no_parsing;
2424 }
2425 }
2426
2427 if (h1s->sess->t_idle == -1)
2428 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2429
2430 /* Get the stream rxbuf */
2431 buf = h1_get_buf(h1c, &h1s->rxbuf);
2432 if (!buf) {
2433 h1c->flags |= H1C_F_IN_SALLOC;
2434 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2435 return 0;
2436 }
2437
2438 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2439 h1_process_input(h1c, buf, count);
2440 h1_release_buf(h1c, &h1s->rxbuf);
2441 h1_set_idle_expiration(h1c);
2442
2443 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002444 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002445 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002446 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002447 }
2448 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2449 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002450 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002451 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002452 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2453 h1_handle_not_impl_err(h1c);
2454 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
2455 }
Christopher Fauletae635762020-09-21 11:47:16 +02002456 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002457 h1_send(h1c);
2458
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002459 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
2460 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002461 /* No conn-stream */
2462 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002463 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002464 if (h1_handle_bad_req(h1c))
2465 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002466 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002467 }
2468
2469 /* Handle pending error, if any (only possible on frontend connection) */
2470 if (h1c->flags & H1C_F_ERR_PENDING) {
2471 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2472 if (h1_send_error(h1c))
2473 h1_send(h1c);
2474 }
Christopher Fauletae635762020-09-21 11:47:16 +02002475
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002476 /* If there is some pending outgoing data or error, just wait */
2477 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2478 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002479
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002480 /* Otherwise we can release the H1 connection */
2481 goto release;
2482 }
2483 else {
2484 /* Here there is still a H1 stream with a conn-stream.
2485 * Report the connection state at the stream level
2486 */
2487 if (conn_xprt_read0_pending(conn)) {
2488 h1s->flags |= H1S_F_REOS;
2489 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2490 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002491 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002492 h1s->cs->flags |= CS_FL_ERROR;
2493 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
2494 if (h1s->cs->data_cb->wake) {
2495 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
2496 h1s->cs->data_cb->wake(h1s->cs);
2497 }
2498 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002499 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002500
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002501 if (!b_data(&h1c->ibuf))
2502 h1_release_buf(h1c, &h1c->ibuf);
2503
2504
2505 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2506 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2507 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002508 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002509
Christopher Faulet47365272018-10-31 17:40:50 +01002510 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002511 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002512 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002513 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002514
2515 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002516 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002517 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002518 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002519}
2520
Willy Tarreau691d5032021-01-20 14:55:01 +01002521struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002522{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002523 struct connection *conn;
2524 struct tasklet *tl = (struct tasklet *)t;
2525 int conn_in_list;
2526 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002527 int ret = 0;
2528
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002529
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002530 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002531 if (tl->context == NULL) {
2532 /* The connection has been taken over by another thread,
2533 * we're no longer responsible for it, so just free the
2534 * tasklet, and do nothing.
2535 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002536 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002537 tasklet_free(tl);
2538 return NULL;
2539 }
2540 h1c = ctx;
2541 conn = h1c->conn;
2542
2543 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2544
2545 /* Remove the connection from the list, to be sure nobody attempts
2546 * to use it while we handle the I/O events
2547 */
2548 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2549 if (conn_in_list)
2550 MT_LIST_DEL(&conn->list);
2551
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002552 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002553
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002554 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002555 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002556 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002557 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002558 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002559 ret = h1_process(h1c);
2560 /* If we were in an idle list, we want to add it back into it,
2561 * unless h1_process() returned -1, which mean it has destroyed
2562 * the connection (testing !ret is enough, if h1_process() wasn't
2563 * called then ret will be 0 anyway.
2564 */
2565 if (!ret && conn_in_list) {
2566 struct server *srv = objt_server(conn->target);
2567
2568 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002569 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002570 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002571 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002572 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002573 return NULL;
2574}
2575
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002576static void h1_reset(struct connection *conn)
2577{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002578
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002579}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002580
2581static int h1_wake(struct connection *conn)
2582{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002583 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002584 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002585
Christopher Faulet6b81df72019-10-01 22:08:43 +02002586 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2587
Christopher Faulet539e0292018-11-19 10:40:09 +01002588 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002589 ret = h1_process(h1c);
2590 if (ret == 0) {
2591 struct h1s *h1s = h1c->h1s;
2592
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002593 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data_cb->wake) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002594 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002595 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002596 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002597 }
2598 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002599}
2600
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002601/* Connection timeout management. The principle is that if there's no receipt
2602 * nor sending for a certain amount of time, the connection is closed.
2603 */
2604static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2605{
2606 struct h1c *h1c = context;
2607 int expired = tick_is_expired(t->expire, now_ms);
2608
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002609 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002610
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002611 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002612 /* Make sure nobody stole the connection from us */
2613 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2614
2615 /* Somebody already stole the connection from us, so we should not
2616 * free it, we just have to free the task.
2617 */
2618 if (!t->context) {
2619 h1c = NULL;
2620 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2621 goto do_leave;
2622 }
2623
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002624 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002625 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2626 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002627 return t;
2628 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002629
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002630 /* If a conn-stream is still attached to the mux, wait for the
2631 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002632 */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002633 if (h1c->flags & H1C_F_ST_ATTACHED) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002634 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2635 t->expire = TICK_ETERNITY;
2636 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2637 return t;
2638 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002639
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002640 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002641 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2642 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002643 if (h1_handle_req_tout(h1c))
2644 h1_send(h1c);
2645 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2646 h1_refresh_timeout(h1c);
Christopher Fauletcc043f62020-12-14 10:06:12 +01002647 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002648 return t;
2649 }
2650 }
2651
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002652 /* We're about to destroy the connection, so make sure nobody attempts
2653 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002654 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002655 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002656 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002657
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002658 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002659 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002660
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002661 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002662 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002663
2664 if (!h1c) {
2665 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002666 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002667 return NULL;
2668 }
2669
2670 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002671 h1_release(h1c);
2672 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002673 return NULL;
2674}
2675
Christopher Faulet51dbc942018-09-13 09:05:15 +02002676/*******************************************/
2677/* functions below are used by the streams */
2678/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002679
Christopher Faulet51dbc942018-09-13 09:05:15 +02002680/*
2681 * Attach a new stream to a connection
2682 * (Used for outgoing connections)
2683 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002684static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002685{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002686 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002687 struct conn_stream *cs = NULL;
2688 struct h1s *h1s;
2689
Christopher Faulet6b81df72019-10-01 22:08:43 +02002690 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002691 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002692 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 +02002693 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002694 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002695
Christopher Faulet236c93b2020-07-02 09:19:54 +02002696 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002697 if (!cs) {
2698 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 +02002699 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002700 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002701
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002702 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002703 if (h1s == NULL) {
2704 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 +02002705 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002706 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002707
Christopher Faulet6b81df72019-10-01 22:08:43 +02002708 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002709 return cs;
2710 end:
2711 cs_free(cs);
2712 return NULL;
2713}
2714
2715/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2716 * this mux, it's easy as we can only store a single conn_stream.
2717 */
2718static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2719{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002720 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002721 struct h1s *h1s = h1c->h1s;
2722
2723 if (h1s)
2724 return h1s->cs;
2725
2726 return NULL;
2727}
2728
Christopher Faulet73c12072019-04-08 11:23:22 +02002729static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002730{
Christopher Faulet73c12072019-04-08 11:23:22 +02002731 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002732
Christopher Faulet6b81df72019-10-01 22:08:43 +02002733 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002734 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002735 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002736}
2737
2738/*
2739 * Detach the stream from the connection and possibly release the connection.
2740 */
2741static void h1_detach(struct conn_stream *cs)
2742{
2743 struct h1s *h1s = cs->ctx;
2744 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002745 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002746 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002747
Christopher Faulet6b81df72019-10-01 22:08:43 +02002748 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2749
Christopher Faulet51dbc942018-09-13 09:05:15 +02002750 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002751 if (!h1s) {
2752 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002753 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002754 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002755
Olivier Houchardf502aca2018-12-14 19:42:40 +01002756 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002757 h1c = h1s->h1c;
2758 h1s->cs = NULL;
2759
Christopher Faulet42849b02020-10-05 11:35:17 +02002760 sess->accept_date = date;
2761 sess->tv_accept = now;
2762 sess->t_handshake = 0;
2763 sess->t_idle = -1;
2764
Olivier Houchard8a786902018-12-15 16:05:40 +01002765 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2766 h1s_destroy(h1s);
2767
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002768 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 +02002769 /* If there are any excess server data in the input buffer,
2770 * release it and close the connection ASAP (some data may
2771 * remain in the output buffer). This happens if a server sends
2772 * invalid responses. So in such case, we don't want to reuse
2773 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002774 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002775 if (b_data(&h1c->ibuf)) {
2776 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002777 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002778 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002779 goto release;
2780 }
Christopher Faulet03627242019-07-19 11:34:08 +02002781
Christopher Faulet08016ab2020-07-01 16:10:06 +02002782 if (h1c->conn->flags & CO_FL_PRIVATE) {
2783 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002784 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2785 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002786 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002787 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002788 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002789 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002790 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002791 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002792 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2793 goto end;
2794 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002795 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002796 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002797 if (h1c->conn->owner == sess)
2798 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002799 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002800 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002801 /* The server doesn't want it, let's kill the connection right away */
2802 h1c->conn->mux->destroy(h1c);
2803 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2804 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002805 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002806 /* At this point, the connection has been added to the
2807 * server idle list, so another thread may already have
2808 * hijacked it, so we can't do anything with it.
2809 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002810 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002811 }
2812 }
2813
Christopher Fauletf1204b82019-07-19 14:51:06 +02002814 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002815 /* 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 +01002816 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002817 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002818 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002819 !h1c->conn->owner) {
2820 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002821 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002822 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002823 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002824 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002825 /* If we have a new request, process it immediately or
2826 * subscribe for reads waiting for new data
2827 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01002828 if (unlikely(b_data(&h1c->ibuf))) {
2829 if (h1_process(h1c) == -1)
2830 goto end;
2831 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002832 else
2833 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2834 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002835 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002836 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002837 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002838 end:
2839 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002840}
2841
2842
2843static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2844{
2845 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002846 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002847
2848 if (!h1s)
2849 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002850 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002851
Christopher Faulet6b81df72019-10-01 22:08:43 +02002852 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2853
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002854 if (cs->flags & CS_FL_SHR)
2855 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002856 if (cs->flags & CS_FL_KILL_CONN) {
2857 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2858 goto do_shutr;
2859 }
2860 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2861 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002862 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002863 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002864
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002865 if (h1s->flags & H1S_F_WANT_KAL) {
2866 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002867 goto end;
2868 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002869
Christopher Faulet7f366362019-04-08 10:51:20 +02002870 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002871 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2872 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002873 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002874 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002875 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002876 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002877 end:
2878 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002879}
2880
2881static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2882{
2883 struct h1s *h1s = cs->ctx;
2884 struct h1c *h1c;
2885
2886 if (!h1s)
2887 return;
2888 h1c = h1s->h1c;
2889
Christopher Faulet6b81df72019-10-01 22:08:43 +02002890 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2891
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002892 if (cs->flags & CS_FL_SHW)
2893 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002894 if (cs->flags & CS_FL_KILL_CONN) {
2895 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002896 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002897 }
2898 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2899 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2900 goto do_shutw;
2901 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002902
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002903 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2904 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002905 goto end;
2906 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002907
Christopher Faulet7f366362019-04-08 10:51:20 +02002908 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002909 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002910 if (!b_data(&h1c->obuf))
2911 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002912 end:
2913 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002914}
2915
Christopher Faulet666a0c42019-01-08 11:12:04 +01002916static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002917{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002918 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002919
Christopher Faulet6b81df72019-10-01 22:08:43 +02002920 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002921 conn_xprt_shutw(conn);
2922 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002923 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002924}
2925
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002926/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2927 * The <es> pointer is not allowed to differ from the one passed to the
2928 * subscribe() call. It always returns zero.
2929 */
2930static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002931{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002932 struct h1s *h1s = cs->ctx;
2933
2934 if (!h1s)
2935 return 0;
2936
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002937 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002938 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002939
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002940 es->events &= ~event_type;
2941 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002942 h1s->subs = NULL;
2943
2944 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002945 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002946
2947 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002948 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002949
Christopher Faulet51dbc942018-09-13 09:05:15 +02002950 return 0;
2951}
2952
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002953/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2954 * event subscriber <es> is not allowed to change from a previous call as long
2955 * as at least one event is still subscribed. The <event_type> must only be a
2956 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2957 * the conn_stream <cs> was already detached, in which case it will return -1.
2958 */
2959static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002960{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002961 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002962 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002963
2964 if (!h1s)
2965 return -1;
2966
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002967 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002968 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002969
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002970 es->events |= event_type;
2971 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002972
2973 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002974 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002975
2976
Christopher Faulet6b81df72019-10-01 22:08:43 +02002977 if (event_type & SUB_RETRY_SEND) {
2978 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002979 /*
2980 * If the conn_stream attempt to subscribe, and the
2981 * mux isn't subscribed to the connection, then it
2982 * probably means the connection wasn't established
2983 * yet, so we have to subscribe.
2984 */
2985 h1c = h1s->h1c;
2986 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2987 h1c->conn->xprt->subscribe(h1c->conn,
2988 h1c->conn->xprt_ctx,
2989 SUB_RETRY_SEND,
2990 &h1c->wait_event);
2991 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002992 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002993}
2994
2995/* Called from the upper layer, to receive data */
2996static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2997{
2998 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002999 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003000 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003001 size_t ret = 0;
3002
Willy Tarreau022e5e52020-09-10 09:33:15 +02003003 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01003004 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02003005 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003006 else
3007 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003008
Christopher Faulete18777b2019-04-16 16:46:36 +02003009 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02003010 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02003011 h1c->flags |= H1C_F_WANT_SPLICE;
3012 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 +02003013 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003014 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003015 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003016 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003017 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003018 }
Willy Tarreau022e5e52020-09-10 09:33:15 +02003019 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003020 return ret;
3021}
3022
3023
3024/* Called from the upper layer, to send data */
3025static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3026{
3027 struct h1s *h1s = cs->ctx;
3028 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003029 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003030
3031 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003032 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003033 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003034
Willy Tarreau022e5e52020-09-10 09:33:15 +02003035 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003036
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003037 /* If we're not connected yet, or we're waiting for a handshake, stop
3038 * now, as we don't want to remove everything from the channel buffer
3039 * before we're sure we can send it.
3040 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003041 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003042 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003043 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003044 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003045
Christopher Faulet46230362019-10-17 16:04:20 +02003046 /* Inherit some flags from the upper layer */
3047 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3048 if (flags & CO_SFL_MSG_MORE)
3049 h1c->flags |= H1C_F_CO_MSG_MORE;
3050 if (flags & CO_SFL_STREAMER)
3051 h1c->flags |= H1C_F_CO_STREAMER;
3052
Christopher Fauletc31872f2019-06-04 22:09:36 +02003053 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003054 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003055
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003056 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3057 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003058 else
3059 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003060
3061 if ((count - ret) > 0)
3062 h1c->flags |= H1C_F_CO_MSG_MORE;
3063
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003064 if (!ret)
3065 break;
3066 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003067 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003068 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003069 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003070 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02003071 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003072 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003073 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003074}
3075
Willy Tarreaue5733232019-05-22 19:24:06 +02003076#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003077/* Send and get, using splicing */
3078static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3079{
3080 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003081 struct h1c *h1c = h1s->h1c;
3082 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003083 int ret = 0;
3084
Willy Tarreau022e5e52020-09-10 09:33:15 +02003085 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003086
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003087 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003088 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003089 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 +02003090 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003091 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003092 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003093 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003094 goto end;
3095 }
3096
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003097 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3098 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003099 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3100 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003101 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003102 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003103 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003104 }
3105
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003106 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003107 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3108 goto end;
3109 }
3110
Christopher Faulet1be55f92018-10-02 15:59:23 +02003111 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3112 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003113 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003114 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003115 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003116 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003117 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003118 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003119 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003120 }
3121
Christopher Faulet1be55f92018-10-02 15:59:23 +02003122 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003123 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003124 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003125 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003126 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003127 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003128
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003129 if ((h1s->flags & H1S_F_REOS) ||
3130 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003131 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003132 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003133 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003134 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003135
Willy Tarreau022e5e52020-09-10 09:33:15 +02003136 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003137 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003138}
3139
3140static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3141{
3142 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003143 int ret = 0;
3144
Willy Tarreau022e5e52020-09-10 09:33:15 +02003145 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003146
Christopher Faulet1be55f92018-10-02 15:59:23 +02003147 if (b_data(&h1s->h1c->obuf))
3148 goto end;
3149
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003150 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003151 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003152 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003153 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3154 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003155 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003156 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003157 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003158
Willy Tarreau022e5e52020-09-10 09:33:15 +02003159 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003160 return ret;
3161}
3162#endif
3163
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003164static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3165{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003166 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003167 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003168
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003169 switch (mux_ctl) {
3170 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003171 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003172 ret |= MUX_STATUS_READY;
3173 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003174 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003175 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3176 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003177 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3178 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3179 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003180 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003181 default:
3182 return -1;
3183 }
3184}
3185
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003186/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003187static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003188{
3189 struct h1c *h1c = conn->ctx;
3190 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003191 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003192
Christopher Fauletf376a312019-01-04 15:16:06 +01003193 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3194 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003195 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3196 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3197 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3198 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3199
3200 if (h1s) {
3201 char *method;
3202
3203 if (h1s->meth < HTTP_METH_OTHER)
3204 method = http_known_methods[h1s->meth].ptr;
3205 else
3206 method = "UNKNOWN";
3207 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3208 " .meth=%s status=%d",
3209 h1s, h1s->flags,
3210 h1m_state_str(h1s->req.state),
3211 h1m_state_str(h1s->res.state), method, h1s->status);
3212 if (h1s->cs)
3213 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3214 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003215
3216 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3217 if (h1s->subs) {
3218 if (h1s->subs) {
3219 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3220 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3221 h1s->subs->tasklet->calls,
3222 h1s->subs->tasklet->context);
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003223 if (h1s->subs->tasklet->calls >= 1000000)
3224 ret = 1;
Willy Tarreau150c4f82021-01-20 17:05:58 +01003225 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3226 chunk_appendf(&trash, ")");
3227 }
3228 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003229 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003230 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003231}
3232
3233
3234/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3235static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3236{
3237 struct h1_hdr_entry *entry;
3238
3239 /* Be sure there is a non-empty <to> */
3240 if (!strlen(to)) {
3241 memprintf(err, "expect <to>");
3242 return -1;
3243 }
3244
3245 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003246 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003247 memprintf(err, "<from> and <to> must not differ execpt the case");
3248 return -1;
3249 }
3250
3251 /* Be sure <from> does not already existsin the tree */
3252 if (ebis_lookup(&hdrs_map.map, from)) {
3253 memprintf(err, "duplicate entry '%s'", from);
3254 return -1;
3255 }
3256
3257 /* Create the entry and insert it in the tree */
3258 entry = malloc(sizeof(*entry));
3259 if (!entry) {
3260 memprintf(err, "out of memory");
3261 return -1;
3262 }
3263
3264 entry->node.key = strdup(from);
3265 entry->name.ptr = strdup(to);
3266 entry->name.len = strlen(to);
3267 if (!entry->node.key || !entry->name.ptr) {
3268 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003269 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003270 free(entry);
3271 memprintf(err, "out of memory");
3272 return -1;
3273 }
3274 ebis_insert(&hdrs_map.map, &entry->node);
3275 return 0;
3276}
3277
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003278/* Migrate the the connection to the current thread.
3279 * Return 0 if successful, non-zero otherwise.
3280 * Expected to be called with the old thread lock held.
3281 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003282static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003283{
3284 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003285 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003286
3287 if (fd_takeover(conn->handle.fd, conn) != 0)
3288 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003289
3290 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3291 /* We failed to takeover the xprt, even if the connection may
3292 * still be valid, flag it as error'd, as we have already
3293 * taken over the fd, and wake the tasklet, so that it will
3294 * destroy it.
3295 */
3296 conn->flags |= CO_FL_ERROR;
3297 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3298 return -1;
3299 }
3300
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003301 if (h1c->wait_event.events)
3302 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3303 h1c->wait_event.events, &h1c->wait_event);
3304 /* To let the tasklet know it should free itself, and do nothing else,
3305 * set its context to NULL.
3306 */
3307 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003308 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003309
3310 task = h1c->task;
3311 if (task) {
3312 task->context = NULL;
3313 h1c->task = NULL;
3314 __ha_barrier_store();
3315 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003316
3317 h1c->task = task_new(tid_bit);
3318 if (!h1c->task) {
3319 h1_release(h1c);
3320 return -1;
3321 }
3322 h1c->task->process = h1_timeout_task;
3323 h1c->task->context = h1c;
3324 }
3325 h1c->wait_event.tasklet = tasklet_new();
3326 if (!h1c->wait_event.tasklet) {
3327 h1_release(h1c);
3328 return -1;
3329 }
3330 h1c->wait_event.tasklet->process = h1_io_cb;
3331 h1c->wait_event.tasklet->context = h1c;
3332 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3333 SUB_RETRY_RECV, &h1c->wait_event);
3334
3335 return 0;
3336}
3337
3338
Christopher Faulet98fbe952019-07-22 16:18:24 +02003339static void h1_hdeaders_case_adjust_deinit()
3340{
3341 struct ebpt_node *node, *next;
3342 struct h1_hdr_entry *entry;
3343
3344 node = ebpt_first(&hdrs_map.map);
3345 while (node) {
3346 next = ebpt_next(node);
3347 ebpt_delete(node);
3348 entry = container_of(node, struct h1_hdr_entry, node);
3349 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003350 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003351 free(entry);
3352 node = next;
3353 }
3354 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003355}
3356
Christopher Faulet98fbe952019-07-22 16:18:24 +02003357static int cfg_h1_headers_case_adjust_postparser()
3358{
3359 FILE *file = NULL;
3360 char *c, *key_beg, *key_end, *value_beg, *value_end;
3361 char *err;
3362 int rc, line = 0, err_code = 0;
3363
3364 if (!hdrs_map.name)
3365 goto end;
3366
3367 file = fopen(hdrs_map.name, "r");
3368 if (!file) {
3369 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3370 hdrs_map.name);
3371 err_code |= ERR_ALERT | ERR_FATAL;
3372 goto end;
3373 }
3374
3375 /* now parse all lines. The file may contain only two header name per
3376 * line, separated by spaces. All heading and trailing spaces will be
3377 * ignored. Lines starting with a # are ignored.
3378 */
3379 while (fgets(trash.area, trash.size, file) != NULL) {
3380 line++;
3381 c = trash.area;
3382
3383 /* strip leading spaces and tabs */
3384 while (*c == ' ' || *c == '\t')
3385 c++;
3386
3387 /* ignore emptu lines, or lines beginning with a dash */
3388 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3389 continue;
3390
3391 /* look for the end of the key */
3392 key_beg = c;
3393 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3394 c++;
3395 key_end = c;
3396
3397 /* strip middle spaces and tabs */
3398 while (*c == ' ' || *c == '\t')
3399 c++;
3400
3401 /* look for the end of the value, it is the end of the line */
3402 value_beg = c;
3403 while (*c && *c != '\n' && *c != '\r')
3404 c++;
3405 value_end = c;
3406
3407 /* trim possibly trailing spaces and tabs */
3408 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3409 value_end--;
3410
3411 /* set final \0 and check entries */
3412 *key_end = '\0';
3413 *value_end = '\0';
3414
3415 err = NULL;
3416 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3417 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003418 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3419 hdrs_map.name, err, line);
3420 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003421 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003422 goto end;
3423 }
3424 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003425 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3426 hdrs_map.name, err, line);
3427 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003428 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003429 }
3430 }
3431
3432 end:
3433 if (file)
3434 fclose(file);
3435 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3436 return err_code;
3437}
3438
3439
3440/* config parser for global "h1-outgoing-header-case-adjust" */
3441static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3442 struct proxy *defpx, const char *file, int line,
3443 char **err)
3444{
3445 if (too_many_args(2, args, err, NULL))
3446 return -1;
3447 if (!*(args[1]) || !*(args[2])) {
3448 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3449 return -1;
3450 }
3451 return add_hdr_case_adjust(args[1], args[2], err);
3452}
3453
3454/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3455static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3456 struct proxy *defpx, const char *file, int line,
3457 char **err)
3458{
3459 if (too_many_args(1, args, err, NULL))
3460 return -1;
3461 if (!*(args[1])) {
3462 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3463 return -1;
3464 }
3465 free(hdrs_map.name);
3466 hdrs_map.name = strdup(args[1]);
3467 return 0;
3468}
3469
3470
3471/* config keyword parsers */
3472static struct cfg_kw_list cfg_kws = {{ }, {
3473 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3474 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3475 { 0, NULL, NULL },
3476 }
3477};
3478
3479INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3480REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3481
3482
Christopher Faulet51dbc942018-09-13 09:05:15 +02003483/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003484/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003485/****************************************/
3486
3487/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003488static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003489 .init = h1_init,
3490 .wake = h1_wake,
3491 .attach = h1_attach,
3492 .get_first_cs = h1_get_first_cs,
3493 .detach = h1_detach,
3494 .destroy = h1_destroy,
3495 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003496 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003497 .rcv_buf = h1_rcv_buf,
3498 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003499#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003500 .rcv_pipe = h1_rcv_pipe,
3501 .snd_pipe = h1_snd_pipe,
3502#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003503 .subscribe = h1_subscribe,
3504 .unsubscribe = h1_unsubscribe,
3505 .shutr = h1_shutr,
3506 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003507 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003508 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003509 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003510 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003511 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003512 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003513};
3514
3515
3516/* this mux registers default HTX proto */
3517static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003518{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003519
Willy Tarreau0108d902018-11-25 19:14:37 +01003520INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3521
Christopher Faulet51dbc942018-09-13 09:05:15 +02003522/*
3523 * Local variables:
3524 * c-indent-level: 8
3525 * c-basic-offset: 8
3526 * End:
3527 */