blob: ef22ccee18a93c3e49c8532500438e7981b60eb9 [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 Tarreau4c7e4b72020-05-27 12:58:42 +020012#include <haproxy/api.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020013#include <haproxy/istbuf.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020014#include <common/cfgparse.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020015#include <haproxy/h1.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020016#include <haproxy/h2.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020017#include <haproxy/htx.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Willy Tarreau8d2b7772020-05-27 10:58:19 +020019#include <import/ebistree.h>
Christopher Faulet98fbe952019-07-22 16:18:24 +020020
Christopher Faulet1be55f92018-10-02 15:59:23 +020021#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020022#include <types/proxy.h>
23#include <types/session.h>
24
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/connection.h>
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020026#include <proto/h1_htx.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020027#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020028#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010029#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030#include <proto/stream.h>
31#include <proto/stream_interface.h>
Christopher Faulet6b81df72019-10-01 22:08:43 +020032#include <proto/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020033
34/*
35 * H1 Connection flags (32 bits)
36 */
37#define H1C_F_NONE 0x00000000
38
39/* Flags indicating why writing output data are blocked */
40#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
41#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
42/* 0x00000004 - 0x00000008 unused */
43
44/* Flags indicating why reading input data are blocked. */
45#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
46#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Faulet7b109f22019-12-05 11:18:31 +010047#define H1C_F_IN_BUSY 0x00000040 /* mux is blocked on input waiting the other side */
Christopher Faulet539e0292018-11-19 10:40:09 +010048/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
Christopher Faulet7b109f22019-12-05 11:18:31 +010050/* Flags indicating the connection state */
Christopher Faulet51dbc942018-09-13 09:05:15 +020051#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
52#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet7b109f22019-12-05 11:18:31 +010053#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +010054#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
55 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020056
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020058#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020059
Christopher Faulet46230362019-10-17 16:04:20 +020060#define H1C_F_CO_MSG_MORE 0x00040000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
61#define H1C_F_CO_STREAMER 0x00080000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
62
Christopher Faulet51dbc942018-09-13 09:05:15 +020063/*
64 * H1 Stream flags (32 bits)
65 */
Christopher Faulet129817b2018-09-20 16:14:40 +020066#define H1S_F_NONE 0x00000000
67#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020068#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
69#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020070#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020071#define H1S_F_WANT_KAL 0x00000010
72#define H1S_F_WANT_TUN 0x00000020
73#define H1S_F_WANT_CLO 0x00000040
74#define H1S_F_WANT_MSK 0x00000070
75#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010076#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010077#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet76014fd2019-12-10 11:47:22 +010078#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
79/* 0x00000800 .. 0x00001000 unused */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020080#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 +020081#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020082
83/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020084struct h1c {
85 struct connection *conn;
86 struct proxy *px;
87 uint32_t flags; /* Connection flags: H1C_F_* */
88
89 struct buffer ibuf; /* Input buffer to store data before parsing */
90 struct buffer obuf; /* Output buffer to store data after reformatting */
91
92 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
93 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
94
95 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010096 struct task *task; /* timeout management task */
97 int timeout; /* idle timeout duration in ticks */
98 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020099};
100
101/* H1 stream descriptor */
102struct h1s {
103 struct h1c *h1c;
104 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100105 struct cs_info csinfo; /* CS info, only used for client connections */
106 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200107
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100108 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200109
Olivier Houchardf502aca2018-12-14 19:42:40 +0100110 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200111 struct h1m req;
112 struct h1m res;
113
114 enum http_meth_t meth; /* HTTP resquest method */
115 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200116};
117
Christopher Faulet98fbe952019-07-22 16:18:24 +0200118/* Map of headers used to convert outgoing headers */
119struct h1_hdrs_map {
120 char *name;
121 struct eb_root map;
122};
123
124/* An entry in a headers map */
125struct h1_hdr_entry {
126 struct ist name;
127 struct ebpt_node node;
128};
129
130/* Declare the headers map */
131static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
132
133
Christopher Faulet6b81df72019-10-01 22:08:43 +0200134/* trace source and events */
135static void h1_trace(enum trace_level level, uint64_t mask,
136 const struct trace_source *src,
137 const struct ist where, const struct ist func,
138 const void *a1, const void *a2, const void *a3, const void *a4);
139
140/* The event representation is split like this :
141 * h1c - internal H1 connection
142 * h1s - internal H1 stream
143 * strm - application layer
144 * rx - data receipt
145 * tx - data transmission
146 *
147 */
148static const struct trace_event h1_trace_events[] = {
149#define H1_EV_H1C_NEW (1ULL << 0)
150 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
151#define H1_EV_H1C_RECV (1ULL << 1)
152 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
153#define H1_EV_H1C_SEND (1ULL << 2)
154 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
155#define H1_EV_H1C_BLK (1ULL << 3)
156 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
157#define H1_EV_H1C_WAKE (1ULL << 4)
158 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
159#define H1_EV_H1C_END (1ULL << 5)
160 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
161#define H1_EV_H1C_ERR (1ULL << 6)
162 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
163
164#define H1_EV_RX_DATA (1ULL << 7)
165 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
166#define H1_EV_RX_EOI (1ULL << 8)
167 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
168#define H1_EV_RX_HDRS (1ULL << 9)
169 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
170#define H1_EV_RX_BODY (1ULL << 10)
171 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
172#define H1_EV_RX_TLRS (1ULL << 11)
173 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
174
175#define H1_EV_TX_DATA (1ULL << 12)
176 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
177#define H1_EV_TX_EOI (1ULL << 13)
178 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
179#define H1_EV_TX_HDRS (1ULL << 14)
180 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
181#define H1_EV_TX_BODY (1ULL << 15)
182 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
183#define H1_EV_TX_TLRS (1ULL << 16)
184 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
185
186#define H1_EV_H1S_NEW (1ULL << 17)
187 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
188#define H1_EV_H1S_BLK (1ULL << 18)
189 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
190#define H1_EV_H1S_END (1ULL << 19)
191 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
192#define H1_EV_H1S_ERR (1ULL << 20)
193 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
194
195#define H1_EV_STRM_NEW (1ULL << 21)
196 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
197#define H1_EV_STRM_RECV (1ULL << 22)
198 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
199#define H1_EV_STRM_SEND (1ULL << 23)
200 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
201#define H1_EV_STRM_WAKE (1ULL << 24)
202 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
203#define H1_EV_STRM_SHUT (1ULL << 25)
204 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
205#define H1_EV_STRM_END (1ULL << 26)
206 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
207#define H1_EV_STRM_ERR (1ULL << 27)
208 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
209
210 { }
211};
212
213static const struct name_desc h1_trace_lockon_args[4] = {
214 /* arg1 */ { /* already used by the connection */ },
215 /* arg2 */ { .name="h1s", .desc="H1 stream" },
216 /* arg3 */ { },
217 /* arg4 */ { }
218};
219
220static const struct name_desc h1_trace_decoding[] = {
221#define H1_VERB_CLEAN 1
222 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
223#define H1_VERB_MINIMAL 2
224 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
225#define H1_VERB_SIMPLE 3
226 { .name="simple", .desc="add request/response status line or htx info when available" },
227#define H1_VERB_ADVANCED 4
228 { .name="advanced", .desc="add header fields or frame decoding when available" },
229#define H1_VERB_COMPLETE 5
230 { .name="complete", .desc="add full data dump when available" },
231 { /* end */ }
232};
233
234static struct trace_source trace_h1 = {
235 .name = IST("h1"),
236 .desc = "HTTP/1 multiplexer",
237 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
238 .default_cb = h1_trace,
239 .known_events = h1_trace_events,
240 .lockon_args = h1_trace_lockon_args,
241 .decoding = h1_trace_decoding,
242 .report_events = ~0, // report everything by default
243};
244
245#define TRACE_SOURCE &trace_h1
246INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
247
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100249DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
250DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200251
Christopher Faulet51dbc942018-09-13 09:05:15 +0200252static int h1_recv(struct h1c *h1c);
253static int h1_send(struct h1c *h1c);
254static int h1_process(struct h1c *h1c);
255static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100256static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100257static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200258static void h1_wake_stream_for_recv(struct h1s *h1s);
259static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200260
Christopher Faulet6b81df72019-10-01 22:08:43 +0200261/* the H1 traces always expect that arg1, if non-null, is of type connection
262 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
263 * that arg3, if non-null, is a htx for rx/tx headers.
264 */
265static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
266 const struct ist where, const struct ist func,
267 const void *a1, const void *a2, const void *a3, const void *a4)
268{
269 const struct connection *conn = a1;
270 const struct h1c *h1c = conn ? conn->ctx : NULL;
271 const struct h1s *h1s = a2;
272 const struct htx *htx = a3;
273 const size_t *val = a4;
274
275 if (!h1c)
276 h1c = (h1s ? h1s->h1c : NULL);
277
278 if (!h1c || src->verbosity < H1_VERB_CLEAN)
279 return;
280
281 /* Display frontend/backend info by default */
282 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
283
284 /* Display request and response states if h1s is defined */
285 if (h1s)
286 chunk_appendf(&trace_buf, " [%s, %s]",
287 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
288
289 if (src->verbosity == H1_VERB_CLEAN)
290 return;
291
292 /* Display the value to the 4th argument (level > STATE) */
293 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100294 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200295
296 /* Display status-line if possible (verbosity > MINIMAL) */
297 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
298 const struct htx_blk *blk = htx_get_head_blk(htx);
299 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
300 enum htx_blk_type type = htx_get_blk_type(blk);
301
302 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
303 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
304 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
305 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
306 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
307 }
308
309 /* Display h1c info and, if defined, h1s info (pointer + flags) */
310 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
311 if (h1s)
312 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
313
314 if (src->verbosity == H1_VERB_MINIMAL)
315 return;
316
317 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
318 if (src->level > TRACE_LEVEL_USER) {
319 if (src->verbosity == H1_VERB_COMPLETE ||
320 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
321 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
322 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
323 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
324 if (src->verbosity == H1_VERB_COMPLETE ||
325 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
326 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
327 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
328 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
329 }
330
331 /* Display htx info if defined (level > USER) */
332 if (src->level > TRACE_LEVEL_USER && htx) {
333 int full = 0;
334
335 /* Full htx info (level > STATE && verbosity > SIMPLE) */
336 if (src->level > TRACE_LEVEL_STATE) {
337 if (src->verbosity == H1_VERB_COMPLETE)
338 full = 1;
339 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
340 full = 1;
341 }
342
343 chunk_memcat(&trace_buf, "\n\t", 2);
344 htx_dump(&trace_buf, htx, full);
345 }
346}
347
348
Christopher Faulet51dbc942018-09-13 09:05:15 +0200349/*****************************************************/
350/* functions below are for dynamic buffer management */
351/*****************************************************/
352/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100353 * Indicates whether or not we may receive data. The rules are the following :
354 * - if an error or a shutdown for reads was detected on the connection we
355 must not attempt to receive
356 * - if the input buffer failed to be allocated or is full , we must not try
357 * to receive
358 * - if he input processing is busy waiting for the output side, we may
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500359 * attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360 * - otherwise must may not attempt to receive
361 */
362static inline int h1_recv_allowed(const struct h1c *h1c)
363{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100364 if (h1c->flags & H1C_F_CS_ERROR) {
365 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 +0200366 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200367 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100369 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200370 TRACE_DEVEL("recv not allowed because of (error|read0) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletcf56b992018-12-11 16:12:31 +0100371 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200372 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100373
Christopher Fauletcb55f482018-12-10 11:56:47 +0100374 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200375 return 1;
376
Christopher Faulet6b81df72019-10-01 22:08:43 +0200377 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 +0200378 return 0;
379}
380
381/*
382 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
383 * flags are used to figure what buffer was requested. It returns 1 if the
384 * allocation succeeds, in which case the connection is woken up, or 0 if it's
385 * impossible to wake up and we prefer to be woken up later.
386 */
387static int h1_buf_available(void *target)
388{
389 struct h1c *h1c = target;
390
391 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200392 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 +0200393 h1c->flags &= ~H1C_F_IN_ALLOC;
394 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200395 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200396 return 1;
397 }
398
399 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200400 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 +0200401 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200402 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200403 if (h1c->h1s)
404 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200405 return 1;
406 }
407
Christopher Faulet51dbc942018-09-13 09:05:15 +0200408 return 0;
409}
410
411/*
412 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
413 */
414static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
415{
416 struct buffer *buf = NULL;
417
Willy Tarreau21046592020-02-26 10:39:36 +0100418 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200419 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
420 h1c->buf_wait.target = h1c;
421 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau21046592020-02-26 10:39:36 +0100422 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200423 }
424 return buf;
425}
426
427/*
428 * Release a buffer, if any, and try to wake up entities waiting in the buffer
429 * wait queue.
430 */
431static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
432{
433 if (bptr->size) {
434 b_free(bptr);
435 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
436 }
437}
438
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100439/* returns the number of streams in use on a connection to figure if it's idle
440 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
441 * not. This flag is only set when no H1S is attached and when the previous
442 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100443 */
444static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200445{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200447
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100448 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449}
450
Willy Tarreau00f18a32019-01-26 12:19:01 +0100451/* returns the number of streams still available on a connection */
452static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100453{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100454 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100455}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456
Willy Tarreau00f18a32019-01-26 12:19:01 +0100457
Christopher Faulet51dbc942018-09-13 09:05:15 +0200458/*****************************************************************/
459/* functions below are dedicated to the mux setup and management */
460/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200461
462/* returns non-zero if there are input data pending for stream h1s. */
463static inline size_t h1s_data_pending(const struct h1s *h1s)
464{
465 const struct h1m *h1m;
466
467 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
468 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100469 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200470
471 return b_data(&h1s->h1c->ibuf);
472}
473
Christopher Faulet47365272018-10-31 17:40:50 +0100474static struct conn_stream *h1s_new_cs(struct h1s *h1s)
475{
476 struct conn_stream *cs;
477
Christopher Faulet6b81df72019-10-01 22:08:43 +0200478 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100479 cs = cs_new(h1s->h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200480 if (!cs) {
481 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 +0100482 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200483 }
Christopher Faulet47365272018-10-31 17:40:50 +0100484 h1s->cs = cs;
485 cs->ctx = h1s;
486
487 if (h1s->flags & H1S_F_NOT_FIRST)
488 cs->flags |= CS_FL_NOT_FIRST;
489
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100490 if (global.tune.options & GTUNE_USE_SPLICE)
491 cs->flags |= CS_FL_MAY_SPLICE;
492
Christopher Faulet6b81df72019-10-01 22:08:43 +0200493 if (stream_create_from_cs(cs) < 0) {
494 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 +0100495 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200496 }
497
498 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100499 return cs;
500
501 err:
502 cs_free(cs);
503 h1s->cs = NULL;
504 return NULL;
505}
506
Olivier Houchardf502aca2018-12-14 19:42:40 +0100507static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200508{
509 struct h1s *h1s;
510
Christopher Faulet6b81df72019-10-01 22:08:43 +0200511 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
512
Christopher Faulet51dbc942018-09-13 09:05:15 +0200513 h1s = pool_alloc(pool_head_h1s);
514 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100515 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200516
517 h1s->h1c = h1c;
518 h1c->h1s = h1s;
519
Olivier Houchardf502aca2018-12-14 19:42:40 +0100520 h1s->sess = sess;
521
Christopher Faulet51dbc942018-09-13 09:05:15 +0200522 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100523 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200524
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100525 h1s->subs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200526
527 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100528 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200529
Christopher Faulet129817b2018-09-20 16:14:40 +0200530 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100531 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200532
533 h1s->status = 0;
534 h1s->meth = HTTP_METH_OTHER;
535
Christopher Faulet47365272018-10-31 17:40:50 +0100536 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
537 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100538 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100539
Christopher Faulet129817b2018-09-20 16:14:40 +0200540 if (!conn_is_back(h1c->conn)) {
541 if (h1c->px->options2 & PR_O2_REQBUG_OK)
542 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200543
544 /* For frontend connections we should always have a session */
545 if (!sess)
546 sess = h1c->conn->owner;
547
Dave Pirotte234740f2019-07-10 13:57:38 +0000548 /* Timers for subsequent sessions on the same HTTP 1.x connection
549 * measure from `now`, not from the connection accept time */
550 if (h1s->flags & H1S_F_NOT_FIRST) {
551 h1s->csinfo.create_date = date;
552 h1s->csinfo.tv_create = now;
553 h1s->csinfo.t_handshake = 0;
554 h1s->csinfo.t_idle = -1;
555 }
556 else {
557 h1s->csinfo.create_date = sess->accept_date;
558 h1s->csinfo.tv_create = sess->tv_accept;
559 h1s->csinfo.t_handshake = sess->t_handshake;
560 h1s->csinfo.t_idle = -1;
561 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200562 }
563 else {
564 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
565 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200566
Christopher Fauletfeb11742018-11-29 15:12:34 +0100567 h1s->csinfo.create_date = date;
568 h1s->csinfo.tv_create = now;
569 h1s->csinfo.t_handshake = 0;
570 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200571 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100572
Christopher Faulete9b70722019-04-08 10:46:02 +0200573 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
574 * create a new one.
575 */
576 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200577 cs->ctx = h1s;
578 h1s->cs = cs;
579 }
Christopher Faulet47365272018-10-31 17:40:50 +0100580 else {
581 cs = h1s_new_cs(h1s);
582 if (!cs)
583 goto fail;
584 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200585 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200586 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100587
588 fail:
589 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200590 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 +0100591 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200592}
593
594static void h1s_destroy(struct h1s *h1s)
595{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596 if (h1s) {
597 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200598
Christopher Faulet6b81df72019-10-01 22:08:43 +0200599 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200600 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200601
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100602 if (h1s->subs)
603 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200604
Christopher Fauletcb55f482018-12-10 11:56:47 +0100605 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200606 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100607 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200608 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
609 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100610
611 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
612 !(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 +0100613 (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 +0100614 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
615 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
616 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
617 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200618 pool_free(pool_head_h1s, h1s);
619 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200620}
621
Christopher Fauletfeb11742018-11-29 15:12:34 +0100622static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
623{
624 struct h1s *h1s = cs->ctx;
625
626 if (h1s && !conn_is_back(cs->conn))
627 return &h1s->csinfo;
628 return NULL;
629}
630
Christopher Faulet51dbc942018-09-13 09:05:15 +0200631/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200632 * Initialize the mux once it's attached. It is expected that conn->ctx points
633 * to the existing conn_stream (for outgoing connections or for incoming onces
634 * during a mux upgrade) or NULL (for incoming ones during the connexion
635 * establishment). <input> is always used as Input buffer and may contain
636 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
637 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200638 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200639static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
640 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200641{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200642 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100643 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200644 void *conn_ctx = conn->ctx;
645
646 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200647
648 h1c = pool_alloc(pool_head_h1c);
649 if (!h1c)
650 goto fail_h1c;
651 h1c->conn = conn;
652 h1c->px = proxy;
653
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100654 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200655 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200656 h1c->obuf = BUF_NULL;
657 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200658 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200659
Willy Tarreau21046592020-02-26 10:39:36 +0100660 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200661 h1c->wait_event.tasklet = tasklet_new();
662 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200663 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200664 h1c->wait_event.tasklet->process = h1_io_cb;
665 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100666 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200667
Christopher Faulete9b70722019-04-08 10:46:02 +0200668 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100669 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
670 if (tick_isset(proxy->timeout.serverfin))
671 h1c->shut_timeout = proxy->timeout.serverfin;
672 } else {
673 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
674 if (tick_isset(proxy->timeout.clientfin))
675 h1c->shut_timeout = proxy->timeout.clientfin;
676 }
677 if (tick_isset(h1c->timeout)) {
678 t = task_new(tid_bit);
679 if (!t)
680 goto fail;
681
682 h1c->task = t;
683 t->process = h1_timeout_task;
684 t->context = h1c;
685 t->expire = tick_add(now_ms, h1c->timeout);
686 }
687
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100688 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200689
Christopher Faulet6b81df72019-10-01 22:08:43 +0200690 /* Always Create a new H1S */
691 if (!h1s_create(h1c, conn_ctx, sess))
692 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100693
694 if (t)
695 task_queue(t);
696
Christopher Faulet51dbc942018-09-13 09:05:15 +0200697 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau2c1f37d2020-03-04 17:50:02 +0100698 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200699
700 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200701 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200702 return 0;
703
704 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200705 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200706 if (h1c->wait_event.tasklet)
707 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200708 pool_free(pool_head_h1c, h1c);
709 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200710 conn->ctx = conn_ctx; // restore saved context
711 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200712 return -1;
713}
714
Christopher Faulet73c12072019-04-08 11:23:22 +0200715/* release function. This one should be called to free all resources allocated
716 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200717 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200718static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200719{
Christopher Faulet61840e72019-04-15 09:33:32 +0200720 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200721
Christopher Faulet6b81df72019-10-01 22:08:43 +0200722 TRACE_POINT(H1_EV_H1C_END);
723
Christopher Faulet51dbc942018-09-13 09:05:15 +0200724 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200725 /* The connection must be aattached to this mux to be released */
726 if (h1c->conn && h1c->conn->ctx == h1c)
727 conn = h1c->conn;
728
Christopher Faulet6b81df72019-10-01 22:08:43 +0200729 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
730
Christopher Faulet61840e72019-04-15 09:33:32 +0200731 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200732 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200733 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200734 /* Make sure we're no longer subscribed to anything */
735 if (h1c->wait_event.events)
736 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
737 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200738 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200739 /* connection successfully upgraded to H2, this
740 * mux was already released */
741 return;
742 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200743 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200744 sess_log(conn->owner); /* Log if the upgrade failed */
745 }
746
Olivier Houchard45c44372019-06-11 14:06:23 +0200747
Willy Tarreau21046592020-02-26 10:39:36 +0100748 if (MT_LIST_ADDED(&h1c->buf_wait.list))
749 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200750
751 h1_release_buf(h1c, &h1c->ibuf);
752 h1_release_buf(h1c, &h1c->obuf);
753
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100754 if (h1c->task) {
755 h1c->task->context = NULL;
756 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
757 h1c->task = NULL;
758 }
759
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200760 if (h1c->wait_event.tasklet)
761 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200762
Christopher Fauletf2824e62018-10-01 12:12:37 +0200763 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200764 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100765 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200766 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200767 pool_free(pool_head_h1c, h1c);
768 }
769
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200770 if (conn) {
771 conn->mux = NULL;
772 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200773 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200774
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200775 conn_stop_tracking(conn);
776 conn_full_close(conn);
777 if (conn->destroy_cb)
778 conn->destroy_cb(conn);
779 conn_free(conn);
780 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200781}
782
783/******************************************************/
784/* functions below are for the H1 protocol processing */
785/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200786/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
787 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200788 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100789static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200790{
Christopher Faulet570d1612018-11-26 11:13:57 +0100791 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200792
Christopher Faulet570d1612018-11-26 11:13:57 +0100793 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200794 (*(p + 5) > '1' ||
795 (*(p + 5) == '1' && *(p + 7) >= '1')))
796 h1m->flags |= H1_MF_VER_11;
797}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200798
Christopher Faulet9768c262018-10-22 09:34:31 +0200799/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
800 * greater or equal to 1.1
801 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100802static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200803{
Christopher Faulet570d1612018-11-26 11:13:57 +0100804 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200805
Christopher Faulet570d1612018-11-26 11:13:57 +0100806 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200807 (*(p + 5) > '1' ||
808 (*(p + 5) == '1' && *(p + 7) >= '1')))
809 h1m->flags |= H1_MF_VER_11;
810}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200811
Christopher Fauletf2824e62018-10-01 12:12:37 +0200812/* Deduce the connection mode of the client connection, depending on the
813 * configuration and the H1 message flags. This function is called twice, the
814 * first time when the request is parsed and the second time when the response
815 * is parsed.
816 */
817static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
818{
819 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200820
821 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100822 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200823 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100824 h1s->status == 101) {
825 /* Either we've established an explicit tunnel, or we're
826 * switching the protocol. In both cases, we're very unlikely to
827 * understand the next protocols. We have to switch to tunnel
828 * mode, so that we transfer the request and responses then let
829 * this protocol pass unmodified. When we later implement
830 * specific parsers for such protocols, we'll want to check the
831 * Upgrade header which contains information about that protocol
832 * for responses with status 101 (eg: see RFC2817 about TLS).
833 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200834 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200835 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 +0100836 }
837 else if (h1s->flags & H1S_F_WANT_KAL) {
838 /* By default the client is in KAL mode. CLOSE mode mean
839 * it is imposed by the client itself. So only change
840 * KAL mode here. */
841 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
842 /* no length known or explicit close => close */
843 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200844 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 +0100845 }
846 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
847 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500848 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100849 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200850 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 +0100851 }
852 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200853 }
854 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100855 /* Input direction: first pass */
856 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
857 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200858 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200859 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 +0100860 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200861 }
862
863 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200864 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200865 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200866 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);
867 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200868}
869
870/* Deduce the connection mode of the client connection, depending on the
871 * configuration and the H1 message flags. This function is called twice, the
872 * first time when the request is parsed and the second time when the response
873 * is parsed.
874 */
875static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
876{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100877 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100878 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100879 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200880
Christopher Fauletf2824e62018-10-01 12:12:37 +0200881 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100882 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200883 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100884 h1s->status == 101) {
885 /* Either we've established an explicit tunnel, or we're
886 * switching the protocol. In both cases, we're very unlikely to
887 * understand the next protocols. We have to switch to tunnel
888 * mode, so that we transfer the request and responses then let
889 * this protocol pass unmodified. When we later implement
890 * specific parsers for such protocols, we'll want to check the
891 * Upgrade header which contains information about that protocol
892 * for responses with status 101 (eg: see RFC2817 about TLS).
893 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200894 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200895 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 +0100896 }
897 else if (h1s->flags & H1S_F_WANT_KAL) {
898 /* By default the server is in KAL mode. CLOSE mode mean
899 * it is imposed by haproxy itself. So only change KAL
900 * mode here. */
901 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
902 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
903 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
904 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200905 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 +0100906 }
907 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200908 }
Christopher Faulet70033782018-12-05 13:50:11 +0100909 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100910 /* Output direction: first pass */
911 if (h1m->flags & H1_MF_CONN_CLO) {
912 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100913 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200914 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 +0100915 }
916 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
917 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
918 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
919 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
920 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
921 /* no explicit keep-alive option httpclose/server-close => close */
922 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200923 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 +0100924 }
Christopher Faulet70033782018-12-05 13:50:11 +0100925 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200926
927 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200928 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200929 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200930 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);
931 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200932}
933
Christopher Fauletb992af02019-03-28 15:42:24 +0100934static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200935{
936 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200937
938 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
939 * token is found
940 */
941 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200942 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200943
944 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200945 if (!(h1m->flags & H1_MF_VER_11)) {
946 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 +0100947 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200948 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200949 }
950 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200951 if (h1m->flags & H1_MF_VER_11) {
952 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100953 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200954 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200955 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200956}
957
Christopher Fauletb992af02019-03-28 15:42:24 +0100958static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200959{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200960 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
961 * token is found
962 */
963 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200964 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200965
966 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100967 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +0200968 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
969 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 +0100970 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200971 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200972 }
973 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200974 if (h1m->flags & H1_MF_VER_11) {
975 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100976 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200977 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200978 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200979}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200980
Christopher Fauletb992af02019-03-28 15:42:24 +0100981static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200982{
Christopher Fauletb992af02019-03-28 15:42:24 +0100983 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200984 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100985 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200986 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200987}
988
Christopher Fauletb992af02019-03-28 15:42:24 +0100989static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
990{
991 if (!conn_is_back(h1s->h1c->conn))
992 h1_set_cli_conn_mode(h1s, h1m);
993 else
994 h1_set_srv_conn_mode(h1s, h1m);
995
996 if (!(h1m->flags & H1_MF_RESP))
997 h1_update_req_conn_value(h1s, h1m, conn_val);
998 else
999 h1_update_res_conn_value(h1s, h1m, conn_val);
1000}
Christopher Faulete44769b2018-11-29 23:01:45 +01001001
Christopher Faulet98fbe952019-07-22 16:18:24 +02001002/* Try to adjust the case of the message header name using the global map
1003 * <hdrs_map>.
1004 */
1005static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1006{
1007 struct ebpt_node *node;
1008 struct h1_hdr_entry *entry;
1009
1010 /* No entry in the map, do nothing */
1011 if (eb_is_empty(&hdrs_map.map))
1012 return;
1013
1014 /* No conversion fo the request headers */
1015 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1016 return;
1017
1018 /* No conversion fo the response headers */
1019 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1020 return;
1021
1022 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1023 if (!node)
1024 return;
1025 entry = container_of(node, struct h1_hdr_entry, node);
1026 name->ptr = entry->name.ptr;
1027 name->len = entry->name.len;
1028}
1029
Christopher Faulete44769b2018-11-29 23:01:45 +01001030/* Append the description of what is present in error snapshot <es> into <out>.
1031 * The description must be small enough to always fit in a buffer. The output
1032 * buffer may be the trash so the trash must not be used inside this function.
1033 */
1034static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1035{
1036 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001037 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1038 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1039 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1040 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1041 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1042 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001043}
1044/*
1045 * Capture a bad request or response and archive it in the proxy's structure.
1046 * By default it tries to report the error position as h1m->err_pos. However if
1047 * this one is not set, it will then report h1m->next, which is the last known
1048 * parsing point. The function is able to deal with wrapping buffers. It always
1049 * displays buffers as a contiguous area starting at buf->p. The direction is
1050 * determined thanks to the h1m's flags.
1051 */
1052static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1053 struct h1m *h1m, struct buffer *buf)
1054{
1055 struct session *sess = h1c->conn->owner;
1056 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001057 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001058 union error_snapshot_ctx ctx;
1059
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001060 if (h1s->cs && h1s->cs->data) {
1061 if (sess == NULL)
1062 sess = si_strm(h1s->cs->data)->sess;
1063 if (!(h1m->flags & H1_MF_RESP))
1064 other_end = si_strm(h1s->cs->data)->be;
1065 else
1066 other_end = sess->fe;
1067 } else
1068 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001069
1070 /* http-specific part now */
1071 ctx.h1.state = h1m->state;
1072 ctx.h1.c_flags = h1c->flags;
1073 ctx.h1.s_flags = h1s->flags;
1074 ctx.h1.m_flags = h1m->flags;
1075 ctx.h1.m_clen = h1m->curr_len;
1076 ctx.h1.m_blen = h1m->body_len;
1077
1078 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1079 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001080 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1081 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001082}
1083
Christopher Fauletadb22202018-12-12 10:32:09 +01001084/* Emit the chunksize followed by a CRLF in front of data of the buffer
1085 * <buf>. It goes backwards and starts with the byte before the buffer's
1086 * head. The caller is responsible for ensuring there is enough room left before
1087 * the buffer's head for the string.
1088 */
1089static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1090{
1091 char *beg, *end;
1092
1093 beg = end = b_head(buf);
1094 *--beg = '\n';
1095 *--beg = '\r';
1096 do {
1097 *--beg = hextab[chksz & 0xF];
1098 } while (chksz >>= 4);
1099 buf->head -= (end - beg);
1100 b_add(buf, end - beg);
1101}
1102
1103/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1104 * ensuring there is enough room left in the buffer for the string. */
1105static void h1_emit_chunk_crlf(struct buffer *buf)
1106{
1107 *(b_peek(buf, b_data(buf))) = '\r';
1108 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1109 b_add(buf, 2);
1110}
1111
Christopher Faulet129817b2018-09-20 16:14:40 +02001112/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001113 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001114 * CONNECT requests. On the client side, if the response is not finished, the
1115 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001116 */
1117static void h1_set_req_tunnel_mode(struct h1s *h1s)
1118{
1119 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1120 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001121 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1122
Christopher Faulet76014fd2019-12-10 11:47:22 +01001123 if (!conn_is_back(h1s->h1c->conn)) {
1124 h1s->flags &= ~H1S_F_PARSING_DONE;
1125 if (h1s->res.state < H1_MSG_DONE) {
1126 h1s->h1c->flags |= H1C_F_IN_BUSY;
1127 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1128 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001129 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001130 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1131 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1132 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1133 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1134 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001135}
1136
1137/*
1138 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001139 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001140 * last case, this function takes care to switch the request to tunnel mode if
1141 * possible. On the server side, if the request is not finished, the mux is mark
1142 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001143 */
1144static void h1_set_res_tunnel_mode(struct h1s *h1s)
1145{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001146
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001147 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1148 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001149 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1150
Christopher Faulet76014fd2019-12-10 11:47:22 +01001151 if (conn_is_back(h1s->h1c->conn)) {
1152 h1s->flags &= ~H1S_F_PARSING_DONE;
1153 /* On protocol switching, switch the request to tunnel mode if it is in
1154 * DONE state. Otherwise we will wait the end of the request to switch
1155 * it in tunnel mode.
1156 */
1157 if (h1s->req.state < H1_MSG_DONE) {
1158 h1s->h1c->flags |= H1C_F_IN_BUSY;
1159 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1160 }
1161 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1162 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1163 h1s->req.state = H1_MSG_TUNNEL;
1164 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1165 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001166 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001167 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1168 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1169 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1170 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001171 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001172}
1173
1174/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001175 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001176 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001177 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001178 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001179static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001180 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001181{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001182 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001183 int ret = 0;
1184
Christopher Faulet6b81df72019-10-01 22:08:43 +02001185 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1186
Christopher Faulet89aed322020-06-02 17:33:56 +02001187 if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
1188 !(h1s->flags & H1S_F_NOT_FIRST) && /* It is the first transaction */
1189 !(h1m->flags & H1_MF_RESP)) { /* It is a request */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001190 /* Try to match H2 preface before parsing the request headers. */
1191 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001192 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001193 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001194 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001195 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001196 else {
1197 if (h1s->meth == HTTP_METH_CONNECT)
1198 h1m->flags |= H1_MF_METH_CONNECT;
1199 if (h1s->meth == HTTP_METH_HEAD)
1200 h1m->flags |= H1_MF_METH_HEAD;
1201 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001202
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001203 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1204 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001205 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 +02001206 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001207 if (!(h1m->flags & H1_MF_RESP)) {
1208 h1s->flags |= H1S_F_REQ_ERROR;
1209 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1210 }
1211 else {
1212 h1s->flags |= H1S_F_RES_ERROR;
1213 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1214 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001215 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001216 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001217 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1218 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001219 goto end;
1220 }
1221
Christopher Faulet486498c2019-10-11 14:22:00 +02001222 if (h1m->err_pos >= 0) {
1223 /* Maybe we found an error during the parsing while we were
1224 * configured not to block on that, so we have to capture it
1225 * now.
1226 */
1227 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1228 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1229 }
1230
Christopher Faulet129817b2018-09-20 16:14:40 +02001231 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001232 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001233 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001234 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001235 }
1236 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001237 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001238 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001239 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001240 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001241 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001242 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001243
Christopher Faulet129817b2018-09-20 16:14:40 +02001244 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001245 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001246 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001247
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001248 h2c_upgrade:
1249 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001250 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001251 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001252 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1253 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001254}
1255
1256/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001257 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001258 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1259 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001260 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001261static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001262 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001263 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001264{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001265 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001266
Christopher Faulet6b81df72019-10-01 22:08:43 +02001267 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001268 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001269 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001270 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 +01001271 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001272 if (!(h1m->flags & H1_MF_RESP)) {
1273 h1s->flags |= H1S_F_REQ_ERROR;
1274 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1275 }
1276 else {
1277 h1s->flags |= H1S_F_RES_ERROR;
1278 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1279 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001280 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001281 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001282 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001283 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001284 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001285 }
1286
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001287 if (h1m->state == H1_MSG_DATA && h1m->curr_len && h1s->cs)
1288 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1289 else if (h1s->cs)
1290 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1291
Christopher Faulet02a02532019-11-15 09:36:28 +01001292 *ofs += ret;
1293
1294 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001295 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001296 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001297}
1298
1299/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001300 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1301 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1302 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1303 * responsible to update the parser state <h1m>.
1304 */
1305static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1306 struct buffer *buf, size_t *ofs, size_t max)
1307{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001308 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001309
Christopher Faulet6b81df72019-10-01 22:08:43 +02001310 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001311 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001312 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001313 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 +01001314 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001315 if (!(h1m->flags & H1_MF_RESP)) {
1316 h1s->flags |= H1S_F_REQ_ERROR;
1317 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1318 }
1319 else {
1320 h1s->flags |= H1S_F_RES_ERROR;
1321 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1322 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001323 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001324 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001325 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1326 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001327 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001328 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001329
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001330 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001331
1332 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001333 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001334 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001335}
1336
1337/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001338 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
1339 * proceed. This functions is responsible to update the parser state <h1m>. It
1340 * also add the flag CS_FL_EOI on the CS.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001341 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001342static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1343 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001344{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001345 int ret;
1346
1347 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){max});
1348 ret = h1_parse_msg_eom(h1m, htx, max);
1349 if (!ret) {
1350 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1351 if (htx->flags & HTX_FL_PARSING_ERROR) {
1352 if (!(h1m->flags & H1_MF_RESP)) {
1353 h1s->flags |= H1S_F_REQ_ERROR;
1354 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1355 }
1356 else {
1357 h1s->flags |= H1S_F_RES_ERROR;
1358 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1359 }
1360 h1s->cs->flags |= CS_FL_EOI;
1361 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1362 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1363 }
1364 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001365 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001366
Christopher Faulet76014fd2019-12-10 11:47:22 +01001367 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001368 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001369 end:
1370 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){ret});
1371 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001372}
1373
1374/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001375 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001376 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1377 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001378 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001379static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001380{
Christopher Faulet539e0292018-11-19 10:40:09 +01001381 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001382 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001383 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001384 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001385 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001386 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001387
Christopher Faulet539e0292018-11-19 10:40:09 +01001388 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001389 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001390
Christopher Fauletf2824e62018-10-01 12:12:37 +02001391 if (!conn_is_back(h1c->conn)) {
1392 h1m = &h1s->req;
1393 errflag = H1S_F_REQ_ERROR;
1394 }
1395 else {
1396 h1m = &h1s->res;
1397 errflag = H1S_F_RES_ERROR;
1398 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001399
Christopher Faulet74027762019-02-26 14:45:05 +01001400 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001401 if (h1s->flags & errflag)
1402 goto end;
1403
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001404 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001405 size_t used = htx_used_space(htx);
1406
Christopher Faulet129817b2018-09-20 16:14:40 +02001407 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001408 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001409 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001410 if (!ret)
1411 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001412
1413 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1414 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1415
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001416 if ((h1m->flags & H1_MF_RESP) &&
1417 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1418 h1m_init_res(&h1s->res);
1419 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001420 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001421 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001422 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001423 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001424 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001425 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001426 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001427 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001428
1429 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1430 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001431 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001432 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001433 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1434 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1435 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001436 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001437
Christopher Faulet76014fd2019-12-10 11:47:22 +01001438 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1439 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001440 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001441 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001442 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1443 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1444 break;
1445
1446 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1447 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1448 }
1449
Christopher Fauletf3158e92019-11-15 11:14:23 +01001450 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1451 h1_set_req_tunnel_mode(h1s);
1452 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001453 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001454 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1455 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001456 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001457 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001458 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001459 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001460 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001461 if (!ret)
1462 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001463
1464 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1465 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001466 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001467 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001468 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001469 break;
1470 }
1471
Christopher Faulet30db3d72019-05-17 15:35:33 +02001472 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001473 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001474
Christopher Faulet6b81df72019-10-01 22:08:43 +02001475 if (h1s->flags & errflag) {
1476 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001477 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001478 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001479
Christopher Faulet539e0292018-11-19 10:40:09 +01001480 b_del(&h1c->ibuf, total);
1481
1482 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001483 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001484 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001485 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001486 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001487 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE);
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001488 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001489 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001490
Christopher Fauletcf56b992018-12-11 16:12:31 +01001491 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1492
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001493 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001494 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001495 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001496 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001497 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001498 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001499 if (h1m->state == H1_MSG_TUNNEL)
1500 h1s->cs->flags |= CS_FL_EOI;
1501 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001502 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001503 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001504
Christopher Faulet6b81df72019-10-01 22:08:43 +02001505 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001506 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001507
1508 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001509 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001510 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001511 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001512 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001513}
1514
Christopher Faulet129817b2018-09-20 16:14:40 +02001515/*
1516 * Process outgoing data. It parses data and transfer them from the channel buffer into
1517 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1518 * 0 if it couldn't proceed.
1519 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001520static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1521{
Christopher Faulet129817b2018-09-20 16:14:40 +02001522 struct h1s *h1s = h1c->h1s;
1523 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001524 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001525 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001526 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001527 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001528 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001529
Christopher Faulet47365272018-10-31 17:40:50 +01001530 if (!count)
1531 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001532
Christopher Faulet94b2c762019-05-24 15:28:57 +02001533 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001534 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1535
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001536 if (htx_is_empty(chn_htx))
1537 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001538
Christopher Faulet51dbc942018-09-13 09:05:15 +02001539 if (!h1_get_buf(h1c, &h1c->obuf)) {
1540 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001541 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 +02001542 goto end;
1543 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001544
Christopher Fauletf2824e62018-10-01 12:12:37 +02001545 if (!conn_is_back(h1c->conn)) {
1546 h1m = &h1s->res;
1547 errflag = H1S_F_RES_ERROR;
1548 }
1549 else {
1550 h1m = &h1s->req;
1551 errflag = H1S_F_REQ_ERROR;
1552 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001553
Christopher Faulet0e54d542019-07-04 21:22:34 +02001554 if (h1s->flags & errflag)
1555 goto end;
1556
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001557 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001558 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001559
Willy Tarreau3815b222018-12-11 19:50:43 +01001560 /* Perform some optimizations to reduce the number of buffer copies.
1561 * First, if the mux's buffer is empty and the htx area contains
1562 * exactly one data block of the same size as the requested count,
1563 * then it's possible to simply swap the caller's buffer with the
1564 * mux's output buffer and adjust offsets and length to match the
1565 * entire DATA HTX block in the middle. In this case we perform a
1566 * true zero-copy operation from end-to-end. This is the situation
1567 * that happens all the time with large files. Second, if this is not
1568 * possible, but the mux's output buffer is empty, we still have an
1569 * opportunity to avoid the copy to the intermediary buffer, by making
1570 * the intermediary buffer's area point to the output buffer's area.
1571 * In this case we want to skip the HTX header to make sure that copies
1572 * remain aligned and that this operation remains possible all the
1573 * time. This goes for headers, data blocks and any data extracted from
1574 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001575 */
1576 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001577 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001578 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001579 htx_get_blk_value(chn_htx, blk).len == count) {
1580 void *old_area = h1c->obuf.area;
1581
Christopher Faulet6b81df72019-10-01 22:08:43 +02001582 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 +01001583 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001584 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001585 h1c->obuf.data = count;
1586
1587 buf->area = old_area;
1588 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001589
Christopher Faulet6b81df72019-10-01 22:08:43 +02001590 chn_htx = (struct htx *)buf->area;
1591 htx_reset(chn_htx);
1592
Christopher Fauletadb22202018-12-12 10:32:09 +01001593 /* The message is chunked. We need to emit the chunk
1594 * size. We have at least the size of the struct htx to
1595 * write the chunk envelope. It should be enough.
1596 */
1597 if (h1m->flags & H1_MF_CHNK) {
1598 h1_emit_chunk_size(&h1c->obuf, count);
1599 h1_emit_chunk_crlf(&h1c->obuf);
1600 }
1601
Willy Tarreau3815b222018-12-11 19:50:43 +01001602 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001603 if (h1m->state == H1_MSG_DATA)
1604 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001605 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001606 else
1607 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001608 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001609 goto out;
1610 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001611 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001612 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001613 else
1614 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001615
Christopher Fauletc2518a52019-06-25 21:41:02 +02001616 tmp.data = 0;
1617 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001618 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001619 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001620 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001621 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001622 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001623 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001624
Christopher Fauletb2e84162018-12-06 11:39:49 +01001625 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001626 if (type != HTX_BLK_DATA && vlen > count)
1627 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001628
Christopher Faulet94b2c762019-05-24 15:28:57 +02001629 if (type == HTX_BLK_UNUSED)
1630 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001631
Christopher Faulet94b2c762019-05-24 15:28:57 +02001632 switch (h1m->state) {
1633 case H1_MSG_RQBEFORE:
1634 if (type != HTX_BLK_REQ_SL)
1635 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001636 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 +02001637 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001638 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001639 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001640 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001641 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001643 if (sl->flags & HTX_SL_F_BODYLESS)
1644 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001646 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001647
Christopher Faulet94b2c762019-05-24 15:28:57 +02001648 case H1_MSG_RPBEFORE:
1649 if (type != HTX_BLK_RES_SL)
1650 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001651 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 +02001652 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001653 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001654 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001655 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001656 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001657 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001658 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001659 if (sl->info.res.status < 200 &&
1660 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001661 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001662 h1m->state = H1_MSG_HDR_FIRST;
1663 break;
1664
Christopher Faulet94b2c762019-05-24 15:28:57 +02001665 case H1_MSG_HDR_FIRST:
1666 case H1_MSG_HDR_NAME:
1667 case H1_MSG_HDR_L2_LWS:
1668 if (type == HTX_BLK_EOH)
1669 goto last_lf;
1670 if (type != HTX_BLK_HDR)
1671 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001672
Christopher Faulet9768c262018-10-22 09:34:31 +02001673 h1m->state = H1_MSG_HDR_NAME;
1674 n = htx_get_blk_name(chn_htx, blk);
1675 v = htx_get_blk_value(chn_htx, blk);
1676
Christopher Faulet86d144c2019-08-14 16:32:25 +02001677 /* Skip all pseudo-headers */
1678 if (*(n.ptr) == ':')
1679 goto skip_hdr;
1680
Christopher Fauletb045bb22020-02-28 10:42:20 +01001681 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001682 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001683 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001684 /* Only skip C-L header with invalid value. */
1685 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001686 goto skip_hdr;
1687 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001688 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001689 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001690 if (!v.len)
1691 goto skip_hdr;
1692 }
1693
Christopher Faulet67d58092019-10-02 10:51:38 +02001694 /* Skip header if same name is used to add the server name */
1695 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1696 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1697 goto skip_hdr;
1698
Christopher Faulet98fbe952019-07-22 16:18:24 +02001699 /* Try to adjust the case of the header name */
1700 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1701 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001702 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001703 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001704 skip_hdr:
1705 h1m->state = H1_MSG_HDR_L2_LWS;
1706 break;
1707
Christopher Faulet94b2c762019-05-24 15:28:57 +02001708 case H1_MSG_LAST_LF:
1709 if (type != HTX_BLK_EOH)
1710 goto error;
1711 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001712 h1m->state = H1_MSG_LAST_LF;
1713 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001714 /* If the reply comes from haproxy while the request is
1715 * not finished, we force the connection close. */
1716 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1717 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1718 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1719 }
1720
1721 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001722 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001723 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001724 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001725 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001726 /* Try to adjust the case of the header name */
1727 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1728 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001729 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001730 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001731 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001732 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001733 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001734
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001735 if ((h1s->meth != HTTP_METH_CONNECT &&
1736 (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 +01001737 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1738 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1739 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1740 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1741 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001742 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001743 n = ist("transfer-encoding");
1744 v = ist("chunked");
1745 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1746 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001747 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001748 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001749 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001750 h1m->flags |= H1_MF_CHNK;
1751 }
1752
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001753 /* Now add the server name to a header (if requested) */
1754 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1755 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1756 struct server *srv = objt_server(h1c->conn->target);
1757
1758 if (srv) {
1759 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1760 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001761
1762 /* Try to adjust the case of the header name */
1763 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1764 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001765 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001766 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001767 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001768 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001769 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1770 }
1771
Christopher Fauletc2518a52019-06-25 21:41:02 +02001772 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001773 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001774
Christopher Faulet6b81df72019-10-01 22:08:43 +02001775 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1776 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1777
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001778 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1779 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1780 h1_set_req_tunnel_mode(h1s);
1781 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001782 else if ((h1m->flags & H1_MF_RESP) &&
1783 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001784 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001785 * to the client. Switch the response to tunnel mode.
1786 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001787 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001788 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 +01001789 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001790 else if ((h1m->flags & H1_MF_RESP) &&
1791 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1792 h1m_init_res(&h1s->res);
1793 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001794 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001795 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001796 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001797 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001798 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001799 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1800 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001801 else
1802 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001803 break;
1804
Christopher Faulet94b2c762019-05-24 15:28:57 +02001805 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001806 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001807 if (type == HTX_BLK_EOM) {
1808 /* Chunked message without explicit trailers */
1809 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001810 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001811 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001812 }
1813 goto done;
1814 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001815 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001816 /* If the message is not chunked, never
1817 * add the last chunk. */
1818 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001819 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001820 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 +02001821 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001822 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001823 else if (type != HTX_BLK_DATA)
1824 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001825
1826 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 +02001827
1828
1829 if (vlen > count) {
1830 /* Get the maximum amount of data we can xferred */
1831 vlen = count;
1832 }
1833
1834 chklen = 0;
1835 if (h1m->flags & H1_MF_CHNK) {
1836 chklen = b_room(&tmp);
1837 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1838 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1839 (chklen < 1048576) ? 5 : 8);
1840 chklen += 4; /* 2 x CRLF */
1841 }
1842
1843 if (vlen + chklen > b_room(&tmp)) {
1844 /* too large for the buffer */
1845 if (chklen >= b_room(&tmp))
1846 goto full;
1847 vlen = b_room(&tmp) - chklen;
1848 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001849 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001850 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001851 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001852 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001853
1854 if (h1m->state == H1_MSG_DATA)
1855 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001856 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001857 else
1858 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001859 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001860 break;
1861
Christopher Faulet94b2c762019-05-24 15:28:57 +02001862 case H1_MSG_TRAILERS:
1863 if (type == HTX_BLK_EOM)
1864 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001865 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001866 goto error;
1867 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001868 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001869 /* If the message is not chunked, ignore
1870 * trailers. It may happen with H2 messages. */
1871 if (!(h1m->flags & H1_MF_CHNK))
1872 break;
1873
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001874 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001875 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001876 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001877 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1878 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001879 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001880 else { // HTX_BLK_TLR
1881 n = htx_get_blk_name(chn_htx, blk);
1882 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001883
1884 /* Try to adjust the case of the header name */
1885 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1886 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001887 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001888 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001889 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001890 break;
1891
Christopher Faulet94b2c762019-05-24 15:28:57 +02001892 case H1_MSG_DONE:
1893 if (type != HTX_BLK_EOM)
1894 goto error;
1895 done:
1896 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001897 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1898 h1_set_req_tunnel_mode(h1s);
1899 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1900 }
1901 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001902 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001903 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001904 TRACE_STATE("h1c no more busy", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001905 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001906
1907 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1908 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001909 break;
1910
Christopher Faulet9768c262018-10-22 09:34:31 +02001911 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001912 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001913 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001914 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001915 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001916 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001917 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001918 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1919 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001920 break;
1921 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001922
1923 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001924 total += vlen;
1925 count -= vlen;
1926 if (sz == vlen)
1927 blk = htx_remove_blk(chn_htx, blk);
1928 else {
1929 htx_cut_data_blk(chn_htx, blk, vlen);
1930 break;
1931 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001932 }
1933
Christopher Faulet9768c262018-10-22 09:34:31 +02001934 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001935 /* when the output buffer is empty, tmp shares the same area so that we
1936 * only have to update pointers and lengths.
1937 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001938 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1939 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001940 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001941 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001942
Willy Tarreau3815b222018-12-11 19:50:43 +01001943 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001944 out:
1945 if (!buf_room_for_htx_data(&h1c->obuf)) {
1946 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001947 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001948 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001949 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001950 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001951 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001952
1953 full:
1954 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1955 h1c->flags |= H1C_F_OUT_FULL;
1956 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001957}
1958
Christopher Faulet51dbc942018-09-13 09:05:15 +02001959/*********************************************************/
1960/* functions below are I/O callbacks from the connection */
1961/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001962static void h1_wake_stream_for_recv(struct h1s *h1s)
1963{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001964 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001965 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001966 tasklet_wakeup(h1s->subs->tasklet);
1967 h1s->subs->events &= ~SUB_RETRY_RECV;
1968 if (!h1s->subs->events)
1969 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001970 }
1971}
1972static void h1_wake_stream_for_send(struct h1s *h1s)
1973{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001974 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001975 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001976 tasklet_wakeup(h1s->subs->tasklet);
1977 h1s->subs->events &= ~SUB_RETRY_SEND;
1978 if (!h1s->subs->events)
1979 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001980 }
1981}
1982
Christopher Faulet51dbc942018-09-13 09:05:15 +02001983/*
1984 * Attempt to read data, and subscribe if none available
1985 */
1986static int h1_recv(struct h1c *h1c)
1987{
1988 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001989 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001990 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001991 int rcvd = 0;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01001992 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001993
Christopher Faulet6b81df72019-10-01 22:08:43 +02001994 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
1995
1996 if (h1c->wait_event.events & SUB_RETRY_RECV) {
1997 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01001998 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001999 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000
Olivier Houchard75159a92018-12-03 18:46:09 +01002001 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002002 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01002003 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002004 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01002005 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002006
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002007 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2008 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002009 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002010 goto end;
2011 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002012
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002013 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002014 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002015 h1_wake_stream_for_recv(h1s);
2016 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002017 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002018 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002019 }
2020
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002021 /*
2022 * If we only have a small amount of data, realign it,
2023 * it's probably cheaper than doing 2 recv() calls.
2024 */
2025 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2026 b_slow_realign(&h1c->ibuf, trash.area, 0);
2027
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002028 /* avoid useless reads after first responses */
2029 if (h1s && (h1s->req.state == H1_MSG_RQBEFORE || h1s->res.state == H1_MSG_RPBEFORE))
2030 flags |= CO_RFL_READ_ONCE;
2031
Willy Tarreau45f2b892018-12-05 07:59:27 +01002032 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002033 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002034 if (h1c->flags & H1C_F_IN_FULL) {
2035 h1c->flags &= ~H1C_F_IN_FULL;
2036 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2037 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002038
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002039 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002040 if (!b_data(&h1c->ibuf)) {
2041 /* try to pre-align the buffer like the rxbufs will be
2042 * to optimize memory copies.
2043 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002044 h1c->ibuf.head = sizeof(struct htx);
2045 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002046 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002047 }
Christopher Faulet47365272018-10-31 17:40:50 +01002048 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002049 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn,,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002050 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002051 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002052 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002053 if (h1s->csinfo.t_idle == -1)
2054 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2055 }
Christopher Faulet47365272018-10-31 17:40:50 +01002056 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002057
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002058 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002059 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002060 goto end;
2061 }
2062
Christopher Faulet6b81df72019-10-01 22:08:43 +02002063 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002064 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002065
Christopher Faulet9768c262018-10-22 09:34:31 +02002066 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002067 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2068 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002069
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002070 if (conn_xprt_read0_pending(conn) && h1s) {
2071 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002072 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002073 rcvd = 1;
2074 }
2075
Christopher Faulet51dbc942018-09-13 09:05:15 +02002076 if (!b_data(&h1c->ibuf))
2077 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002078 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002079 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002080 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2081 }
2082
2083 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002084 return rcvd;
2085}
2086
2087
2088/*
2089 * Try to send data if possible
2090 */
2091static int h1_send(struct h1c *h1c)
2092{
2093 struct connection *conn = h1c->conn;
2094 unsigned int flags = 0;
2095 size_t ret;
2096 int sent = 0;
2097
Christopher Faulet6b81df72019-10-01 22:08:43 +02002098 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2099
2100 if (conn->flags & CO_FL_ERROR) {
2101 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002102 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002103 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002104
2105 if (!b_data(&h1c->obuf))
2106 goto end;
2107
Christopher Faulet46230362019-10-17 16:04:20 +02002108 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002109 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002110 if (h1c->flags & H1C_F_CO_STREAMER)
2111 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002112
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002113 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002114 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002115 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn,,, (size_t[]){ret});
2116 if (h1c->flags & H1C_F_OUT_FULL) {
2117 h1c->flags &= ~H1C_F_OUT_FULL;
2118 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2119 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002120 b_del(&h1c->obuf, ret);
2121 sent = 1;
2122 }
2123
Christopher Faulet145aa472018-12-06 10:56:20 +01002124 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002125 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002126 /* error or output closed, nothing to send, clear the buffer to release it */
2127 b_reset(&h1c->obuf);
2128 }
2129
Christopher Faulet51dbc942018-09-13 09:05:15 +02002130 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002131 if (!(h1c->flags & H1C_F_OUT_FULL))
2132 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002133
Christopher Faulet51dbc942018-09-13 09:05:15 +02002134 /* We're done, no more to send */
2135 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002136 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002137 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002138 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2139 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002140 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002141 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002142 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002143 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2144 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002145 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002146 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002147
Christopher Faulet6b81df72019-10-01 22:08:43 +02002148 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002149 return sent;
2150}
2151
Christopher Faulet51dbc942018-09-13 09:05:15 +02002152
2153/* callback called on any event by the connection handler.
2154 * It applies changes and returns zero, or < 0 if it wants immediate
2155 * destruction of the connection.
2156 */
2157static int h1_process(struct h1c * h1c)
2158{
2159 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002160 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002161
Christopher Faulet6b81df72019-10-01 22:08:43 +02002162 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2163
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002164 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002165 return -1;
2166
Christopher Fauletfeb11742018-11-29 15:12:34 +01002167 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002168 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002169 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002170 goto release;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002171 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002172 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002173 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002174 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002175 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002176 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002177 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002178 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002179 }
2180
Christopher Fauletfeb11742018-11-29 15:12:34 +01002181 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2182 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2183
Christopher Faulet6b81df72019-10-01 22:08:43 +02002184 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002185 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002186 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2187 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002188
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002189 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002190 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002191 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002192 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002193 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002194 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002195 h1s->cs->data_cb->wake(h1s->cs);
2196 }
Christopher Faulet47365272018-10-31 17:40:50 +01002197 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002198 if (h1c->task) {
2199 h1c->task->expire = TICK_ETERNITY;
2200 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002201 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002202 ? h1c->shut_timeout
2203 : h1c->timeout));
2204 task_queue(h1c->task);
2205 }
2206 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002207 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002209
2210 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002211 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002212 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002213 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002214}
2215
2216static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2217{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002218 struct connection *conn;
2219 struct tasklet *tl = (struct tasklet *)t;
2220 int conn_in_list;
2221 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002222 int ret = 0;
2223
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002224
2225 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
2226 if (tl->context == NULL) {
2227 /* The connection has been taken over by another thread,
2228 * we're no longer responsible for it, so just free the
2229 * tasklet, and do nothing.
2230 */
2231 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
2232 tasklet_free(tl);
2233 return NULL;
2234 }
2235 h1c = ctx;
2236 conn = h1c->conn;
2237
2238 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2239
2240 /* Remove the connection from the list, to be sure nobody attempts
2241 * to use it while we handle the I/O events
2242 */
2243 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2244 if (conn_in_list)
2245 MT_LIST_DEL(&conn->list);
2246
2247 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002248
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002249 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002250 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002251 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002252 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002253 if (ret || !h1c->h1s)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002254 ret = h1_process(h1c);
2255 /* If we were in an idle list, we want to add it back into it,
2256 * unless h1_process() returned -1, which mean it has destroyed
2257 * the connection (testing !ret is enough, if h1_process() wasn't
2258 * called then ret will be 0 anyway.
2259 */
2260 if (!ret && conn_in_list) {
2261 struct server *srv = objt_server(conn->target);
2262
2263 if (conn_in_list == CO_FL_SAFE_LIST)
2264 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
2265 else
2266 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
2267 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002268 return NULL;
2269}
2270
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002271static void h1_reset(struct connection *conn)
2272{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002273
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002274}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002275
2276static int h1_wake(struct connection *conn)
2277{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002278 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002279 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002280
Christopher Faulet6b81df72019-10-01 22:08:43 +02002281 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2282
Christopher Faulet539e0292018-11-19 10:40:09 +01002283 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002284 ret = h1_process(h1c);
2285 if (ret == 0) {
2286 struct h1s *h1s = h1c->h1s;
2287
Christopher Faulet6b81df72019-10-01 22:08:43 +02002288 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2289 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002290 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002291 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002292 }
2293 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002294}
2295
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002296/* Connection timeout management. The principle is that if there's no receipt
2297 * nor sending for a certain amount of time, the connection is closed.
2298 */
2299static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2300{
2301 struct h1c *h1c = context;
2302 int expired = tick_is_expired(t->expire, now_ms);
2303
Christopher Faulet6b81df72019-10-01 22:08:43 +02002304 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2305
2306 if (!expired && h1c) {
2307 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002308 return t;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002309 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002310
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002311 /* We're about to destroy the connection, so make sure nobody attempts
2312 * to steal it from us.
2313 */
2314 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
2315
2316 if (h1c && h1c->conn->flags & CO_FL_LIST_MASK)
2317 MT_LIST_DEL(&h1c->conn->list);
2318
2319 /* Somebody already stole the connection from us, so we should not
2320 * free it, we just have to free the task.
2321 */
2322 if (!t->context)
2323 h1c = NULL;
2324
2325 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
2326
Olivier Houchard3f795f72019-04-17 22:51:06 +02002327 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002328
2329 if (!h1c) {
2330 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002331 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002332 return NULL;
2333 }
2334
2335 h1c->task = NULL;
2336 /* If a stream is still attached to the mux, just set an error and wait
2337 * for the stream's timeout. Otherwise, release the mux. This is only ok
2338 * because same timeouts are used.
2339 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002340 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002341 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002342 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2343 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002344 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002345 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002346
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002347 return NULL;
2348}
2349
Christopher Faulet51dbc942018-09-13 09:05:15 +02002350/*******************************************/
2351/* functions below are used by the streams */
2352/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002353
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354/*
2355 * Attach a new stream to a connection
2356 * (Used for outgoing connections)
2357 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002358static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002359{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002360 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002361 struct conn_stream *cs = NULL;
2362 struct h1s *h1s;
2363
Christopher Faulet6b81df72019-10-01 22:08:43 +02002364 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2365 if (h1c->flags & H1C_F_CS_ERROR) {
2366 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 +02002367 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002368 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369
2370 cs = cs_new(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002371 if (!cs) {
2372 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 +02002373 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002374 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002375
Olivier Houchardf502aca2018-12-14 19:42:40 +01002376 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002377 if (h1s == NULL) {
2378 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 +02002379 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002380 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002381
Christopher Faulet6b81df72019-10-01 22:08:43 +02002382 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002383 return cs;
2384 end:
2385 cs_free(cs);
2386 return NULL;
2387}
2388
2389/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2390 * this mux, it's easy as we can only store a single conn_stream.
2391 */
2392static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2393{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002394 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002395 struct h1s *h1s = h1c->h1s;
2396
2397 if (h1s)
2398 return h1s->cs;
2399
2400 return NULL;
2401}
2402
Christopher Faulet73c12072019-04-08 11:23:22 +02002403static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002404{
Christopher Faulet73c12072019-04-08 11:23:22 +02002405 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002406
Christopher Faulet6b81df72019-10-01 22:08:43 +02002407 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002408 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002409 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002410}
2411
2412/*
2413 * Detach the stream from the connection and possibly release the connection.
2414 */
2415static void h1_detach(struct conn_stream *cs)
2416{
2417 struct h1s *h1s = cs->ctx;
2418 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002419 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002420 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002421
Christopher Faulet6b81df72019-10-01 22:08:43 +02002422 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2423
Christopher Faulet51dbc942018-09-13 09:05:15 +02002424 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002425 if (!h1s) {
2426 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002427 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002428 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002429
Olivier Houchardf502aca2018-12-14 19:42:40 +01002430 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002431 h1c = h1s->h1c;
2432 h1s->cs = NULL;
2433
Olivier Houchard8a786902018-12-15 16:05:40 +01002434 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2435 h1s_destroy(h1s);
2436
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002437 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002438 /* If there are any excess server data in the input buffer,
2439 * release it and close the connection ASAP (some data may
2440 * remain in the output buffer). This happens if a server sends
2441 * invalid responses. So in such case, we don't want to reuse
2442 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002443 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002444 if (b_data(&h1c->ibuf)) {
2445 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002446 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002447 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002448 goto release;
2449 }
Christopher Faulet03627242019-07-19 11:34:08 +02002450
Christopher Faulet9400a392018-11-23 23:10:39 +01002451 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002452 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002453 h1c->conn->flags |= CO_FL_PRIVATE;
2454
Olivier Houchard2444aa52020-01-20 13:56:01 +01002455 if (!(h1c->conn->owner) && (h1c->conn->flags & CO_FL_PRIVATE)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002456 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002457 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2458 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002459 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002460 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002461 }
Olivier Houchard2444aa52020-01-20 13:56:01 +01002462 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002463 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002464 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2465 goto end;
2466 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002467 }
Olivier Houchard2444aa52020-01-20 13:56:01 +01002468 if (!(h1c->conn->flags & CO_FL_PRIVATE)) {
2469 if (h1c->conn->owner == sess)
2470 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002471 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002472 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002473 /* The server doesn't want it, let's kill the connection right away */
2474 h1c->conn->mux->destroy(h1c);
2475 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2476 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002477 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002478 /* At this point, the connection has been added to the
2479 * server idle list, so another thread may already have
2480 * hijacked it, so we can't do anything with it.
2481 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002482 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002483 }
2484 }
2485
Christopher Fauletf1204b82019-07-19 14:51:06 +02002486 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002487 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet37243bc2019-07-11 15:40:25 +02002488 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2489 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2490 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002491 !h1c->conn->owner) {
2492 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002493 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002494 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002495 else {
Olivier Houchard69664412020-03-25 12:24:11 +01002496 /* If we have a new request, process it immediately */
Olivier Houchardc3500c32020-03-25 17:05:21 +01002497 if (unlikely(b_data(&h1c->ibuf)))
Olivier Houchard69664412020-03-25 12:24:11 +01002498 h1_process(h1c);
2499 else
2500 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002501 if (h1c->task) {
2502 h1c->task->expire = TICK_ETERNITY;
Willy Tarreauca397472020-04-28 19:48:41 +02002503 if ((!h1c->h1s && !conn_is_back(h1c->conn)) || b_data(&h1c->obuf)) {
2504 /* front connections waiting for a stream, as well as any connection with
2505 * pending data, need a timeout.
2506 */
Christopher Faulet666a0c42019-01-08 11:12:04 +01002507 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002508 ? h1c->shut_timeout
2509 : h1c->timeout));
2510 task_queue(h1c->task);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002511 TRACE_DEVEL("refreshing connection's timeout", H1_EV_STRM_END, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002512 }
2513 }
2514 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002515 end:
2516 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002517}
2518
2519
2520static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2521{
2522 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002523 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002524
2525 if (!h1s)
2526 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002527 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002528
Christopher Faulet6b81df72019-10-01 22:08:43 +02002529 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2530
2531 if (cs->flags & CS_FL_KILL_CONN) {
2532 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2533 goto do_shutr;
2534 }
2535 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2536 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002537 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002538 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002539
Christopher Faulet6b81df72019-10-01 22:08:43 +02002540 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2541 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2542 goto end;
2543 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002544
Christopher Faulet7f366362019-04-08 10:51:20 +02002545 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002546 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2547 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002548 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002549 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002550 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002551 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002552 end:
2553 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002554}
2555
2556static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2557{
2558 struct h1s *h1s = cs->ctx;
2559 struct h1c *h1c;
2560
2561 if (!h1s)
2562 return;
2563 h1c = h1s->h1c;
2564
Christopher Faulet6b81df72019-10-01 22:08:43 +02002565 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2566
2567 if (cs->flags & CS_FL_KILL_CONN) {
2568 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002569 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002570 }
2571 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2572 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2573 goto do_shutw;
2574 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002575
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002576 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002577 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2578 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2579 goto end;
2580 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002581
Christopher Faulet7f366362019-04-08 10:51:20 +02002582 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002583 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2584 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002585 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002586 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002587 end:
2588 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002589}
2590
Christopher Faulet666a0c42019-01-08 11:12:04 +01002591static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002592{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002593 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002594
Christopher Faulet6b81df72019-10-01 22:08:43 +02002595 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002596 conn_xprt_shutw(conn);
2597 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002598 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002599 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002600}
2601
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002602/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2603 * The <es> pointer is not allowed to differ from the one passed to the
2604 * subscribe() call. It always returns zero.
2605 */
2606static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002607{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002608 struct h1s *h1s = cs->ctx;
2609
2610 if (!h1s)
2611 return 0;
2612
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002613 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002614 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002615
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002616 es->events &= ~event_type;
2617 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002618 h1s->subs = NULL;
2619
2620 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002621 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002622
2623 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002624 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002625
Christopher Faulet51dbc942018-09-13 09:05:15 +02002626 return 0;
2627}
2628
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002629/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2630 * event subscriber <es> is not allowed to change from a previous call as long
2631 * as at least one event is still subscribed. The <event_type> must only be a
2632 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2633 * the conn_stream <cs> was already detached, in which case it will return -1.
2634 */
2635static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002636{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002637 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002638 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002639
2640 if (!h1s)
2641 return -1;
2642
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002643 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002644 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002645
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002646 es->events |= event_type;
2647 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002648
2649 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002650 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002651
2652
Christopher Faulet6b81df72019-10-01 22:08:43 +02002653 if (event_type & SUB_RETRY_SEND) {
2654 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002655 /*
2656 * If the conn_stream attempt to subscribe, and the
2657 * mux isn't subscribed to the connection, then it
2658 * probably means the connection wasn't established
2659 * yet, so we have to subscribe.
2660 */
2661 h1c = h1s->h1c;
2662 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2663 h1c->conn->xprt->subscribe(h1c->conn,
2664 h1c->conn->xprt_ctx,
2665 SUB_RETRY_SEND,
2666 &h1c->wait_event);
2667 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002668 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002669}
2670
2671/* Called from the upper layer, to receive data */
2672static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2673{
2674 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002675 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002676 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002677 size_t ret = 0;
2678
Christopher Faulet6b81df72019-10-01 22:08:43 +02002679 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002680 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002681 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002682 else
2683 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002684
Christopher Faulete18777b2019-04-16 16:46:36 +02002685 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002686 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002687 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002688 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2689 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002690 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002691 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002692 if (h1s->flags & H1S_F_SPLICED_DATA) {
2693 h1s->flags &= ~H1S_F_SPLICED_DATA;
2694 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2695 }
2696 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002697 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002698 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002699 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002700 return ret;
2701}
2702
2703
2704/* Called from the upper layer, to send data */
2705static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2706{
2707 struct h1s *h1s = cs->ctx;
2708 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002709 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002710
2711 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002712 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002713 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002714
Christopher Faulet6b81df72019-10-01 22:08:43 +02002715 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){count});
2716
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002717 /* If we're not connected yet, or we're waiting for a handshake, stop
2718 * now, as we don't want to remove everything from the channel buffer
2719 * before we're sure we can send it.
2720 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01002721 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002722 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002723 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002724 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002725
Christopher Faulet46230362019-10-17 16:04:20 +02002726 /* Inherit some flags from the upper layer */
2727 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
2728 if (flags & CO_SFL_MSG_MORE)
2729 h1c->flags |= H1C_F_CO_MSG_MORE;
2730 if (flags & CO_SFL_STREAMER)
2731 h1c->flags |= H1C_F_CO_STREAMER;
2732
Christopher Fauletc31872f2019-06-04 22:09:36 +02002733 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002734 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002735
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002736 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2737 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002738 else
2739 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002740 if (!ret)
2741 break;
2742 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002743 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002744 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002745 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002746 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002747
Christopher Faulet6b81df72019-10-01 22:08:43 +02002748 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002749 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002750}
2751
Willy Tarreaue5733232019-05-22 19:24:06 +02002752#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002753/* Send and get, using splicing */
2754static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2755{
2756 struct h1s *h1s = cs->ctx;
2757 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2758 int ret = 0;
2759
Christopher Faulet6b81df72019-10-01 22:08:43 +02002760 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){count});
2761
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002762 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002763 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002764 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2765 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2766 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002767 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002768 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002769 goto end;
2770 }
2771
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002772 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002773 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002774 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002775 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002776 }
2777
2778 h1s->flags &= ~H1S_F_BUF_FLUSH;
2779 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002780 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002781 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2782 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002783 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002784 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002785 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002786 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002787 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002788 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2789 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002790 }
2791
Christopher Faulet1be55f92018-10-02 15:59:23 +02002792 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002793 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002794 h1s->flags |= H1S_F_REOS;
Christopher Fauletf3158e92019-11-15 11:14:23 +01002795 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002796 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002797 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002798
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002799 if (h1m->state != H1_MSG_DATA || !h1m->curr_len)
2800 cs->flags &= ~CS_FL_MAY_SPLICE;
2801
Christopher Faulet6b81df72019-10-01 22:08:43 +02002802 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002803 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002804}
2805
2806static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2807{
2808 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002809 int ret = 0;
2810
Christopher Faulet6b81df72019-10-01 22:08:43 +02002811 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){pipe->data});
2812
Christopher Faulet1be55f92018-10-02 15:59:23 +02002813 if (b_data(&h1s->h1c->obuf))
2814 goto end;
2815
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002816 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002817 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002818 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002819 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2820 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002821 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002822 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002823 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002824
2825 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002826 return ret;
2827}
2828#endif
2829
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002830static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2831{
2832 int ret = 0;
2833 switch (mux_ctl) {
2834 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01002835 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002836 ret |= MUX_STATUS_READY;
2837 return ret;
2838 default:
2839 return -1;
2840 }
2841}
2842
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002843/* for debugging with CLI's "show fd" command */
2844static void h1_show_fd(struct buffer *msg, struct connection *conn)
2845{
2846 struct h1c *h1c = conn->ctx;
2847 struct h1s *h1s = h1c->h1s;
2848
Christopher Fauletf376a312019-01-04 15:16:06 +01002849 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2850 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002851 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2852 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2853 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2854 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2855
2856 if (h1s) {
2857 char *method;
2858
2859 if (h1s->meth < HTTP_METH_OTHER)
2860 method = http_known_methods[h1s->meth].ptr;
2861 else
2862 method = "UNKNOWN";
2863 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2864 " .meth=%s status=%d",
2865 h1s, h1s->flags,
2866 h1m_state_str(h1s->req.state),
2867 h1m_state_str(h1s->res.state), method, h1s->status);
2868 if (h1s->cs)
2869 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2870 h1s->cs->flags, h1s->cs->data);
2871 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002872}
2873
2874
2875/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2876static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2877{
2878 struct h1_hdr_entry *entry;
2879
2880 /* Be sure there is a non-empty <to> */
2881 if (!strlen(to)) {
2882 memprintf(err, "expect <to>");
2883 return -1;
2884 }
2885
2886 /* Be sure only the case differs between <from> and <to> */
2887 if (strcasecmp(from, to)) {
2888 memprintf(err, "<from> and <to> must not differ execpt the case");
2889 return -1;
2890 }
2891
2892 /* Be sure <from> does not already existsin the tree */
2893 if (ebis_lookup(&hdrs_map.map, from)) {
2894 memprintf(err, "duplicate entry '%s'", from);
2895 return -1;
2896 }
2897
2898 /* Create the entry and insert it in the tree */
2899 entry = malloc(sizeof(*entry));
2900 if (!entry) {
2901 memprintf(err, "out of memory");
2902 return -1;
2903 }
2904
2905 entry->node.key = strdup(from);
2906 entry->name.ptr = strdup(to);
2907 entry->name.len = strlen(to);
2908 if (!entry->node.key || !entry->name.ptr) {
2909 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01002910 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002911 free(entry);
2912 memprintf(err, "out of memory");
2913 return -1;
2914 }
2915 ebis_insert(&hdrs_map.map, &entry->node);
2916 return 0;
2917}
2918
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002919/* Migrate the the connection to the current thread.
2920 * Return 0 if successful, non-zero otherwise.
2921 * Expected to be called with the old thread lock held.
2922 */
2923static int h1_takeover(struct connection *conn)
2924{
2925 struct h1c *h1c = conn->ctx;
2926
2927 if (fd_takeover(conn->handle.fd, conn) != 0)
2928 return -1;
2929 if (h1c->wait_event.events)
2930 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
2931 h1c->wait_event.events, &h1c->wait_event);
2932 /* To let the tasklet know it should free itself, and do nothing else,
2933 * set its context to NULL.
2934 */
2935 h1c->wait_event.tasklet->context = NULL;
2936 tasklet_wakeup(h1c->wait_event.tasklet);
2937 if (h1c->task) {
2938 h1c->task->context = NULL;
2939 /* Wake the task, to let it free itself */
2940 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
2941
2942 h1c->task = task_new(tid_bit);
2943 if (!h1c->task) {
2944 h1_release(h1c);
2945 return -1;
2946 }
2947 h1c->task->process = h1_timeout_task;
2948 h1c->task->context = h1c;
2949 }
2950 h1c->wait_event.tasklet = tasklet_new();
2951 if (!h1c->wait_event.tasklet) {
2952 h1_release(h1c);
2953 return -1;
2954 }
2955 h1c->wait_event.tasklet->process = h1_io_cb;
2956 h1c->wait_event.tasklet->context = h1c;
2957 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
2958 SUB_RETRY_RECV, &h1c->wait_event);
2959
2960 return 0;
2961}
2962
2963
Christopher Faulet98fbe952019-07-22 16:18:24 +02002964static void h1_hdeaders_case_adjust_deinit()
2965{
2966 struct ebpt_node *node, *next;
2967 struct h1_hdr_entry *entry;
2968
2969 node = ebpt_first(&hdrs_map.map);
2970 while (node) {
2971 next = ebpt_next(node);
2972 ebpt_delete(node);
2973 entry = container_of(node, struct h1_hdr_entry, node);
2974 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01002975 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002976 free(entry);
2977 node = next;
2978 }
2979 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002980}
2981
Christopher Faulet98fbe952019-07-22 16:18:24 +02002982static int cfg_h1_headers_case_adjust_postparser()
2983{
2984 FILE *file = NULL;
2985 char *c, *key_beg, *key_end, *value_beg, *value_end;
2986 char *err;
2987 int rc, line = 0, err_code = 0;
2988
2989 if (!hdrs_map.name)
2990 goto end;
2991
2992 file = fopen(hdrs_map.name, "r");
2993 if (!file) {
2994 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2995 hdrs_map.name);
2996 err_code |= ERR_ALERT | ERR_FATAL;
2997 goto end;
2998 }
2999
3000 /* now parse all lines. The file may contain only two header name per
3001 * line, separated by spaces. All heading and trailing spaces will be
3002 * ignored. Lines starting with a # are ignored.
3003 */
3004 while (fgets(trash.area, trash.size, file) != NULL) {
3005 line++;
3006 c = trash.area;
3007
3008 /* strip leading spaces and tabs */
3009 while (*c == ' ' || *c == '\t')
3010 c++;
3011
3012 /* ignore emptu lines, or lines beginning with a dash */
3013 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3014 continue;
3015
3016 /* look for the end of the key */
3017 key_beg = c;
3018 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3019 c++;
3020 key_end = c;
3021
3022 /* strip middle spaces and tabs */
3023 while (*c == ' ' || *c == '\t')
3024 c++;
3025
3026 /* look for the end of the value, it is the end of the line */
3027 value_beg = c;
3028 while (*c && *c != '\n' && *c != '\r')
3029 c++;
3030 value_end = c;
3031
3032 /* trim possibly trailing spaces and tabs */
3033 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3034 value_end--;
3035
3036 /* set final \0 and check entries */
3037 *key_end = '\0';
3038 *value_end = '\0';
3039
3040 err = NULL;
3041 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3042 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003043 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3044 hdrs_map.name, err, line);
3045 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003046 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003047 goto end;
3048 }
3049 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003050 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3051 hdrs_map.name, err, line);
3052 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003053 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003054 }
3055 }
3056
3057 end:
3058 if (file)
3059 fclose(file);
3060 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3061 return err_code;
3062}
3063
3064
3065/* config parser for global "h1-outgoing-header-case-adjust" */
3066static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3067 struct proxy *defpx, const char *file, int line,
3068 char **err)
3069{
3070 if (too_many_args(2, args, err, NULL))
3071 return -1;
3072 if (!*(args[1]) || !*(args[2])) {
3073 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3074 return -1;
3075 }
3076 return add_hdr_case_adjust(args[1], args[2], err);
3077}
3078
3079/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3080static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3081 struct proxy *defpx, const char *file, int line,
3082 char **err)
3083{
3084 if (too_many_args(1, args, err, NULL))
3085 return -1;
3086 if (!*(args[1])) {
3087 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3088 return -1;
3089 }
3090 free(hdrs_map.name);
3091 hdrs_map.name = strdup(args[1]);
3092 return 0;
3093}
3094
3095
3096/* config keyword parsers */
3097static struct cfg_kw_list cfg_kws = {{ }, {
3098 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3099 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3100 { 0, NULL, NULL },
3101 }
3102};
3103
3104INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3105REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3106
3107
Christopher Faulet51dbc942018-09-13 09:05:15 +02003108/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003109/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003110/****************************************/
3111
3112/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003113static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003114 .init = h1_init,
3115 .wake = h1_wake,
3116 .attach = h1_attach,
3117 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01003118 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003119 .detach = h1_detach,
3120 .destroy = h1_destroy,
3121 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003122 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003123 .rcv_buf = h1_rcv_buf,
3124 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003125#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003126 .rcv_pipe = h1_rcv_pipe,
3127 .snd_pipe = h1_snd_pipe,
3128#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003129 .subscribe = h1_subscribe,
3130 .unsubscribe = h1_unsubscribe,
3131 .shutr = h1_shutr,
3132 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003133 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003134 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003135 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003136 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003137 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003138 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003139};
3140
3141
3142/* this mux registers default HTX proto */
3143static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003144{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003145
Willy Tarreau0108d902018-11-25 19:14:37 +01003146INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3147
Christopher Faulet51dbc942018-09-13 09:05:15 +02003148/*
3149 * Local variables:
3150 * c-indent-level: 8
3151 * c-basic-offset: 8
3152 * End:
3153 */