blob: eed716b2ee8b19b9ba2f2b59f1d3e17050490a1e [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 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Christopher Faulet0ef372a2019-04-08 10:57:20 +020015#include <common/h2.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Christopher Faulet98fbe952019-07-22 16:18:24 +020019#include <ebistree.h>
20
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 Faulet51dbc942018-09-13 09:05:15 +020060/*
61 * H1 Stream flags (32 bits)
62 */
Christopher Faulet129817b2018-09-20 16:14:40 +020063#define H1S_F_NONE 0x00000000
64#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020065#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
66#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020067#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020068#define H1S_F_WANT_KAL 0x00000010
69#define H1S_F_WANT_TUN 0x00000020
70#define H1S_F_WANT_CLO 0x00000040
71#define H1S_F_WANT_MSK 0x00000070
72#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010073#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010074#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet76014fd2019-12-10 11:47:22 +010075#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
76/* 0x00000800 .. 0x00001000 unused */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020077#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 +020078#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020079
80/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020081struct h1c {
82 struct connection *conn;
83 struct proxy *px;
84 uint32_t flags; /* Connection flags: H1C_F_* */
85
86 struct buffer ibuf; /* Input buffer to store data before parsing */
87 struct buffer obuf; /* Output buffer to store data after reformatting */
88
89 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
90 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
91
92 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010093 struct task *task; /* timeout management task */
94 int timeout; /* idle timeout duration in ticks */
95 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096};
97
98/* H1 stream descriptor */
99struct h1s {
100 struct h1c *h1c;
101 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100102 struct cs_info csinfo; /* CS info, only used for client connections */
103 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200104
Christopher Faulet51dbc942018-09-13 09:05:15 +0200105 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
106 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200107
Olivier Houchardf502aca2018-12-14 19:42:40 +0100108 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200109 struct h1m req;
110 struct h1m res;
111
112 enum http_meth_t meth; /* HTTP resquest method */
113 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200114};
115
Christopher Faulet98fbe952019-07-22 16:18:24 +0200116/* Map of headers used to convert outgoing headers */
117struct h1_hdrs_map {
118 char *name;
119 struct eb_root map;
120};
121
122/* An entry in a headers map */
123struct h1_hdr_entry {
124 struct ist name;
125 struct ebpt_node node;
126};
127
128/* Declare the headers map */
129static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
130
131
Christopher Faulet6b81df72019-10-01 22:08:43 +0200132/* trace source and events */
133static void h1_trace(enum trace_level level, uint64_t mask,
134 const struct trace_source *src,
135 const struct ist where, const struct ist func,
136 const void *a1, const void *a2, const void *a3, const void *a4);
137
138/* The event representation is split like this :
139 * h1c - internal H1 connection
140 * h1s - internal H1 stream
141 * strm - application layer
142 * rx - data receipt
143 * tx - data transmission
144 *
145 */
146static const struct trace_event h1_trace_events[] = {
147#define H1_EV_H1C_NEW (1ULL << 0)
148 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
149#define H1_EV_H1C_RECV (1ULL << 1)
150 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
151#define H1_EV_H1C_SEND (1ULL << 2)
152 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
153#define H1_EV_H1C_BLK (1ULL << 3)
154 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
155#define H1_EV_H1C_WAKE (1ULL << 4)
156 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
157#define H1_EV_H1C_END (1ULL << 5)
158 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
159#define H1_EV_H1C_ERR (1ULL << 6)
160 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
161
162#define H1_EV_RX_DATA (1ULL << 7)
163 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
164#define H1_EV_RX_EOI (1ULL << 8)
165 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
166#define H1_EV_RX_HDRS (1ULL << 9)
167 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
168#define H1_EV_RX_BODY (1ULL << 10)
169 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
170#define H1_EV_RX_TLRS (1ULL << 11)
171 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
172
173#define H1_EV_TX_DATA (1ULL << 12)
174 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
175#define H1_EV_TX_EOI (1ULL << 13)
176 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
177#define H1_EV_TX_HDRS (1ULL << 14)
178 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
179#define H1_EV_TX_BODY (1ULL << 15)
180 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
181#define H1_EV_TX_TLRS (1ULL << 16)
182 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
183
184#define H1_EV_H1S_NEW (1ULL << 17)
185 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
186#define H1_EV_H1S_BLK (1ULL << 18)
187 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
188#define H1_EV_H1S_END (1ULL << 19)
189 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
190#define H1_EV_H1S_ERR (1ULL << 20)
191 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
192
193#define H1_EV_STRM_NEW (1ULL << 21)
194 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
195#define H1_EV_STRM_RECV (1ULL << 22)
196 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
197#define H1_EV_STRM_SEND (1ULL << 23)
198 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
199#define H1_EV_STRM_WAKE (1ULL << 24)
200 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
201#define H1_EV_STRM_SHUT (1ULL << 25)
202 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
203#define H1_EV_STRM_END (1ULL << 26)
204 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
205#define H1_EV_STRM_ERR (1ULL << 27)
206 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
207
208 { }
209};
210
211static const struct name_desc h1_trace_lockon_args[4] = {
212 /* arg1 */ { /* already used by the connection */ },
213 /* arg2 */ { .name="h1s", .desc="H1 stream" },
214 /* arg3 */ { },
215 /* arg4 */ { }
216};
217
218static const struct name_desc h1_trace_decoding[] = {
219#define H1_VERB_CLEAN 1
220 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
221#define H1_VERB_MINIMAL 2
222 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
223#define H1_VERB_SIMPLE 3
224 { .name="simple", .desc="add request/response status line or htx info when available" },
225#define H1_VERB_ADVANCED 4
226 { .name="advanced", .desc="add header fields or frame decoding when available" },
227#define H1_VERB_COMPLETE 5
228 { .name="complete", .desc="add full data dump when available" },
229 { /* end */ }
230};
231
232static struct trace_source trace_h1 = {
233 .name = IST("h1"),
234 .desc = "HTTP/1 multiplexer",
235 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
236 .default_cb = h1_trace,
237 .known_events = h1_trace_events,
238 .lockon_args = h1_trace_lockon_args,
239 .decoding = h1_trace_decoding,
240 .report_events = ~0, // report everything by default
241};
242
243#define TRACE_SOURCE &trace_h1
244INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
245
Christopher Faulet51dbc942018-09-13 09:05:15 +0200246/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100247DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
248DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249
Christopher Faulet51dbc942018-09-13 09:05:15 +0200250static int h1_recv(struct h1c *h1c);
251static int h1_send(struct h1c *h1c);
252static int h1_process(struct h1c *h1c);
253static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100254static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100255static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200256static void h1_wake_stream_for_recv(struct h1s *h1s);
257static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200258
Christopher Faulet6b81df72019-10-01 22:08:43 +0200259/* the H1 traces always expect that arg1, if non-null, is of type connection
260 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
261 * that arg3, if non-null, is a htx for rx/tx headers.
262 */
263static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
264 const struct ist where, const struct ist func,
265 const void *a1, const void *a2, const void *a3, const void *a4)
266{
267 const struct connection *conn = a1;
268 const struct h1c *h1c = conn ? conn->ctx : NULL;
269 const struct h1s *h1s = a2;
270 const struct htx *htx = a3;
271 const size_t *val = a4;
272
273 if (!h1c)
274 h1c = (h1s ? h1s->h1c : NULL);
275
276 if (!h1c || src->verbosity < H1_VERB_CLEAN)
277 return;
278
279 /* Display frontend/backend info by default */
280 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
281
282 /* Display request and response states if h1s is defined */
283 if (h1s)
284 chunk_appendf(&trace_buf, " [%s, %s]",
285 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
286
287 if (src->verbosity == H1_VERB_CLEAN)
288 return;
289
290 /* Display the value to the 4th argument (level > STATE) */
291 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100292 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200293
294 /* Display status-line if possible (verbosity > MINIMAL) */
295 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
296 const struct htx_blk *blk = htx_get_head_blk(htx);
297 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
298 enum htx_blk_type type = htx_get_blk_type(blk);
299
300 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
301 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
302 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
303 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
304 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
305 }
306
307 /* Display h1c info and, if defined, h1s info (pointer + flags) */
308 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
309 if (h1s)
310 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
311
312 if (src->verbosity == H1_VERB_MINIMAL)
313 return;
314
315 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
316 if (src->level > TRACE_LEVEL_USER) {
317 if (src->verbosity == H1_VERB_COMPLETE ||
318 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
319 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
320 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
321 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
322 if (src->verbosity == H1_VERB_COMPLETE ||
323 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
324 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
325 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
326 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
327 }
328
329 /* Display htx info if defined (level > USER) */
330 if (src->level > TRACE_LEVEL_USER && htx) {
331 int full = 0;
332
333 /* Full htx info (level > STATE && verbosity > SIMPLE) */
334 if (src->level > TRACE_LEVEL_STATE) {
335 if (src->verbosity == H1_VERB_COMPLETE)
336 full = 1;
337 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
338 full = 1;
339 }
340
341 chunk_memcat(&trace_buf, "\n\t", 2);
342 htx_dump(&trace_buf, htx, full);
343 }
344}
345
346
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347/*****************************************************/
348/* functions below are for dynamic buffer management */
349/*****************************************************/
350/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100351 * Indicates whether or not we may receive data. The rules are the following :
352 * - if an error or a shutdown for reads was detected on the connection we
353 must not attempt to receive
354 * - if the input buffer failed to be allocated or is full , we must not try
355 * to receive
356 * - if he input processing is busy waiting for the output side, we may
357 * attemp to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200358 * - otherwise must may not attempt to receive
359 */
360static inline int h1_recv_allowed(const struct h1c *h1c)
361{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100362 if (h1c->flags & H1C_F_CS_ERROR) {
363 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 +0200364 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200365 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100367 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200368 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 +0100369 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200370 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100371
Christopher Fauletcb55f482018-12-10 11:56:47 +0100372 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200373 return 1;
374
Christopher Faulet6b81df72019-10-01 22:08:43 +0200375 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 +0200376 return 0;
377}
378
379/*
380 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
381 * flags are used to figure what buffer was requested. It returns 1 if the
382 * allocation succeeds, in which case the connection is woken up, or 0 if it's
383 * impossible to wake up and we prefer to be woken up later.
384 */
385static int h1_buf_available(void *target)
386{
387 struct h1c *h1c = target;
388
389 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200390 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 +0200391 h1c->flags &= ~H1C_F_IN_ALLOC;
392 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200393 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200394 return 1;
395 }
396
397 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200398 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 +0200399 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200400 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200401 if (h1c->h1s)
402 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200403 return 1;
404 }
405
Christopher Faulet51dbc942018-09-13 09:05:15 +0200406 return 0;
407}
408
409/*
410 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
411 */
412static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
413{
414 struct buffer *buf = NULL;
415
416 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
417 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
418 h1c->buf_wait.target = h1c;
419 h1c->buf_wait.wakeup_cb = h1_buf_available;
420 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
421 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
422 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
423 __conn_xprt_stop_recv(h1c->conn);
424 }
425 return buf;
426}
427
428/*
429 * Release a buffer, if any, and try to wake up entities waiting in the buffer
430 * wait queue.
431 */
432static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
433{
434 if (bptr->size) {
435 b_free(bptr);
436 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
437 }
438}
439
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100440/* returns the number of streams in use on a connection to figure if it's idle
441 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
442 * not. This flag is only set when no H1S is attached and when the previous
443 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100444 */
445static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200446{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100447 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200448
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100449 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200450}
451
Willy Tarreau00f18a32019-01-26 12:19:01 +0100452/* returns the number of streams still available on a connection */
453static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100454{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100455 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100456}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200457
Willy Tarreau00f18a32019-01-26 12:19:01 +0100458
Christopher Faulet51dbc942018-09-13 09:05:15 +0200459/*****************************************************************/
460/* functions below are dedicated to the mux setup and management */
461/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200462
463/* returns non-zero if there are input data pending for stream h1s. */
464static inline size_t h1s_data_pending(const struct h1s *h1s)
465{
466 const struct h1m *h1m;
467
468 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
469 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100470 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200471
472 return b_data(&h1s->h1c->ibuf);
473}
474
Christopher Faulet47365272018-10-31 17:40:50 +0100475static struct conn_stream *h1s_new_cs(struct h1s *h1s)
476{
477 struct conn_stream *cs;
478
Christopher Faulet6b81df72019-10-01 22:08:43 +0200479 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100480 cs = cs_new(h1s->h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200481 if (!cs) {
482 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 +0100483 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200484 }
Christopher Faulet47365272018-10-31 17:40:50 +0100485 h1s->cs = cs;
486 cs->ctx = h1s;
487
488 if (h1s->flags & H1S_F_NOT_FIRST)
489 cs->flags |= CS_FL_NOT_FIRST;
490
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100491 if (global.tune.options & GTUNE_USE_SPLICE)
492 cs->flags |= CS_FL_MAY_SPLICE;
493
Christopher Faulet6b81df72019-10-01 22:08:43 +0200494 if (stream_create_from_cs(cs) < 0) {
495 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 +0100496 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200497 }
498
499 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100500 return cs;
501
502 err:
503 cs_free(cs);
504 h1s->cs = NULL;
505 return NULL;
506}
507
Olivier Houchardf502aca2018-12-14 19:42:40 +0100508static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200509{
510 struct h1s *h1s;
511
Christopher Faulet6b81df72019-10-01 22:08:43 +0200512 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
513
Christopher Faulet51dbc942018-09-13 09:05:15 +0200514 h1s = pool_alloc(pool_head_h1s);
515 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100516 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200517
518 h1s->h1c = h1c;
519 h1c->h1s = h1s;
520
Olivier Houchardf502aca2018-12-14 19:42:40 +0100521 h1s->sess = sess;
522
Christopher Faulet51dbc942018-09-13 09:05:15 +0200523 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100524 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200525
526 h1s->recv_wait = NULL;
527 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200528
529 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100530 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200531
Christopher Faulet129817b2018-09-20 16:14:40 +0200532 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100533 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200534
535 h1s->status = 0;
536 h1s->meth = HTTP_METH_OTHER;
537
Christopher Faulet47365272018-10-31 17:40:50 +0100538 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
539 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100540 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100541
Christopher Faulet129817b2018-09-20 16:14:40 +0200542 if (!conn_is_back(h1c->conn)) {
543 if (h1c->px->options2 & PR_O2_REQBUG_OK)
544 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200545
546 /* For frontend connections we should always have a session */
547 if (!sess)
548 sess = h1c->conn->owner;
549
Dave Pirotte234740f2019-07-10 13:57:38 +0000550 /* Timers for subsequent sessions on the same HTTP 1.x connection
551 * measure from `now`, not from the connection accept time */
552 if (h1s->flags & H1S_F_NOT_FIRST) {
553 h1s->csinfo.create_date = date;
554 h1s->csinfo.tv_create = now;
555 h1s->csinfo.t_handshake = 0;
556 h1s->csinfo.t_idle = -1;
557 }
558 else {
559 h1s->csinfo.create_date = sess->accept_date;
560 h1s->csinfo.tv_create = sess->tv_accept;
561 h1s->csinfo.t_handshake = sess->t_handshake;
562 h1s->csinfo.t_idle = -1;
563 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200564 }
565 else {
566 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
567 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200568
Christopher Fauletfeb11742018-11-29 15:12:34 +0100569 h1s->csinfo.create_date = date;
570 h1s->csinfo.tv_create = now;
571 h1s->csinfo.t_handshake = 0;
572 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200573 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100574
Christopher Faulete9b70722019-04-08 10:46:02 +0200575 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
576 * create a new one.
577 */
578 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200579 cs->ctx = h1s;
580 h1s->cs = cs;
581 }
Christopher Faulet47365272018-10-31 17:40:50 +0100582 else {
583 cs = h1s_new_cs(h1s);
584 if (!cs)
585 goto fail;
586 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200587 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200588 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100589
590 fail:
591 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200592 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 +0100593 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200594}
595
596static void h1s_destroy(struct h1s *h1s)
597{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200598 if (h1s) {
599 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200600
Christopher Faulet6b81df72019-10-01 22:08:43 +0200601 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200602 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200603
Christopher Fauletf2824e62018-10-01 12:12:37 +0200604 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100605 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200606 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100607 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200608
Christopher Fauletcb55f482018-12-10 11:56:47 +0100609 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200610 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100611 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200612 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
613 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100614
615 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
616 !(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 +0100617 (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 +0100618 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
619 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
620 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
621 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200622 pool_free(pool_head_h1s, h1s);
623 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200624}
625
Christopher Fauletfeb11742018-11-29 15:12:34 +0100626static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
627{
628 struct h1s *h1s = cs->ctx;
629
630 if (h1s && !conn_is_back(cs->conn))
631 return &h1s->csinfo;
632 return NULL;
633}
634
Christopher Faulet51dbc942018-09-13 09:05:15 +0200635/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200636 * Initialize the mux once it's attached. It is expected that conn->ctx points
637 * to the existing conn_stream (for outgoing connections or for incoming onces
638 * during a mux upgrade) or NULL (for incoming ones during the connexion
639 * establishment). <input> is always used as Input buffer and may contain
640 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
641 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200642 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200643static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
644 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200645{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200646 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100647 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200648 void *conn_ctx = conn->ctx;
649
650 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200651
652 h1c = pool_alloc(pool_head_h1c);
653 if (!h1c)
654 goto fail_h1c;
655 h1c->conn = conn;
656 h1c->px = proxy;
657
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100658 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200659 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200660 h1c->obuf = BUF_NULL;
661 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200662 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200663
Christopher Faulet51dbc942018-09-13 09:05:15 +0200664 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200665 h1c->wait_event.tasklet = tasklet_new();
666 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200667 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200668 h1c->wait_event.tasklet->process = h1_io_cb;
669 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100670 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200671
Christopher Faulete9b70722019-04-08 10:46:02 +0200672 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100673 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
674 if (tick_isset(proxy->timeout.serverfin))
675 h1c->shut_timeout = proxy->timeout.serverfin;
676 } else {
677 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
678 if (tick_isset(proxy->timeout.clientfin))
679 h1c->shut_timeout = proxy->timeout.clientfin;
680 }
681 if (tick_isset(h1c->timeout)) {
682 t = task_new(tid_bit);
683 if (!t)
684 goto fail;
685
686 h1c->task = t;
687 t->process = h1_timeout_task;
688 t->context = h1c;
689 t->expire = tick_add(now_ms, h1c->timeout);
690 }
691
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100692 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200693
Christopher Faulet6b81df72019-10-01 22:08:43 +0200694 /* Always Create a new H1S */
695 if (!h1s_create(h1c, conn_ctx, sess))
696 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100697
698 if (t)
699 task_queue(t);
700
Christopher Faulet51dbc942018-09-13 09:05:15 +0200701 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200702 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200703
704 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200705 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200706 return 0;
707
708 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200709 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200710 if (h1c->wait_event.tasklet)
711 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200712 pool_free(pool_head_h1c, h1c);
713 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200714 conn->ctx = conn_ctx; // restore saved context
715 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200716 return -1;
717}
718
Christopher Faulet73c12072019-04-08 11:23:22 +0200719/* release function. This one should be called to free all resources allocated
720 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200721 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200722static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200723{
Christopher Faulet61840e72019-04-15 09:33:32 +0200724 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200725
Christopher Faulet6b81df72019-10-01 22:08:43 +0200726 TRACE_POINT(H1_EV_H1C_END);
727
Christopher Faulet51dbc942018-09-13 09:05:15 +0200728 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200729 /* The connection must be aattached to this mux to be released */
730 if (h1c->conn && h1c->conn->ctx == h1c)
731 conn = h1c->conn;
732
Christopher Faulet6b81df72019-10-01 22:08:43 +0200733 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
734
Christopher Faulet61840e72019-04-15 09:33:32 +0200735 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200736 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200737 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200738 /* Make sure we're no longer subscribed to anything */
739 if (h1c->wait_event.events)
740 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
741 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200742 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200743 /* connection successfully upgraded to H2, this
744 * mux was already released */
745 return;
746 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200747 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200748 sess_log(conn->owner); /* Log if the upgrade failed */
749 }
750
Olivier Houchard45c44372019-06-11 14:06:23 +0200751
Christopher Faulet51dbc942018-09-13 09:05:15 +0200752 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
753 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
754 LIST_DEL(&h1c->buf_wait.list);
755 LIST_INIT(&h1c->buf_wait.list);
756 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
757 }
758
759 h1_release_buf(h1c, &h1c->ibuf);
760 h1_release_buf(h1c, &h1c->obuf);
761
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100762 if (h1c->task) {
763 h1c->task->context = NULL;
764 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
765 h1c->task = NULL;
766 }
767
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200768 if (h1c->wait_event.tasklet)
769 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200770
Christopher Fauletf2824e62018-10-01 12:12:37 +0200771 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200772 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100773 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200774 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200775 pool_free(pool_head_h1c, h1c);
776 }
777
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200778 if (conn) {
779 conn->mux = NULL;
780 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200781 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200782
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200783 conn_stop_tracking(conn);
784 conn_full_close(conn);
785 if (conn->destroy_cb)
786 conn->destroy_cb(conn);
787 conn_free(conn);
788 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200789}
790
791/******************************************************/
792/* functions below are for the H1 protocol processing */
793/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200794/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
795 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200796 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100797static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200798{
Christopher Faulet570d1612018-11-26 11:13:57 +0100799 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200800
Christopher Faulet570d1612018-11-26 11:13:57 +0100801 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200802 (*(p + 5) > '1' ||
803 (*(p + 5) == '1' && *(p + 7) >= '1')))
804 h1m->flags |= H1_MF_VER_11;
805}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200806
Christopher Faulet9768c262018-10-22 09:34:31 +0200807/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
808 * greater or equal to 1.1
809 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100810static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200811{
Christopher Faulet570d1612018-11-26 11:13:57 +0100812 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200813
Christopher Faulet570d1612018-11-26 11:13:57 +0100814 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200815 (*(p + 5) > '1' ||
816 (*(p + 5) == '1' && *(p + 7) >= '1')))
817 h1m->flags |= H1_MF_VER_11;
818}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200819
Christopher Fauletf2824e62018-10-01 12:12:37 +0200820/* Deduce the connection mode of the client connection, depending on the
821 * configuration and the H1 message flags. This function is called twice, the
822 * first time when the request is parsed and the second time when the response
823 * is parsed.
824 */
825static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
826{
827 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200828
829 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100830 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200831 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100832 h1s->status == 101) {
833 /* Either we've established an explicit tunnel, or we're
834 * switching the protocol. In both cases, we're very unlikely to
835 * understand the next protocols. We have to switch to tunnel
836 * mode, so that we transfer the request and responses then let
837 * this protocol pass unmodified. When we later implement
838 * specific parsers for such protocols, we'll want to check the
839 * Upgrade header which contains information about that protocol
840 * for responses with status 101 (eg: see RFC2817 about TLS).
841 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200842 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200843 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 +0100844 }
845 else if (h1s->flags & H1S_F_WANT_KAL) {
846 /* By default the client is in KAL mode. CLOSE mode mean
847 * it is imposed by the client itself. So only change
848 * KAL mode here. */
849 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
850 /* no length known or explicit close => close */
851 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200852 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 +0100853 }
854 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
855 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
856 /* no explict keep-alive and option httpclose => close */
857 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200858 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 +0100859 }
860 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200861 }
862 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100863 /* Input direction: first pass */
864 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
865 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200866 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200867 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 +0100868 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200869 }
870
871 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200872 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200873 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200874 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);
875 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200876}
877
878/* Deduce the connection mode of the client connection, depending on the
879 * configuration and the H1 message flags. This function is called twice, the
880 * first time when the request is parsed and the second time when the response
881 * is parsed.
882 */
883static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
884{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100885 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100886 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100887 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200888
Christopher Fauletf2824e62018-10-01 12:12:37 +0200889 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100890 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200891 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100892 h1s->status == 101) {
893 /* Either we've established an explicit tunnel, or we're
894 * switching the protocol. In both cases, we're very unlikely to
895 * understand the next protocols. We have to switch to tunnel
896 * mode, so that we transfer the request and responses then let
897 * this protocol pass unmodified. When we later implement
898 * specific parsers for such protocols, we'll want to check the
899 * Upgrade header which contains information about that protocol
900 * for responses with status 101 (eg: see RFC2817 about TLS).
901 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200902 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200903 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 +0100904 }
905 else if (h1s->flags & H1S_F_WANT_KAL) {
906 /* By default the server is in KAL mode. CLOSE mode mean
907 * it is imposed by haproxy itself. So only change KAL
908 * mode here. */
909 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
910 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
911 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
912 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200913 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 +0100914 }
915 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200916 }
Christopher Faulet70033782018-12-05 13:50:11 +0100917 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100918 /* Output direction: first pass */
919 if (h1m->flags & H1_MF_CONN_CLO) {
920 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100921 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200922 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 +0100923 }
924 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
925 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
926 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
927 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
928 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
929 /* no explicit keep-alive option httpclose/server-close => close */
930 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200931 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 +0100932 }
Christopher Faulet70033782018-12-05 13:50:11 +0100933 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200934
935 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200936 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200937 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200938 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);
939 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200940}
941
Christopher Fauletb992af02019-03-28 15:42:24 +0100942static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200943{
944 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200945
946 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
947 * token is found
948 */
949 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200950 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200951
952 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200953 if (!(h1m->flags & H1_MF_VER_11)) {
954 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 +0100955 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200956 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200957 }
958 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200959 if (h1m->flags & H1_MF_VER_11) {
960 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100961 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200962 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200963 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200964}
965
Christopher Fauletb992af02019-03-28 15:42:24 +0100966static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200967{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200968 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
969 * token is found
970 */
971 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200972 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200973
974 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100975 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +0200976 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
977 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 +0100978 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200979 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200980 }
981 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200982 if (h1m->flags & H1_MF_VER_11) {
983 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100984 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200985 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200986 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200987}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200988
Christopher Fauletb992af02019-03-28 15:42:24 +0100989static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200990{
Christopher Fauletb992af02019-03-28 15:42:24 +0100991 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200992 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100993 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200994 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200995}
996
Christopher Fauletb992af02019-03-28 15:42:24 +0100997static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
998{
999 if (!conn_is_back(h1s->h1c->conn))
1000 h1_set_cli_conn_mode(h1s, h1m);
1001 else
1002 h1_set_srv_conn_mode(h1s, h1m);
1003
1004 if (!(h1m->flags & H1_MF_RESP))
1005 h1_update_req_conn_value(h1s, h1m, conn_val);
1006 else
1007 h1_update_res_conn_value(h1s, h1m, conn_val);
1008}
Christopher Faulete44769b2018-11-29 23:01:45 +01001009
Christopher Faulet98fbe952019-07-22 16:18:24 +02001010/* Try to adjust the case of the message header name using the global map
1011 * <hdrs_map>.
1012 */
1013static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1014{
1015 struct ebpt_node *node;
1016 struct h1_hdr_entry *entry;
1017
1018 /* No entry in the map, do nothing */
1019 if (eb_is_empty(&hdrs_map.map))
1020 return;
1021
1022 /* No conversion fo the request headers */
1023 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1024 return;
1025
1026 /* No conversion fo the response headers */
1027 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1028 return;
1029
1030 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1031 if (!node)
1032 return;
1033 entry = container_of(node, struct h1_hdr_entry, node);
1034 name->ptr = entry->name.ptr;
1035 name->len = entry->name.len;
1036}
1037
Christopher Faulete44769b2018-11-29 23:01:45 +01001038/* Append the description of what is present in error snapshot <es> into <out>.
1039 * The description must be small enough to always fit in a buffer. The output
1040 * buffer may be the trash so the trash must not be used inside this function.
1041 */
1042static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1043{
1044 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001045 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1046 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1047 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1048 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1049 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1050 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001051}
1052/*
1053 * Capture a bad request or response and archive it in the proxy's structure.
1054 * By default it tries to report the error position as h1m->err_pos. However if
1055 * this one is not set, it will then report h1m->next, which is the last known
1056 * parsing point. The function is able to deal with wrapping buffers. It always
1057 * displays buffers as a contiguous area starting at buf->p. The direction is
1058 * determined thanks to the h1m's flags.
1059 */
1060static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1061 struct h1m *h1m, struct buffer *buf)
1062{
1063 struct session *sess = h1c->conn->owner;
1064 struct proxy *proxy = h1c->px;
1065 struct proxy *other_end = sess->fe;
1066 union error_snapshot_ctx ctx;
1067
Willy Tarreau598d7fc2018-12-18 18:10:38 +01001068 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +01001069 other_end = si_strm(h1s->cs->data)->be;
1070
1071 /* http-specific part now */
1072 ctx.h1.state = h1m->state;
1073 ctx.h1.c_flags = h1c->flags;
1074 ctx.h1.s_flags = h1s->flags;
1075 ctx.h1.m_flags = h1m->flags;
1076 ctx.h1.m_clen = h1m->curr_len;
1077 ctx.h1.m_blen = h1m->body_len;
1078
1079 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1080 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001081 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1082 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001083}
1084
Christopher Fauletadb22202018-12-12 10:32:09 +01001085/* Emit the chunksize followed by a CRLF in front of data of the buffer
1086 * <buf>. It goes backwards and starts with the byte before the buffer's
1087 * head. The caller is responsible for ensuring there is enough room left before
1088 * the buffer's head for the string.
1089 */
1090static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1091{
1092 char *beg, *end;
1093
1094 beg = end = b_head(buf);
1095 *--beg = '\n';
1096 *--beg = '\r';
1097 do {
1098 *--beg = hextab[chksz & 0xF];
1099 } while (chksz >>= 4);
1100 buf->head -= (end - beg);
1101 b_add(buf, end - beg);
1102}
1103
1104/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1105 * ensuring there is enough room left in the buffer for the string. */
1106static void h1_emit_chunk_crlf(struct buffer *buf)
1107{
1108 *(b_peek(buf, b_data(buf))) = '\r';
1109 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1110 b_add(buf, 2);
1111}
1112
Christopher Faulet129817b2018-09-20 16:14:40 +02001113/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001114 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001115 * CONNECT requests. On the client side, if the response is not finished, the
1116 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001117 */
1118static void h1_set_req_tunnel_mode(struct h1s *h1s)
1119{
1120 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1121 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001122 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1123
Christopher Faulet76014fd2019-12-10 11:47:22 +01001124 if (!conn_is_back(h1s->h1c->conn)) {
1125 h1s->flags &= ~H1S_F_PARSING_DONE;
1126 if (h1s->res.state < H1_MSG_DONE) {
1127 h1s->h1c->flags |= H1C_F_IN_BUSY;
1128 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1129 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001130 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001131 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1132 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1133 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1134 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1135 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001136}
1137
1138/*
1139 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletf3158e92019-11-15 11:14:23 +01001140 * successfull replies to CONNECT requests or on protocol switching. In this
1141 * last case, this function takes care to switch the request to tunnel mode if
1142 * possible. On the server side, if the request is not finished, the mux is mark
1143 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001144 */
1145static void h1_set_res_tunnel_mode(struct h1s *h1s)
1146{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001147
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001148 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1149 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001150 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1151
Christopher Faulet76014fd2019-12-10 11:47:22 +01001152 if (conn_is_back(h1s->h1c->conn)) {
1153 h1s->flags &= ~H1S_F_PARSING_DONE;
1154 /* On protocol switching, switch the request to tunnel mode if it is in
1155 * DONE state. Otherwise we will wait the end of the request to switch
1156 * it in tunnel mode.
1157 */
1158 if (h1s->req.state < H1_MSG_DONE) {
1159 h1s->h1c->flags |= H1C_F_IN_BUSY;
1160 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1161 }
1162 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1163 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1164 h1s->req.state = H1_MSG_TUNNEL;
1165 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1166 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001167 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001168 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1169 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1170 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1171 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 +01001172 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001173}
1174
1175/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001176 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001177 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001178 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001179 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001180static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001181 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001182{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001183 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001184 int ret = 0;
1185
Christopher Faulet6b81df72019-10-01 22:08:43 +02001186 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1187
Christopher Fauleteec96b52019-09-25 09:10:46 +02001188 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001189 /* Try to match H2 preface before parsing the request headers. */
1190 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001191 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001192 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001193 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001194 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001195 else {
1196 if (h1s->meth == HTTP_METH_CONNECT)
1197 h1m->flags |= H1_MF_METH_CONNECT;
1198 if (h1s->meth == HTTP_METH_HEAD)
1199 h1m->flags |= H1_MF_METH_HEAD;
1200 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001201
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001202 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1203 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001204 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 +02001205 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001206 if (!(h1m->flags & H1_MF_RESP)) {
1207 h1s->flags |= H1S_F_REQ_ERROR;
1208 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1209 }
1210 else {
1211 h1s->flags |= H1S_F_RES_ERROR;
1212 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1213 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001214 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001215 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 +02001216 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1217 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001218 goto end;
1219 }
1220
Christopher Faulet486498c2019-10-11 14:22:00 +02001221 if (h1m->err_pos >= 0) {
1222 /* Maybe we found an error during the parsing while we were
1223 * configured not to block on that, so we have to capture it
1224 * now.
1225 */
1226 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1227 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1228 }
1229
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001231 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001232 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001233 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001234 }
1235 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001236 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001237 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001238 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001239 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001240 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001242
Christopher Faulet129817b2018-09-20 16:14:40 +02001243 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001244 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001245 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001246
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001247 h2c_upgrade:
1248 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001249 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001250 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001251 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1252 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001253}
1254
1255/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001256 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001257 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1258 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001259 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001260static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001261 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001262 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001263{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001264 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001265
Christopher Faulet6b81df72019-10-01 22:08:43 +02001266 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001267 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001268 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001269 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 +01001270 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001271 if (!(h1m->flags & H1_MF_RESP)) {
1272 h1s->flags |= H1S_F_REQ_ERROR;
1273 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1274 }
1275 else {
1276 h1s->flags |= H1S_F_RES_ERROR;
1277 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1278 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001279 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001280 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 +02001281 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001282 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001283 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001284 }
1285
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001286 if (h1m->state == H1_MSG_DATA && h1m->curr_len && h1s->cs)
1287 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1288 else if (h1s->cs)
1289 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1290
Christopher Faulet02a02532019-11-15 09:36:28 +01001291 *ofs += ret;
1292
1293 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001294 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001295 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001296}
1297
1298/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001299 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1300 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1301 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1302 * responsible to update the parser state <h1m>.
1303 */
1304static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1305 struct buffer *buf, size_t *ofs, size_t max)
1306{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001307 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001308
Christopher Faulet6b81df72019-10-01 22:08:43 +02001309 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001310 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001311 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001312 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 +01001313 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001314 if (!(h1m->flags & H1_MF_RESP)) {
1315 h1s->flags |= H1S_F_REQ_ERROR;
1316 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1317 }
1318 else {
1319 h1s->flags |= H1S_F_RES_ERROR;
1320 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1321 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001322 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001323 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 +02001324 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1325 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001326 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001327 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001328
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001329 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001330
1331 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001332 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001333 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001334}
1335
1336/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001337 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
1338 * proceed. This functions is responsible to update the parser state <h1m>. It
1339 * also add the flag CS_FL_EOI on the CS.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001340 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001341static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1342 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001343{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001344 int ret;
1345
1346 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){max});
1347 ret = h1_parse_msg_eom(h1m, htx, max);
1348 if (!ret) {
1349 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1350 if (htx->flags & HTX_FL_PARSING_ERROR) {
1351 if (!(h1m->flags & H1_MF_RESP)) {
1352 h1s->flags |= H1S_F_REQ_ERROR;
1353 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1354 }
1355 else {
1356 h1s->flags |= H1S_F_RES_ERROR;
1357 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1358 }
1359 h1s->cs->flags |= CS_FL_EOI;
1360 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1361 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1362 }
1363 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001364 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001365
Christopher Faulet76014fd2019-12-10 11:47:22 +01001366 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001367 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001368 end:
1369 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){ret});
1370 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001371}
1372
1373/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001374 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001375 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1376 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001377 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001378static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001379{
Christopher Faulet539e0292018-11-19 10:40:09 +01001380 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001381 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001382 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001383 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001384 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001385 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001386
Christopher Faulet539e0292018-11-19 10:40:09 +01001387 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001388 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001389
Christopher Fauletf2824e62018-10-01 12:12:37 +02001390 if (!conn_is_back(h1c->conn)) {
1391 h1m = &h1s->req;
1392 errflag = H1S_F_REQ_ERROR;
1393 }
1394 else {
1395 h1m = &h1s->res;
1396 errflag = H1S_F_RES_ERROR;
1397 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001398
Christopher Faulet74027762019-02-26 14:45:05 +01001399 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001400 if (h1s->flags & errflag)
1401 goto end;
1402
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001403 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001404 size_t used = htx_used_space(htx);
1405
Christopher Faulet129817b2018-09-20 16:14:40 +02001406 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001407 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001408 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001409 if (!ret)
1410 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001411
1412 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1413 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1414
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001415 if ((h1m->flags & H1_MF_RESP) &&
1416 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1417 h1m_init_res(&h1s->res);
1418 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001419 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001420 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001421 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001422 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001423 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001424 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001425 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001426 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001427
1428 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1429 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001430 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001431 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001432 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1433 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1434 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001435 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001436
Christopher Faulet76014fd2019-12-10 11:47:22 +01001437 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1438 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001439 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001440 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001441 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1442 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1443 break;
1444
1445 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1446 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1447 }
1448
Christopher Fauletf3158e92019-11-15 11:14:23 +01001449 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1450 h1_set_req_tunnel_mode(h1s);
1451 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001452 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001453 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1454 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001455 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001456 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001457 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001458 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001459 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001460 if (!ret)
1461 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001462
1463 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1464 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001465 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001466 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001467 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001468 break;
1469 }
1470
Christopher Faulet30db3d72019-05-17 15:35:33 +02001471 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001472 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001473
Christopher Faulet6b81df72019-10-01 22:08:43 +02001474 if (h1s->flags & errflag) {
1475 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001476 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001477 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001478
Christopher Faulet539e0292018-11-19 10:40:09 +01001479 b_del(&h1c->ibuf, total);
1480
1481 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001482 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001483 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001484 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001485 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001486 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001487 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001488 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001489
Christopher Fauletcf56b992018-12-11 16:12:31 +01001490 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1491
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001492 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001493 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001494 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001495 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001496 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001497 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001498 if (h1m->state == H1_MSG_TUNNEL)
1499 h1s->cs->flags |= CS_FL_EOI;
1500 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001501 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001502 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001503
Christopher Faulet6b81df72019-10-01 22:08:43 +02001504 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001505 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001506
1507 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001508 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001509 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001510 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001511 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001512}
1513
Christopher Faulet129817b2018-09-20 16:14:40 +02001514/*
1515 * Process outgoing data. It parses data and transfer them from the channel buffer into
1516 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1517 * 0 if it couldn't proceed.
1518 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001519static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1520{
Christopher Faulet129817b2018-09-20 16:14:40 +02001521 struct h1s *h1s = h1c->h1s;
1522 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001523 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001524 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001525 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001526 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001527 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001528
Christopher Faulet47365272018-10-31 17:40:50 +01001529 if (!count)
1530 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001531
Christopher Faulet94b2c762019-05-24 15:28:57 +02001532 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001533 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1534
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001535 if (htx_is_empty(chn_htx))
1536 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001537
Christopher Faulet51dbc942018-09-13 09:05:15 +02001538 if (!h1_get_buf(h1c, &h1c->obuf)) {
1539 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001540 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 +02001541 goto end;
1542 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001543
Christopher Fauletf2824e62018-10-01 12:12:37 +02001544 if (!conn_is_back(h1c->conn)) {
1545 h1m = &h1s->res;
1546 errflag = H1S_F_RES_ERROR;
1547 }
1548 else {
1549 h1m = &h1s->req;
1550 errflag = H1S_F_REQ_ERROR;
1551 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001552
Christopher Faulet0e54d542019-07-04 21:22:34 +02001553 if (h1s->flags & errflag)
1554 goto end;
1555
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001556 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001557 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001558
Willy Tarreau3815b222018-12-11 19:50:43 +01001559 /* Perform some optimizations to reduce the number of buffer copies.
1560 * First, if the mux's buffer is empty and the htx area contains
1561 * exactly one data block of the same size as the requested count,
1562 * then it's possible to simply swap the caller's buffer with the
1563 * mux's output buffer and adjust offsets and length to match the
1564 * entire DATA HTX block in the middle. In this case we perform a
1565 * true zero-copy operation from end-to-end. This is the situation
1566 * that happens all the time with large files. Second, if this is not
1567 * possible, but the mux's output buffer is empty, we still have an
1568 * opportunity to avoid the copy to the intermediary buffer, by making
1569 * the intermediary buffer's area point to the output buffer's area.
1570 * In this case we want to skip the HTX header to make sure that copies
1571 * remain aligned and that this operation remains possible all the
1572 * time. This goes for headers, data blocks and any data extracted from
1573 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001574 */
1575 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001576 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001577 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001578 htx_get_blk_value(chn_htx, blk).len == count) {
1579 void *old_area = h1c->obuf.area;
1580
Christopher Faulet6b81df72019-10-01 22:08:43 +02001581 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 +01001582 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001583 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001584 h1c->obuf.data = count;
1585
1586 buf->area = old_area;
1587 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001588
Christopher Faulet6b81df72019-10-01 22:08:43 +02001589 chn_htx = (struct htx *)buf->area;
1590 htx_reset(chn_htx);
1591
Christopher Fauletadb22202018-12-12 10:32:09 +01001592 /* The message is chunked. We need to emit the chunk
1593 * size. We have at least the size of the struct htx to
1594 * write the chunk envelope. It should be enough.
1595 */
1596 if (h1m->flags & H1_MF_CHNK) {
1597 h1_emit_chunk_size(&h1c->obuf, count);
1598 h1_emit_chunk_crlf(&h1c->obuf);
1599 }
1600
Willy Tarreau3815b222018-12-11 19:50:43 +01001601 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001602 if (h1m->state == H1_MSG_DATA)
1603 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001604 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001605 else
1606 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001607 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001608 goto out;
1609 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001610 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001611 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001612 else
1613 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001614
Christopher Fauletc2518a52019-06-25 21:41:02 +02001615 tmp.data = 0;
1616 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001617 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001618 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001619 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001620 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001621 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001622 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001623
Christopher Fauletb2e84162018-12-06 11:39:49 +01001624 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001625 if (type != HTX_BLK_DATA && vlen > count)
1626 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001627
Christopher Faulet94b2c762019-05-24 15:28:57 +02001628 if (type == HTX_BLK_UNUSED)
1629 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001630
Christopher Faulet94b2c762019-05-24 15:28:57 +02001631 switch (h1m->state) {
1632 case H1_MSG_RQBEFORE:
1633 if (type != HTX_BLK_REQ_SL)
1634 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001635 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 +02001636 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001637 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001638 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001639 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001640 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001641 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001642 if (sl->flags & HTX_SL_F_BODYLESS)
1643 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001645 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001646
Christopher Faulet94b2c762019-05-24 15:28:57 +02001647 case H1_MSG_RPBEFORE:
1648 if (type != HTX_BLK_RES_SL)
1649 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001650 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 +02001651 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001652 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001653 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001654 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001655 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001656 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001657 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001658 if (sl->info.res.status < 200 &&
1659 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001660 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001661 h1m->state = H1_MSG_HDR_FIRST;
1662 break;
1663
Christopher Faulet94b2c762019-05-24 15:28:57 +02001664 case H1_MSG_HDR_FIRST:
1665 case H1_MSG_HDR_NAME:
1666 case H1_MSG_HDR_L2_LWS:
1667 if (type == HTX_BLK_EOH)
1668 goto last_lf;
1669 if (type != HTX_BLK_HDR)
1670 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001671
Christopher Faulet9768c262018-10-22 09:34:31 +02001672 h1m->state = H1_MSG_HDR_NAME;
1673 n = htx_get_blk_name(chn_htx, blk);
1674 v = htx_get_blk_value(chn_htx, blk);
1675
Christopher Faulet86d144c2019-08-14 16:32:25 +02001676 /* Skip all pseudo-headers */
1677 if (*(n.ptr) == ':')
1678 goto skip_hdr;
1679
Christopher Faulet9768c262018-10-22 09:34:31 +02001680 if (isteqi(n, ist("transfer-encoding")))
1681 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001682 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001683 /* Only skip C-L header with invalid value. */
1684 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001685 goto skip_hdr;
1686 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001688 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001689 if (!v.len)
1690 goto skip_hdr;
1691 }
1692
Christopher Faulet67d58092019-10-02 10:51:38 +02001693 /* Skip header if same name is used to add the server name */
1694 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1695 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1696 goto skip_hdr;
1697
Christopher Faulet98fbe952019-07-22 16:18:24 +02001698 /* Try to adjust the case of the header name */
1699 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1700 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001701 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001702 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001703 skip_hdr:
1704 h1m->state = H1_MSG_HDR_L2_LWS;
1705 break;
1706
Christopher Faulet94b2c762019-05-24 15:28:57 +02001707 case H1_MSG_LAST_LF:
1708 if (type != HTX_BLK_EOH)
1709 goto error;
1710 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001711 h1m->state = H1_MSG_LAST_LF;
1712 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001713 /* If the reply comes from haproxy while the request is
1714 * not finished, we force the connection close. */
1715 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1716 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1717 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1718 }
1719
1720 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001721 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001722 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001723 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001724 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001725 /* Try to adjust the case of the header name */
1726 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1727 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001728 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001729 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001730 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001731 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001732 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001733
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001734 if ((h1s->meth != HTTP_METH_CONNECT &&
1735 (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 +01001736 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1737 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1738 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1739 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1740 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001741 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001742 n = ist("transfer-encoding");
1743 v = ist("chunked");
1744 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1745 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001746 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001747 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001748 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001749 h1m->flags |= H1_MF_CHNK;
1750 }
1751
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001752 /* Now add the server name to a header (if requested) */
1753 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1754 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1755 struct server *srv = objt_server(h1c->conn->target);
1756
1757 if (srv) {
1758 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1759 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001760
1761 /* Try to adjust the case of the header name */
1762 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1763 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001764 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001765 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001766 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001767 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001768 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1769 }
1770
Christopher Fauletc2518a52019-06-25 21:41:02 +02001771 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001772 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001773
Christopher Faulet6b81df72019-10-01 22:08:43 +02001774 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1775 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1776
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001777 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1778 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1779 h1_set_req_tunnel_mode(h1s);
1780 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001781 else if ((h1m->flags & H1_MF_RESP) &&
1782 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001783 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001784 * to the client. Switch the response to tunnel mode.
1785 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001786 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001787 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 +01001788 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001789 else if ((h1m->flags & H1_MF_RESP) &&
1790 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1791 h1m_init_res(&h1s->res);
1792 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001793 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001794 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001795 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001796 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001797 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001798 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1799 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001800 else
1801 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001802 break;
1803
Christopher Faulet94b2c762019-05-24 15:28:57 +02001804 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001805 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001806 if (type == HTX_BLK_EOM) {
1807 /* Chunked message without explicit trailers */
1808 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001809 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001810 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001811 }
1812 goto done;
1813 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001814 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001815 /* If the message is not chunked, never
1816 * add the last chunk. */
1817 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001818 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001819 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 +02001820 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001821 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001822 else if (type != HTX_BLK_DATA)
1823 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001824
1825 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 +02001826
1827
1828 if (vlen > count) {
1829 /* Get the maximum amount of data we can xferred */
1830 vlen = count;
1831 }
1832
1833 chklen = 0;
1834 if (h1m->flags & H1_MF_CHNK) {
1835 chklen = b_room(&tmp);
1836 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1837 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1838 (chklen < 1048576) ? 5 : 8);
1839 chklen += 4; /* 2 x CRLF */
1840 }
1841
1842 if (vlen + chklen > b_room(&tmp)) {
1843 /* too large for the buffer */
1844 if (chklen >= b_room(&tmp))
1845 goto full;
1846 vlen = b_room(&tmp) - chklen;
1847 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001848 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001849 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001850 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001851 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001852
1853 if (h1m->state == H1_MSG_DATA)
1854 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001855 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001856 else
1857 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001858 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001859 break;
1860
Christopher Faulet94b2c762019-05-24 15:28:57 +02001861 case H1_MSG_TRAILERS:
1862 if (type == HTX_BLK_EOM)
1863 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001864 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001865 goto error;
1866 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001867 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001868 /* If the message is not chunked, ignore
1869 * trailers. It may happen with H2 messages. */
1870 if (!(h1m->flags & H1_MF_CHNK))
1871 break;
1872
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001873 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001874 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001875 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001876 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1877 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001878 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001879 else { // HTX_BLK_TLR
1880 n = htx_get_blk_name(chn_htx, blk);
1881 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001882
1883 /* Try to adjust the case of the header name */
1884 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1885 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001886 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001887 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001888 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001889 break;
1890
Christopher Faulet94b2c762019-05-24 15:28:57 +02001891 case H1_MSG_DONE:
1892 if (type != HTX_BLK_EOM)
1893 goto error;
1894 done:
1895 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001896 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1897 h1_set_req_tunnel_mode(h1s);
1898 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1899 }
1900 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001901 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1902 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001903 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 +02001904 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001905
1906 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1907 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001908 break;
1909
Christopher Faulet9768c262018-10-22 09:34:31 +02001910 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001911 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001912 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001913 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001914 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001915 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001916 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001917 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1918 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001919 break;
1920 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001921
1922 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001923 total += vlen;
1924 count -= vlen;
1925 if (sz == vlen)
1926 blk = htx_remove_blk(chn_htx, blk);
1927 else {
1928 htx_cut_data_blk(chn_htx, blk, vlen);
1929 break;
1930 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001931 }
1932
Christopher Faulet9768c262018-10-22 09:34:31 +02001933 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001934 /* when the output buffer is empty, tmp shares the same area so that we
1935 * only have to update pointers and lengths.
1936 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001937 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1938 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001939 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001940 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001941
Willy Tarreau3815b222018-12-11 19:50:43 +01001942 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001943 out:
1944 if (!buf_room_for_htx_data(&h1c->obuf)) {
1945 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001946 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001947 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001948 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001949 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001950 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001951
1952 full:
1953 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1954 h1c->flags |= H1C_F_OUT_FULL;
1955 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001956}
1957
Christopher Faulet51dbc942018-09-13 09:05:15 +02001958/*********************************************************/
1959/* functions below are I/O callbacks from the connection */
1960/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001961static void h1_wake_stream_for_recv(struct h1s *h1s)
1962{
1963 if (h1s && h1s->recv_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001964 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001965 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001966 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001967 h1s->recv_wait = NULL;
1968 }
1969}
1970static void h1_wake_stream_for_send(struct h1s *h1s)
1971{
1972 if (h1s && h1s->send_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001973 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001974 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001975 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001976 h1s->send_wait = NULL;
1977 }
1978}
1979
Christopher Faulet51dbc942018-09-13 09:05:15 +02001980/*
1981 * Attempt to read data, and subscribe if none available
1982 */
1983static int h1_recv(struct h1c *h1c)
1984{
1985 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001986 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001987 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001988 int rcvd = 0;
1989
Christopher Faulet6b81df72019-10-01 22:08:43 +02001990 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
1991
1992 if (h1c->wait_event.events & SUB_RETRY_RECV) {
1993 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01001994 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001995 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001996
Olivier Houchard75159a92018-12-03 18:46:09 +01001997 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001998 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01001999 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002000 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01002001 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002002
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002003 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2004 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002005 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002006 goto end;
2007 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002008
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002009 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002010 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002011 h1_wake_stream_for_recv(h1s);
2012 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002013 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002014 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002015 }
2016
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002017 /*
2018 * If we only have a small amount of data, realign it,
2019 * it's probably cheaper than doing 2 recv() calls.
2020 */
2021 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2022 b_slow_realign(&h1c->ibuf, trash.area, 0);
2023
Willy Tarreau45f2b892018-12-05 07:59:27 +01002024 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002025 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002026 if (h1c->flags & H1C_F_IN_FULL) {
2027 h1c->flags &= ~H1C_F_IN_FULL;
2028 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2029 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002030
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002031 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002032 if (!b_data(&h1c->ibuf)) {
2033 /* try to pre-align the buffer like the rxbufs will be
2034 * to optimize memory copies.
2035 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002036 h1c->ibuf.head = sizeof(struct htx);
2037 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002038 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002039 }
Christopher Faulet47365272018-10-31 17:40:50 +01002040 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002041 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn,,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002042 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002043 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002044 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002045 if (h1s->csinfo.t_idle == -1)
2046 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2047 }
Christopher Faulet47365272018-10-31 17:40:50 +01002048 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002049
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002050 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002051 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002052 goto end;
2053 }
2054
Christopher Faulet6b81df72019-10-01 22:08:43 +02002055 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002056 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002057
Christopher Faulet9768c262018-10-22 09:34:31 +02002058 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002059 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2060 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002061
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002062 if (conn_xprt_read0_pending(conn) && h1s) {
2063 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002064 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002065 rcvd = 1;
2066 }
2067
Christopher Faulet51dbc942018-09-13 09:05:15 +02002068 if (!b_data(&h1c->ibuf))
2069 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002070 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002071 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002072 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2073 }
2074
2075 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002076 return rcvd;
2077}
2078
2079
2080/*
2081 * Try to send data if possible
2082 */
2083static int h1_send(struct h1c *h1c)
2084{
2085 struct connection *conn = h1c->conn;
2086 unsigned int flags = 0;
2087 size_t ret;
2088 int sent = 0;
2089
Christopher Faulet6b81df72019-10-01 22:08:43 +02002090 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2091
2092 if (conn->flags & CO_FL_ERROR) {
2093 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002094 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002095 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002096
2097 if (!b_data(&h1c->obuf))
2098 goto end;
2099
2100 if (h1c->flags & H1C_F_OUT_FULL)
2101 flags |= CO_SFL_MSG_MORE;
2102
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002103 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002104 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002105 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn,,, (size_t[]){ret});
2106 if (h1c->flags & H1C_F_OUT_FULL) {
2107 h1c->flags &= ~H1C_F_OUT_FULL;
2108 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2109 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002110 b_del(&h1c->obuf, ret);
2111 sent = 1;
2112 }
2113
Christopher Faulet145aa472018-12-06 10:56:20 +01002114 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002115 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002116 /* error or output closed, nothing to send, clear the buffer to release it */
2117 b_reset(&h1c->obuf);
2118 }
2119
Christopher Faulet51dbc942018-09-13 09:05:15 +02002120 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002121 if (!(h1c->flags & H1C_F_OUT_FULL))
2122 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002123
Christopher Faulet51dbc942018-09-13 09:05:15 +02002124 /* We're done, no more to send */
2125 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002126 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002127 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002128 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2129 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002130 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002131 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002132 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002133 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2134 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002135 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002136 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002137
Christopher Faulet6b81df72019-10-01 22:08:43 +02002138 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002139 return sent;
2140}
2141
Christopher Faulet51dbc942018-09-13 09:05:15 +02002142
2143/* callback called on any event by the connection handler.
2144 * It applies changes and returns zero, or < 0 if it wants immediate
2145 * destruction of the connection.
2146 */
2147static int h1_process(struct h1c * h1c)
2148{
2149 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002150 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002151
Christopher Faulet6b81df72019-10-01 22:08:43 +02002152 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2153
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002154 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002155 return -1;
2156
Christopher Fauletfeb11742018-11-29 15:12:34 +01002157 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002158 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002159 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002160 goto release;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002161 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002162 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002163 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002164 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002165 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002166 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002167 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002168 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002169 }
2170
Christopher Fauletfeb11742018-11-29 15:12:34 +01002171 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2172 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2173
Christopher Faulet6b81df72019-10-01 22:08:43 +02002174 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002175 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002176 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2177 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002178
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002179 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002180 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002181 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002182 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002183 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002184 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002185 h1s->cs->data_cb->wake(h1s->cs);
2186 }
Christopher Faulet47365272018-10-31 17:40:50 +01002187 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002188 if (h1c->task) {
2189 h1c->task->expire = TICK_ETERNITY;
2190 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002191 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 +01002192 ? h1c->shut_timeout
2193 : h1c->timeout));
2194 task_queue(h1c->task);
2195 }
2196 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002197 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002198 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002199
2200 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002201 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002202 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002203 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002204}
2205
2206static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2207{
2208 struct h1c *h1c = ctx;
2209 int ret = 0;
2210
Christopher Faulet6b81df72019-10-01 22:08:43 +02002211 TRACE_POINT(H1_EV_H1C_WAKE, h1c->conn);
2212
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002213 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002214 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002215 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002216 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002217 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002218 h1_process(h1c);
2219 return NULL;
2220}
2221
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002222static void h1_reset(struct connection *conn)
2223{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002224
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002225}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002226
2227static int h1_wake(struct connection *conn)
2228{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002229 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002230 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002231
Christopher Faulet6b81df72019-10-01 22:08:43 +02002232 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2233
Christopher Faulet539e0292018-11-19 10:40:09 +01002234 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002235 ret = h1_process(h1c);
2236 if (ret == 0) {
2237 struct h1s *h1s = h1c->h1s;
2238
Christopher Faulet6b81df72019-10-01 22:08:43 +02002239 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2240 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002241 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002242 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002243 }
2244 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002245}
2246
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002247/* Connection timeout management. The principle is that if there's no receipt
2248 * nor sending for a certain amount of time, the connection is closed.
2249 */
2250static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2251{
2252 struct h1c *h1c = context;
2253 int expired = tick_is_expired(t->expire, now_ms);
2254
Christopher Faulet6b81df72019-10-01 22:08:43 +02002255 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2256
2257 if (!expired && h1c) {
2258 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002259 return t;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002260 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002261
Olivier Houchard3f795f72019-04-17 22:51:06 +02002262 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002263
2264 if (!h1c) {
2265 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002266 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002267 return NULL;
2268 }
2269
2270 h1c->task = NULL;
2271 /* If a stream is still attached to the mux, just set an error and wait
2272 * for the stream's timeout. Otherwise, release the mux. This is only ok
2273 * because same timeouts are used.
2274 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002275 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002276 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002277 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2278 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002279 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002280 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002281
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002282 return NULL;
2283}
2284
Christopher Faulet51dbc942018-09-13 09:05:15 +02002285/*******************************************/
2286/* functions below are used by the streams */
2287/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002288
Christopher Faulet51dbc942018-09-13 09:05:15 +02002289/*
2290 * Attach a new stream to a connection
2291 * (Used for outgoing connections)
2292 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002293static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002294{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002295 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002296 struct conn_stream *cs = NULL;
2297 struct h1s *h1s;
2298
Christopher Faulet6b81df72019-10-01 22:08:43 +02002299 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2300 if (h1c->flags & H1C_F_CS_ERROR) {
2301 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 +02002302 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002303 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002304
2305 cs = cs_new(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002306 if (!cs) {
2307 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 +02002308 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002309 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002310
Olivier Houchardf502aca2018-12-14 19:42:40 +01002311 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002312 if (h1s == NULL) {
2313 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 +02002314 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002315 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002316
Christopher Faulet6b81df72019-10-01 22:08:43 +02002317 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002318 return cs;
2319 end:
2320 cs_free(cs);
2321 return NULL;
2322}
2323
2324/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2325 * this mux, it's easy as we can only store a single conn_stream.
2326 */
2327static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2328{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002329 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002330 struct h1s *h1s = h1c->h1s;
2331
2332 if (h1s)
2333 return h1s->cs;
2334
2335 return NULL;
2336}
2337
Christopher Faulet73c12072019-04-08 11:23:22 +02002338static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002339{
Christopher Faulet73c12072019-04-08 11:23:22 +02002340 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002341
Christopher Faulet6b81df72019-10-01 22:08:43 +02002342 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002343 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002344 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002345}
2346
2347/*
2348 * Detach the stream from the connection and possibly release the connection.
2349 */
2350static void h1_detach(struct conn_stream *cs)
2351{
2352 struct h1s *h1s = cs->ctx;
2353 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002354 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002355 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002356
Christopher Faulet6b81df72019-10-01 22:08:43 +02002357 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2358
Christopher Faulet51dbc942018-09-13 09:05:15 +02002359 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002360 if (!h1s) {
2361 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002362 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002363 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002364
Olivier Houchardf502aca2018-12-14 19:42:40 +01002365 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002366 h1c = h1s->h1c;
2367 h1s->cs = NULL;
2368
Olivier Houchard8a786902018-12-15 16:05:40 +01002369 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2370 h1s_destroy(h1s);
2371
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002372 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002373 /* If there are any excess server data in the input buffer,
2374 * release it and close the connection ASAP (some data may
2375 * remain in the output buffer). This happens if a server sends
2376 * invalid responses. So in such case, we don't want to reuse
2377 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002378 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002379 if (b_data(&h1c->ibuf)) {
2380 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002381 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002382 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002383 goto release;
2384 }
Christopher Faulet03627242019-07-19 11:34:08 +02002385
Christopher Faulet9400a392018-11-23 23:10:39 +01002386 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002387 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002388 h1c->conn->flags |= CO_FL_PRIVATE;
2389
Olivier Houchard44d59142018-12-13 18:46:22 +01002390 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002391 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002392 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2393 h1c->conn->owner = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002394 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) {
Olivier Houchard351411f2018-12-27 17:20:54 +01002395 /* The server doesn't want it, let's kill the connection right away */
2396 h1c->conn->mux->destroy(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002397 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2398 goto end;
2399 }
2400 tasklet_wakeup(h1c->wait_event.tasklet);
2401 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2402 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002403 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002404 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002405 if (h1c->conn->owner == sess) {
2406 int ret = session_check_idle_conn(sess, h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002407 if (ret == -1) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002408 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002409 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2410 goto end;
2411 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002412 else if (ret == 1) {
2413 /* The connection was added to the server list,
2414 * wake the task so we can subscribe to events
2415 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002416 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002417 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2418 goto end;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002419 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002420 TRACE_DEVEL("connection in idle session list", H1_EV_STRM_END, h1c->conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002421 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002422 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002423 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002424 struct server *srv = objt_server(h1c->conn->target);
2425
2426 if (srv) {
2427 if (h1c->conn->flags & CO_FL_PRIVATE)
2428 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002429 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002430 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2431 else
2432 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002433 TRACE_DEVEL("connection in idle server list", H1_EV_STRM_END, h1c->conn);
Christopher Faulet9400a392018-11-23 23:10:39 +01002434 }
2435 }
2436 }
2437
Christopher Fauletf1204b82019-07-19 14:51:06 +02002438 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002439 /* 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 +02002440 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2441 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2442 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002443 !h1c->conn->owner) {
2444 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002445 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002446 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002447 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002448 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002449 if (h1c->task) {
2450 h1c->task->expire = TICK_ETERNITY;
2451 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002452 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 +01002453 ? h1c->shut_timeout
2454 : h1c->timeout));
2455 task_queue(h1c->task);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002456 TRACE_DEVEL("refreshing connection's timeout", H1_EV_STRM_END, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002457 }
2458 }
2459 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002460 end:
2461 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002462}
2463
2464
2465static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2466{
2467 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002468 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002469
2470 if (!h1s)
2471 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002472 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002473
Christopher Faulet6b81df72019-10-01 22:08:43 +02002474 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2475
2476 if (cs->flags & CS_FL_KILL_CONN) {
2477 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2478 goto do_shutr;
2479 }
2480 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2481 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002482 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002483 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002484
Christopher Faulet6b81df72019-10-01 22:08:43 +02002485 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2486 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2487 goto end;
2488 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489
Christopher Faulet7f366362019-04-08 10:51:20 +02002490 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002491 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2492 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002493 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002494 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002495 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002496 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002497 end:
2498 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002499}
2500
2501static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2502{
2503 struct h1s *h1s = cs->ctx;
2504 struct h1c *h1c;
2505
2506 if (!h1s)
2507 return;
2508 h1c = h1s->h1c;
2509
Christopher Faulet6b81df72019-10-01 22:08:43 +02002510 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2511
2512 if (cs->flags & CS_FL_KILL_CONN) {
2513 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002514 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002515 }
2516 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2517 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2518 goto do_shutw;
2519 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002520
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002521 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002522 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2523 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2524 goto end;
2525 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002526
Christopher Faulet7f366362019-04-08 10:51:20 +02002527 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002528 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2529 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002530 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002531 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002532 end:
2533 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002534}
2535
Christopher Faulet666a0c42019-01-08 11:12:04 +01002536static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002537{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002538 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002539
Christopher Faulet6b81df72019-10-01 22:08:43 +02002540 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002541 conn_xprt_shutw(conn);
2542 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002543 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002544 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002545}
2546
2547/* Called from the upper layer, to unsubscribe to events */
2548static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2549{
2550 struct wait_event *sw;
2551 struct h1s *h1s = cs->ctx;
2552
2553 if (!h1s)
2554 return 0;
2555
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002556 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002557 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002558 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002559 BUG_ON(h1s->recv_wait != sw);
2560 sw->events &= ~SUB_RETRY_RECV;
2561 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002562 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002563 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002564 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002565 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002566 BUG_ON(h1s->send_wait != sw);
2567 sw->events &= ~SUB_RETRY_SEND;
2568 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002569 }
2570 return 0;
2571}
2572
2573/* Called from the upper layer, to subscribe to events, such as being able to send */
2574static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2575{
2576 struct wait_event *sw;
2577 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002578 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002579
2580 if (!h1s)
2581 return -1;
2582
Christopher Faulet6b81df72019-10-01 22:08:43 +02002583 if (event_type & SUB_RETRY_RECV) {
2584 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
2585 sw = param;
2586 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2587 sw->events |= SUB_RETRY_RECV;
2588 h1s->recv_wait = sw;
2589 event_type &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002590 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002591 if (event_type & SUB_RETRY_SEND) {
2592 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
2593 sw = param;
2594 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2595 sw->events |= SUB_RETRY_SEND;
2596 h1s->send_wait = sw;
2597 event_type &= ~SUB_RETRY_SEND;
2598 /*
2599 * If the conn_stream attempt to subscribe, and the
2600 * mux isn't subscribed to the connection, then it
2601 * probably means the connection wasn't established
2602 * yet, so we have to subscribe.
2603 */
2604 h1c = h1s->h1c;
2605 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2606 h1c->conn->xprt->subscribe(h1c->conn,
2607 h1c->conn->xprt_ctx,
2608 SUB_RETRY_SEND,
2609 &h1c->wait_event);
2610 }
2611 if (event_type != 0)
2612 return -1;
2613 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002614}
2615
2616/* Called from the upper layer, to receive data */
2617static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2618{
2619 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002620 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002621 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002622 size_t ret = 0;
2623
Christopher Faulet6b81df72019-10-01 22:08:43 +02002624 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002625 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002626 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002627 else
2628 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002629
Christopher Faulete18777b2019-04-16 16:46:36 +02002630 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002631 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002632 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002633 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2634 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002635 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002636 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002637 if (h1s->flags & H1S_F_SPLICED_DATA) {
2638 h1s->flags &= ~H1S_F_SPLICED_DATA;
2639 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2640 }
2641 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002642 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002643 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002644 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002645 return ret;
2646}
2647
2648
2649/* Called from the upper layer, to send data */
2650static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2651{
2652 struct h1s *h1s = cs->ctx;
2653 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002654 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002655
2656 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002657 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002658 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002659
Christopher Faulet6b81df72019-10-01 22:08:43 +02002660 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){count});
2661
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002662 /* If we're not connected yet, or we're waiting for a handshake, stop
2663 * now, as we don't want to remove everything from the channel buffer
2664 * before we're sure we can send it.
2665 */
2666 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002667 (h1c->conn->flags & CO_FL_HANDSHAKE)) {
2668 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002669 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002670 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002671
Christopher Fauletc31872f2019-06-04 22:09:36 +02002672 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002673 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002674
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002675 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2676 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002677 else
2678 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002679 if (!ret)
2680 break;
2681 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002682 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002683 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002684 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002685 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002686
Christopher Faulet6b81df72019-10-01 22:08:43 +02002687 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002688 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002689}
2690
Willy Tarreaue5733232019-05-22 19:24:06 +02002691#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002692/* Send and get, using splicing */
2693static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2694{
2695 struct h1s *h1s = cs->ctx;
2696 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2697 int ret = 0;
2698
Christopher Faulet6b81df72019-10-01 22:08:43 +02002699 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){count});
2700
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002701 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002702 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002703 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2704 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2705 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002706 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002707 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002708 goto end;
2709 }
2710
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002711 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002712 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002713 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002714 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002715 }
2716
2717 h1s->flags &= ~H1S_F_BUF_FLUSH;
2718 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002719 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002720 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2721 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002722 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002723 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002724 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002725 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002726 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002727 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2728 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002729 }
2730
Christopher Faulet1be55f92018-10-02 15:59:23 +02002731 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002732 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002733 h1s->flags |= H1S_F_REOS;
Christopher Fauletf3158e92019-11-15 11:14:23 +01002734 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002735 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002736 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002737
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002738 if (h1m->state != H1_MSG_DATA || !h1m->curr_len)
2739 cs->flags &= ~CS_FL_MAY_SPLICE;
2740
Christopher Faulet6b81df72019-10-01 22:08:43 +02002741 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002742 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002743}
2744
2745static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2746{
2747 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002748 int ret = 0;
2749
Christopher Faulet6b81df72019-10-01 22:08:43 +02002750 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){pipe->data});
2751
Christopher Faulet1be55f92018-10-02 15:59:23 +02002752 if (b_data(&h1s->h1c->obuf))
2753 goto end;
2754
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002755 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002756 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002757 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002758 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2759 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002760 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002761 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002762 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002763
2764 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002765 return ret;
2766}
2767#endif
2768
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002769static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2770{
2771 int ret = 0;
2772 switch (mux_ctl) {
2773 case MUX_STATUS:
2774 if (conn->flags & CO_FL_CONNECTED)
2775 ret |= MUX_STATUS_READY;
2776 return ret;
2777 default:
2778 return -1;
2779 }
2780}
2781
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002782/* for debugging with CLI's "show fd" command */
2783static void h1_show_fd(struct buffer *msg, struct connection *conn)
2784{
2785 struct h1c *h1c = conn->ctx;
2786 struct h1s *h1s = h1c->h1s;
2787
Christopher Fauletf376a312019-01-04 15:16:06 +01002788 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2789 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002790 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2791 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2792 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2793 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2794
2795 if (h1s) {
2796 char *method;
2797
2798 if (h1s->meth < HTTP_METH_OTHER)
2799 method = http_known_methods[h1s->meth].ptr;
2800 else
2801 method = "UNKNOWN";
2802 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2803 " .meth=%s status=%d",
2804 h1s, h1s->flags,
2805 h1m_state_str(h1s->req.state),
2806 h1m_state_str(h1s->res.state), method, h1s->status);
2807 if (h1s->cs)
2808 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2809 h1s->cs->flags, h1s->cs->data);
2810 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002811}
2812
2813
2814/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2815static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2816{
2817 struct h1_hdr_entry *entry;
2818
2819 /* Be sure there is a non-empty <to> */
2820 if (!strlen(to)) {
2821 memprintf(err, "expect <to>");
2822 return -1;
2823 }
2824
2825 /* Be sure only the case differs between <from> and <to> */
2826 if (strcasecmp(from, to)) {
2827 memprintf(err, "<from> and <to> must not differ execpt the case");
2828 return -1;
2829 }
2830
2831 /* Be sure <from> does not already existsin the tree */
2832 if (ebis_lookup(&hdrs_map.map, from)) {
2833 memprintf(err, "duplicate entry '%s'", from);
2834 return -1;
2835 }
2836
2837 /* Create the entry and insert it in the tree */
2838 entry = malloc(sizeof(*entry));
2839 if (!entry) {
2840 memprintf(err, "out of memory");
2841 return -1;
2842 }
2843
2844 entry->node.key = strdup(from);
2845 entry->name.ptr = strdup(to);
2846 entry->name.len = strlen(to);
2847 if (!entry->node.key || !entry->name.ptr) {
2848 free(entry->node.key);
2849 free(entry->name.ptr);
2850 free(entry);
2851 memprintf(err, "out of memory");
2852 return -1;
2853 }
2854 ebis_insert(&hdrs_map.map, &entry->node);
2855 return 0;
2856}
2857
2858static void h1_hdeaders_case_adjust_deinit()
2859{
2860 struct ebpt_node *node, *next;
2861 struct h1_hdr_entry *entry;
2862
2863 node = ebpt_first(&hdrs_map.map);
2864 while (node) {
2865 next = ebpt_next(node);
2866 ebpt_delete(node);
2867 entry = container_of(node, struct h1_hdr_entry, node);
2868 free(entry->node.key);
2869 free(entry->name.ptr);
2870 free(entry);
2871 node = next;
2872 }
2873 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002874}
2875
Christopher Faulet98fbe952019-07-22 16:18:24 +02002876static int cfg_h1_headers_case_adjust_postparser()
2877{
2878 FILE *file = NULL;
2879 char *c, *key_beg, *key_end, *value_beg, *value_end;
2880 char *err;
2881 int rc, line = 0, err_code = 0;
2882
2883 if (!hdrs_map.name)
2884 goto end;
2885
2886 file = fopen(hdrs_map.name, "r");
2887 if (!file) {
2888 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2889 hdrs_map.name);
2890 err_code |= ERR_ALERT | ERR_FATAL;
2891 goto end;
2892 }
2893
2894 /* now parse all lines. The file may contain only two header name per
2895 * line, separated by spaces. All heading and trailing spaces will be
2896 * ignored. Lines starting with a # are ignored.
2897 */
2898 while (fgets(trash.area, trash.size, file) != NULL) {
2899 line++;
2900 c = trash.area;
2901
2902 /* strip leading spaces and tabs */
2903 while (*c == ' ' || *c == '\t')
2904 c++;
2905
2906 /* ignore emptu lines, or lines beginning with a dash */
2907 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2908 continue;
2909
2910 /* look for the end of the key */
2911 key_beg = c;
2912 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2913 c++;
2914 key_end = c;
2915
2916 /* strip middle spaces and tabs */
2917 while (*c == ' ' || *c == '\t')
2918 c++;
2919
2920 /* look for the end of the value, it is the end of the line */
2921 value_beg = c;
2922 while (*c && *c != '\n' && *c != '\r')
2923 c++;
2924 value_end = c;
2925
2926 /* trim possibly trailing spaces and tabs */
2927 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2928 value_end--;
2929
2930 /* set final \0 and check entries */
2931 *key_end = '\0';
2932 *value_end = '\0';
2933
2934 err = NULL;
2935 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2936 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002937 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2938 hdrs_map.name, err, line);
2939 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002940 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002941 goto end;
2942 }
2943 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002944 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2945 hdrs_map.name, err, line);
2946 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002947 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002948 }
2949 }
2950
2951 end:
2952 if (file)
2953 fclose(file);
2954 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2955 return err_code;
2956}
2957
2958
2959/* config parser for global "h1-outgoing-header-case-adjust" */
2960static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2961 struct proxy *defpx, const char *file, int line,
2962 char **err)
2963{
2964 if (too_many_args(2, args, err, NULL))
2965 return -1;
2966 if (!*(args[1]) || !*(args[2])) {
2967 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2968 return -1;
2969 }
2970 return add_hdr_case_adjust(args[1], args[2], err);
2971}
2972
2973/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2974static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2975 struct proxy *defpx, const char *file, int line,
2976 char **err)
2977{
2978 if (too_many_args(1, args, err, NULL))
2979 return -1;
2980 if (!*(args[1])) {
2981 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2982 return -1;
2983 }
2984 free(hdrs_map.name);
2985 hdrs_map.name = strdup(args[1]);
2986 return 0;
2987}
2988
2989
2990/* config keyword parsers */
2991static struct cfg_kw_list cfg_kws = {{ }, {
2992 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2993 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2994 { 0, NULL, NULL },
2995 }
2996};
2997
2998INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2999REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3000
3001
Christopher Faulet51dbc942018-09-13 09:05:15 +02003002/****************************************/
3003/* MUX initialization and instanciation */
3004/****************************************/
3005
3006/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003007static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003008 .init = h1_init,
3009 .wake = h1_wake,
3010 .attach = h1_attach,
3011 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01003012 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003013 .detach = h1_detach,
3014 .destroy = h1_destroy,
3015 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003016 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003017 .rcv_buf = h1_rcv_buf,
3018 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003019#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003020 .rcv_pipe = h1_rcv_pipe,
3021 .snd_pipe = h1_snd_pipe,
3022#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003023 .subscribe = h1_subscribe,
3024 .unsubscribe = h1_unsubscribe,
3025 .shutr = h1_shutr,
3026 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003027 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003028 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003029 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003030 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003031 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003032};
3033
3034
3035/* this mux registers default HTX proto */
3036static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003037{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003038
Willy Tarreau0108d902018-11-25 19:14:37 +01003039INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3040
Christopher Faulet51dbc942018-09-13 09:05:15 +02003041/*
3042 * Local variables:
3043 * c-indent-level: 8
3044 * c-basic-offset: 8
3045 * End:
3046 */