blob: a0adae6ca2163077db4e92be49c82aadc2657dbe [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
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100105 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200106
Olivier Houchardf502aca2018-12-14 19:42:40 +0100107 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200108 struct h1m req;
109 struct h1m res;
110
111 enum http_meth_t meth; /* HTTP resquest method */
112 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200113};
114
Christopher Faulet98fbe952019-07-22 16:18:24 +0200115/* Map of headers used to convert outgoing headers */
116struct h1_hdrs_map {
117 char *name;
118 struct eb_root map;
119};
120
121/* An entry in a headers map */
122struct h1_hdr_entry {
123 struct ist name;
124 struct ebpt_node node;
125};
126
127/* Declare the headers map */
128static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
129
130
Christopher Faulet6b81df72019-10-01 22:08:43 +0200131/* trace source and events */
132static void h1_trace(enum trace_level level, uint64_t mask,
133 const struct trace_source *src,
134 const struct ist where, const struct ist func,
135 const void *a1, const void *a2, const void *a3, const void *a4);
136
137/* The event representation is split like this :
138 * h1c - internal H1 connection
139 * h1s - internal H1 stream
140 * strm - application layer
141 * rx - data receipt
142 * tx - data transmission
143 *
144 */
145static const struct trace_event h1_trace_events[] = {
146#define H1_EV_H1C_NEW (1ULL << 0)
147 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
148#define H1_EV_H1C_RECV (1ULL << 1)
149 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
150#define H1_EV_H1C_SEND (1ULL << 2)
151 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
152#define H1_EV_H1C_BLK (1ULL << 3)
153 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
154#define H1_EV_H1C_WAKE (1ULL << 4)
155 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
156#define H1_EV_H1C_END (1ULL << 5)
157 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
158#define H1_EV_H1C_ERR (1ULL << 6)
159 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
160
161#define H1_EV_RX_DATA (1ULL << 7)
162 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
163#define H1_EV_RX_EOI (1ULL << 8)
164 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
165#define H1_EV_RX_HDRS (1ULL << 9)
166 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
167#define H1_EV_RX_BODY (1ULL << 10)
168 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
169#define H1_EV_RX_TLRS (1ULL << 11)
170 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
171
172#define H1_EV_TX_DATA (1ULL << 12)
173 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
174#define H1_EV_TX_EOI (1ULL << 13)
175 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
176#define H1_EV_TX_HDRS (1ULL << 14)
177 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
178#define H1_EV_TX_BODY (1ULL << 15)
179 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
180#define H1_EV_TX_TLRS (1ULL << 16)
181 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
182
183#define H1_EV_H1S_NEW (1ULL << 17)
184 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
185#define H1_EV_H1S_BLK (1ULL << 18)
186 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
187#define H1_EV_H1S_END (1ULL << 19)
188 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
189#define H1_EV_H1S_ERR (1ULL << 20)
190 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
191
192#define H1_EV_STRM_NEW (1ULL << 21)
193 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
194#define H1_EV_STRM_RECV (1ULL << 22)
195 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
196#define H1_EV_STRM_SEND (1ULL << 23)
197 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
198#define H1_EV_STRM_WAKE (1ULL << 24)
199 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
200#define H1_EV_STRM_SHUT (1ULL << 25)
201 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
202#define H1_EV_STRM_END (1ULL << 26)
203 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
204#define H1_EV_STRM_ERR (1ULL << 27)
205 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
206
207 { }
208};
209
210static const struct name_desc h1_trace_lockon_args[4] = {
211 /* arg1 */ { /* already used by the connection */ },
212 /* arg2 */ { .name="h1s", .desc="H1 stream" },
213 /* arg3 */ { },
214 /* arg4 */ { }
215};
216
217static const struct name_desc h1_trace_decoding[] = {
218#define H1_VERB_CLEAN 1
219 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
220#define H1_VERB_MINIMAL 2
221 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
222#define H1_VERB_SIMPLE 3
223 { .name="simple", .desc="add request/response status line or htx info when available" },
224#define H1_VERB_ADVANCED 4
225 { .name="advanced", .desc="add header fields or frame decoding when available" },
226#define H1_VERB_COMPLETE 5
227 { .name="complete", .desc="add full data dump when available" },
228 { /* end */ }
229};
230
231static struct trace_source trace_h1 = {
232 .name = IST("h1"),
233 .desc = "HTTP/1 multiplexer",
234 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
235 .default_cb = h1_trace,
236 .known_events = h1_trace_events,
237 .lockon_args = h1_trace_lockon_args,
238 .decoding = h1_trace_decoding,
239 .report_events = ~0, // report everything by default
240};
241
242#define TRACE_SOURCE &trace_h1
243INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
244
Christopher Faulet51dbc942018-09-13 09:05:15 +0200245/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100246DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
247DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249static int h1_recv(struct h1c *h1c);
250static int h1_send(struct h1c *h1c);
251static int h1_process(struct h1c *h1c);
252static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100253static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100254static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200255static void h1_wake_stream_for_recv(struct h1s *h1s);
256static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200257
Christopher Faulet6b81df72019-10-01 22:08:43 +0200258/* the H1 traces always expect that arg1, if non-null, is of type connection
259 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
260 * that arg3, if non-null, is a htx for rx/tx headers.
261 */
262static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
263 const struct ist where, const struct ist func,
264 const void *a1, const void *a2, const void *a3, const void *a4)
265{
266 const struct connection *conn = a1;
267 const struct h1c *h1c = conn ? conn->ctx : NULL;
268 const struct h1s *h1s = a2;
269 const struct htx *htx = a3;
270 const size_t *val = a4;
271
272 if (!h1c)
273 h1c = (h1s ? h1s->h1c : NULL);
274
275 if (!h1c || src->verbosity < H1_VERB_CLEAN)
276 return;
277
278 /* Display frontend/backend info by default */
279 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
280
281 /* Display request and response states if h1s is defined */
282 if (h1s)
283 chunk_appendf(&trace_buf, " [%s, %s]",
284 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
285
286 if (src->verbosity == H1_VERB_CLEAN)
287 return;
288
289 /* Display the value to the 4th argument (level > STATE) */
290 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100291 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200292
293 /* Display status-line if possible (verbosity > MINIMAL) */
294 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
295 const struct htx_blk *blk = htx_get_head_blk(htx);
296 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
297 enum htx_blk_type type = htx_get_blk_type(blk);
298
299 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
300 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
301 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
302 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
303 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
304 }
305
306 /* Display h1c info and, if defined, h1s info (pointer + flags) */
307 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
308 if (h1s)
309 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
310
311 if (src->verbosity == H1_VERB_MINIMAL)
312 return;
313
314 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
315 if (src->level > TRACE_LEVEL_USER) {
316 if (src->verbosity == H1_VERB_COMPLETE ||
317 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
318 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
319 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
320 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
321 if (src->verbosity == H1_VERB_COMPLETE ||
322 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
323 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
324 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
325 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
326 }
327
328 /* Display htx info if defined (level > USER) */
329 if (src->level > TRACE_LEVEL_USER && htx) {
330 int full = 0;
331
332 /* Full htx info (level > STATE && verbosity > SIMPLE) */
333 if (src->level > TRACE_LEVEL_STATE) {
334 if (src->verbosity == H1_VERB_COMPLETE)
335 full = 1;
336 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
337 full = 1;
338 }
339
340 chunk_memcat(&trace_buf, "\n\t", 2);
341 htx_dump(&trace_buf, htx, full);
342 }
343}
344
345
Christopher Faulet51dbc942018-09-13 09:05:15 +0200346/*****************************************************/
347/* functions below are for dynamic buffer management */
348/*****************************************************/
349/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100350 * Indicates whether or not we may receive data. The rules are the following :
351 * - if an error or a shutdown for reads was detected on the connection we
352 must not attempt to receive
353 * - if the input buffer failed to be allocated or is full , we must not try
354 * to receive
355 * - if he input processing is busy waiting for the output side, we may
356 * attemp to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200357 * - otherwise must may not attempt to receive
358 */
359static inline int h1_recv_allowed(const struct h1c *h1c)
360{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100361 if (h1c->flags & H1C_F_CS_ERROR) {
362 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 +0200363 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200364 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200365
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100366 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200367 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 +0100368 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200369 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100370
Christopher Fauletcb55f482018-12-10 11:56:47 +0100371 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372 return 1;
373
Christopher Faulet6b81df72019-10-01 22:08:43 +0200374 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 +0200375 return 0;
376}
377
378/*
379 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
380 * flags are used to figure what buffer was requested. It returns 1 if the
381 * allocation succeeds, in which case the connection is woken up, or 0 if it's
382 * impossible to wake up and we prefer to be woken up later.
383 */
384static int h1_buf_available(void *target)
385{
386 struct h1c *h1c = target;
387
388 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200389 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 +0200390 h1c->flags &= ~H1C_F_IN_ALLOC;
391 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200392 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200393 return 1;
394 }
395
396 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200397 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 +0200398 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200399 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200400 if (h1c->h1s)
401 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200402 return 1;
403 }
404
Christopher Faulet51dbc942018-09-13 09:05:15 +0200405 return 0;
406}
407
408/*
409 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
410 */
411static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
412{
413 struct buffer *buf = NULL;
414
415 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
416 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
417 h1c->buf_wait.target = h1c;
418 h1c->buf_wait.wakeup_cb = h1_buf_available;
419 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
420 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
421 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
422 __conn_xprt_stop_recv(h1c->conn);
423 }
424 return buf;
425}
426
427/*
428 * Release a buffer, if any, and try to wake up entities waiting in the buffer
429 * wait queue.
430 */
431static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
432{
433 if (bptr->size) {
434 b_free(bptr);
435 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
436 }
437}
438
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100439/* returns the number of streams in use on a connection to figure if it's idle
440 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
441 * not. This flag is only set when no H1S is attached and when the previous
442 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100443 */
444static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200445{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200447
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100448 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449}
450
Willy Tarreau00f18a32019-01-26 12:19:01 +0100451/* returns the number of streams still available on a connection */
452static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100453{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100454 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100455}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456
Willy Tarreau00f18a32019-01-26 12:19:01 +0100457
Christopher Faulet51dbc942018-09-13 09:05:15 +0200458/*****************************************************************/
459/* functions below are dedicated to the mux setup and management */
460/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200461
462/* returns non-zero if there are input data pending for stream h1s. */
463static inline size_t h1s_data_pending(const struct h1s *h1s)
464{
465 const struct h1m *h1m;
466
467 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
468 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100469 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200470
471 return b_data(&h1s->h1c->ibuf);
472}
473
Christopher Faulet47365272018-10-31 17:40:50 +0100474static struct conn_stream *h1s_new_cs(struct h1s *h1s)
475{
476 struct conn_stream *cs;
477
Christopher Faulet6b81df72019-10-01 22:08:43 +0200478 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100479 cs = cs_new(h1s->h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200480 if (!cs) {
481 TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100482 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200483 }
Christopher Faulet47365272018-10-31 17:40:50 +0100484 h1s->cs = cs;
485 cs->ctx = h1s;
486
487 if (h1s->flags & H1S_F_NOT_FIRST)
488 cs->flags |= CS_FL_NOT_FIRST;
489
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100490 if (global.tune.options & GTUNE_USE_SPLICE)
491 cs->flags |= CS_FL_MAY_SPLICE;
492
Christopher Faulet6b81df72019-10-01 22:08:43 +0200493 if (stream_create_from_cs(cs) < 0) {
494 TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100495 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200496 }
497
498 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100499 return cs;
500
501 err:
502 cs_free(cs);
503 h1s->cs = NULL;
504 return NULL;
505}
506
Olivier Houchardf502aca2018-12-14 19:42:40 +0100507static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200508{
509 struct h1s *h1s;
510
Christopher Faulet6b81df72019-10-01 22:08:43 +0200511 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
512
Christopher Faulet51dbc942018-09-13 09:05:15 +0200513 h1s = pool_alloc(pool_head_h1s);
514 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100515 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200516
517 h1s->h1c = h1c;
518 h1c->h1s = h1s;
519
Olivier Houchardf502aca2018-12-14 19:42:40 +0100520 h1s->sess = sess;
521
Christopher Faulet51dbc942018-09-13 09:05:15 +0200522 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100523 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200524
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100525 h1s->subs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200526
527 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100528 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200529
Christopher Faulet129817b2018-09-20 16:14:40 +0200530 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100531 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200532
533 h1s->status = 0;
534 h1s->meth = HTTP_METH_OTHER;
535
Christopher Faulet47365272018-10-31 17:40:50 +0100536 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
537 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100538 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100539
Christopher Faulet129817b2018-09-20 16:14:40 +0200540 if (!conn_is_back(h1c->conn)) {
541 if (h1c->px->options2 & PR_O2_REQBUG_OK)
542 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200543
544 /* For frontend connections we should always have a session */
545 if (!sess)
546 sess = h1c->conn->owner;
547
Dave Pirotte234740f2019-07-10 13:57:38 +0000548 /* Timers for subsequent sessions on the same HTTP 1.x connection
549 * measure from `now`, not from the connection accept time */
550 if (h1s->flags & H1S_F_NOT_FIRST) {
551 h1s->csinfo.create_date = date;
552 h1s->csinfo.tv_create = now;
553 h1s->csinfo.t_handshake = 0;
554 h1s->csinfo.t_idle = -1;
555 }
556 else {
557 h1s->csinfo.create_date = sess->accept_date;
558 h1s->csinfo.tv_create = sess->tv_accept;
559 h1s->csinfo.t_handshake = sess->t_handshake;
560 h1s->csinfo.t_idle = -1;
561 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200562 }
563 else {
564 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
565 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200566
Christopher Fauletfeb11742018-11-29 15:12:34 +0100567 h1s->csinfo.create_date = date;
568 h1s->csinfo.tv_create = now;
569 h1s->csinfo.t_handshake = 0;
570 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200571 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100572
Christopher Faulete9b70722019-04-08 10:46:02 +0200573 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
574 * create a new one.
575 */
576 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200577 cs->ctx = h1s;
578 h1s->cs = cs;
579 }
Christopher Faulet47365272018-10-31 17:40:50 +0100580 else {
581 cs = h1s_new_cs(h1s);
582 if (!cs)
583 goto fail;
584 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200585 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200586 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100587
588 fail:
589 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200590 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100591 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200592}
593
594static void h1s_destroy(struct h1s *h1s)
595{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596 if (h1s) {
597 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200598
Christopher Faulet6b81df72019-10-01 22:08:43 +0200599 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200600 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200601
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100602 if (h1s->subs)
603 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200604
Christopher Fauletcb55f482018-12-10 11:56:47 +0100605 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200606 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100607 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200608 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
609 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100610
611 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
612 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100613 (h1s->flags & (H1S_F_WANT_KAL|H1S_F_PARSING_DONE)) == (H1S_F_WANT_KAL|H1S_F_PARSING_DONE) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100614 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
615 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
616 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
617 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200618 pool_free(pool_head_h1s, h1s);
619 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200620}
621
Christopher Fauletfeb11742018-11-29 15:12:34 +0100622static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
623{
624 struct h1s *h1s = cs->ctx;
625
626 if (h1s && !conn_is_back(cs->conn))
627 return &h1s->csinfo;
628 return NULL;
629}
630
Christopher Faulet51dbc942018-09-13 09:05:15 +0200631/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200632 * Initialize the mux once it's attached. It is expected that conn->ctx points
633 * to the existing conn_stream (for outgoing connections or for incoming onces
634 * during a mux upgrade) or NULL (for incoming ones during the connexion
635 * establishment). <input> is always used as Input buffer and may contain
636 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
637 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200638 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200639static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
640 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200641{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200642 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100643 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200644 void *conn_ctx = conn->ctx;
645
646 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200647
648 h1c = pool_alloc(pool_head_h1c);
649 if (!h1c)
650 goto fail_h1c;
651 h1c->conn = conn;
652 h1c->px = proxy;
653
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100654 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200655 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200656 h1c->obuf = BUF_NULL;
657 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200658 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200659
Christopher Faulet51dbc942018-09-13 09:05:15 +0200660 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200661 h1c->wait_event.tasklet = tasklet_new();
662 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200663 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200664 h1c->wait_event.tasklet->process = h1_io_cb;
665 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100666 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200667
Christopher Faulete9b70722019-04-08 10:46:02 +0200668 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100669 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
670 if (tick_isset(proxy->timeout.serverfin))
671 h1c->shut_timeout = proxy->timeout.serverfin;
672 } else {
673 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
674 if (tick_isset(proxy->timeout.clientfin))
675 h1c->shut_timeout = proxy->timeout.clientfin;
676 }
677 if (tick_isset(h1c->timeout)) {
678 t = task_new(tid_bit);
679 if (!t)
680 goto fail;
681
682 h1c->task = t;
683 t->process = h1_timeout_task;
684 t->context = h1c;
685 t->expire = tick_add(now_ms, h1c->timeout);
686 }
687
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100688 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200689
Christopher Faulet6b81df72019-10-01 22:08:43 +0200690 /* Always Create a new H1S */
691 if (!h1s_create(h1c, conn_ctx, sess))
692 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100693
694 if (t)
695 task_queue(t);
696
Christopher Faulet51dbc942018-09-13 09:05:15 +0200697 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200698 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200699
700 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200701 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200702 return 0;
703
704 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200705 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200706 if (h1c->wait_event.tasklet)
707 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200708 pool_free(pool_head_h1c, h1c);
709 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200710 conn->ctx = conn_ctx; // restore saved context
711 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200712 return -1;
713}
714
Christopher Faulet73c12072019-04-08 11:23:22 +0200715/* release function. This one should be called to free all resources allocated
716 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200717 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200718static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200719{
Christopher Faulet61840e72019-04-15 09:33:32 +0200720 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200721
Christopher Faulet6b81df72019-10-01 22:08:43 +0200722 TRACE_POINT(H1_EV_H1C_END);
723
Christopher Faulet51dbc942018-09-13 09:05:15 +0200724 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200725 /* The connection must be aattached to this mux to be released */
726 if (h1c->conn && h1c->conn->ctx == h1c)
727 conn = h1c->conn;
728
Christopher Faulet6b81df72019-10-01 22:08:43 +0200729 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
730
Christopher Faulet61840e72019-04-15 09:33:32 +0200731 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200732 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200733 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200734 /* Make sure we're no longer subscribed to anything */
735 if (h1c->wait_event.events)
736 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
737 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200738 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200739 /* connection successfully upgraded to H2, this
740 * mux was already released */
741 return;
742 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200743 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200744 sess_log(conn->owner); /* Log if the upgrade failed */
745 }
746
Olivier Houchard45c44372019-06-11 14:06:23 +0200747
Christopher Faulet51dbc942018-09-13 09:05:15 +0200748 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
749 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
750 LIST_DEL(&h1c->buf_wait.list);
751 LIST_INIT(&h1c->buf_wait.list);
752 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
753 }
754
755 h1_release_buf(h1c, &h1c->ibuf);
756 h1_release_buf(h1c, &h1c->obuf);
757
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100758 if (h1c->task) {
759 h1c->task->context = NULL;
760 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
761 h1c->task = NULL;
762 }
763
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200764 if (h1c->wait_event.tasklet)
765 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200766
Christopher Fauletf2824e62018-10-01 12:12:37 +0200767 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200768 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100769 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200770 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200771 pool_free(pool_head_h1c, h1c);
772 }
773
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200774 if (conn) {
775 conn->mux = NULL;
776 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200777 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200778
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200779 conn_stop_tracking(conn);
780 conn_full_close(conn);
781 if (conn->destroy_cb)
782 conn->destroy_cb(conn);
783 conn_free(conn);
784 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200785}
786
787/******************************************************/
788/* functions below are for the H1 protocol processing */
789/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200790/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
791 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200792 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100793static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200794{
Christopher Faulet570d1612018-11-26 11:13:57 +0100795 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200796
Christopher Faulet570d1612018-11-26 11:13:57 +0100797 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200798 (*(p + 5) > '1' ||
799 (*(p + 5) == '1' && *(p + 7) >= '1')))
800 h1m->flags |= H1_MF_VER_11;
801}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200802
Christopher Faulet9768c262018-10-22 09:34:31 +0200803/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
804 * greater or equal to 1.1
805 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100806static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200807{
Christopher Faulet570d1612018-11-26 11:13:57 +0100808 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200809
Christopher Faulet570d1612018-11-26 11:13:57 +0100810 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200811 (*(p + 5) > '1' ||
812 (*(p + 5) == '1' && *(p + 7) >= '1')))
813 h1m->flags |= H1_MF_VER_11;
814}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200815
Christopher Fauletf2824e62018-10-01 12:12:37 +0200816/* Deduce the connection mode of the client connection, depending on the
817 * configuration and the H1 message flags. This function is called twice, the
818 * first time when the request is parsed and the second time when the response
819 * is parsed.
820 */
821static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
822{
823 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200824
825 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100826 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200827 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100828 h1s->status == 101) {
829 /* Either we've established an explicit tunnel, or we're
830 * switching the protocol. In both cases, we're very unlikely to
831 * understand the next protocols. We have to switch to tunnel
832 * mode, so that we transfer the request and responses then let
833 * this protocol pass unmodified. When we later implement
834 * specific parsers for such protocols, we'll want to check the
835 * Upgrade header which contains information about that protocol
836 * for responses with status 101 (eg: see RFC2817 about TLS).
837 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200838 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200839 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 +0100840 }
841 else if (h1s->flags & H1S_F_WANT_KAL) {
842 /* By default the client is in KAL mode. CLOSE mode mean
843 * it is imposed by the client itself. So only change
844 * KAL mode here. */
845 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
846 /* no length known or explicit close => close */
847 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200848 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 +0100849 }
850 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
851 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
852 /* no explict keep-alive and option httpclose => close */
853 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200854 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 +0100855 }
856 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200857 }
858 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100859 /* Input direction: first pass */
860 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
861 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200862 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200863 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 +0100864 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200865 }
866
867 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200868 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200869 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200870 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);
871 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200872}
873
874/* Deduce the connection mode of the client connection, depending on the
875 * configuration and the H1 message flags. This function is called twice, the
876 * first time when the request is parsed and the second time when the response
877 * is parsed.
878 */
879static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
880{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100881 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100882 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100883 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200884
Christopher Fauletf2824e62018-10-01 12:12:37 +0200885 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100886 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200887 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100888 h1s->status == 101) {
889 /* Either we've established an explicit tunnel, or we're
890 * switching the protocol. In both cases, we're very unlikely to
891 * understand the next protocols. We have to switch to tunnel
892 * mode, so that we transfer the request and responses then let
893 * this protocol pass unmodified. When we later implement
894 * specific parsers for such protocols, we'll want to check the
895 * Upgrade header which contains information about that protocol
896 * for responses with status 101 (eg: see RFC2817 about TLS).
897 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200898 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200899 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 +0100900 }
901 else if (h1s->flags & H1S_F_WANT_KAL) {
902 /* By default the server is in KAL mode. CLOSE mode mean
903 * it is imposed by haproxy itself. So only change KAL
904 * mode here. */
905 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
906 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
907 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
908 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200909 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 +0100910 }
911 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200912 }
Christopher Faulet70033782018-12-05 13:50:11 +0100913 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100914 /* Output direction: first pass */
915 if (h1m->flags & H1_MF_CONN_CLO) {
916 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100917 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200918 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 +0100919 }
920 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
921 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
922 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
923 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
924 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
925 /* no explicit keep-alive option httpclose/server-close => close */
926 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200927 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 +0100928 }
Christopher Faulet70033782018-12-05 13:50:11 +0100929 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200930
931 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200932 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200933 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200934 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);
935 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200936}
937
Christopher Fauletb992af02019-03-28 15:42:24 +0100938static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200939{
940 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200941
942 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
943 * token is found
944 */
945 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200946 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200947
948 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200949 if (!(h1m->flags & H1_MF_VER_11)) {
950 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 +0100951 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200952 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200953 }
954 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200955 if (h1m->flags & H1_MF_VER_11) {
956 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100957 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200958 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200959 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200960}
961
Christopher Fauletb992af02019-03-28 15:42:24 +0100962static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200963{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200964 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
965 * token is found
966 */
967 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200968 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200969
970 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100971 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +0200972 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
973 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 +0100974 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200975 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200976 }
977 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200978 if (h1m->flags & H1_MF_VER_11) {
979 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100980 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200981 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200982 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200983}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200984
Christopher Fauletb992af02019-03-28 15:42:24 +0100985static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200986{
Christopher Fauletb992af02019-03-28 15:42:24 +0100987 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200988 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100989 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200990 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200991}
992
Christopher Fauletb992af02019-03-28 15:42:24 +0100993static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
994{
995 if (!conn_is_back(h1s->h1c->conn))
996 h1_set_cli_conn_mode(h1s, h1m);
997 else
998 h1_set_srv_conn_mode(h1s, h1m);
999
1000 if (!(h1m->flags & H1_MF_RESP))
1001 h1_update_req_conn_value(h1s, h1m, conn_val);
1002 else
1003 h1_update_res_conn_value(h1s, h1m, conn_val);
1004}
Christopher Faulete44769b2018-11-29 23:01:45 +01001005
Christopher Faulet98fbe952019-07-22 16:18:24 +02001006/* Try to adjust the case of the message header name using the global map
1007 * <hdrs_map>.
1008 */
1009static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1010{
1011 struct ebpt_node *node;
1012 struct h1_hdr_entry *entry;
1013
1014 /* No entry in the map, do nothing */
1015 if (eb_is_empty(&hdrs_map.map))
1016 return;
1017
1018 /* No conversion fo the request headers */
1019 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1020 return;
1021
1022 /* No conversion fo the response headers */
1023 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1024 return;
1025
1026 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1027 if (!node)
1028 return;
1029 entry = container_of(node, struct h1_hdr_entry, node);
1030 name->ptr = entry->name.ptr;
1031 name->len = entry->name.len;
1032}
1033
Christopher Faulete44769b2018-11-29 23:01:45 +01001034/* Append the description of what is present in error snapshot <es> into <out>.
1035 * The description must be small enough to always fit in a buffer. The output
1036 * buffer may be the trash so the trash must not be used inside this function.
1037 */
1038static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1039{
1040 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001041 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1042 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1043 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1044 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1045 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1046 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001047}
1048/*
1049 * Capture a bad request or response and archive it in the proxy's structure.
1050 * By default it tries to report the error position as h1m->err_pos. However if
1051 * this one is not set, it will then report h1m->next, which is the last known
1052 * parsing point. The function is able to deal with wrapping buffers. It always
1053 * displays buffers as a contiguous area starting at buf->p. The direction is
1054 * determined thanks to the h1m's flags.
1055 */
1056static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1057 struct h1m *h1m, struct buffer *buf)
1058{
1059 struct session *sess = h1c->conn->owner;
1060 struct proxy *proxy = h1c->px;
1061 struct proxy *other_end = sess->fe;
1062 union error_snapshot_ctx ctx;
1063
Willy Tarreau598d7fc2018-12-18 18:10:38 +01001064 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +01001065 other_end = si_strm(h1s->cs->data)->be;
1066
1067 /* http-specific part now */
1068 ctx.h1.state = h1m->state;
1069 ctx.h1.c_flags = h1c->flags;
1070 ctx.h1.s_flags = h1s->flags;
1071 ctx.h1.m_flags = h1m->flags;
1072 ctx.h1.m_clen = h1m->curr_len;
1073 ctx.h1.m_blen = h1m->body_len;
1074
1075 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1076 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001077 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1078 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001079}
1080
Christopher Fauletadb22202018-12-12 10:32:09 +01001081/* Emit the chunksize followed by a CRLF in front of data of the buffer
1082 * <buf>. It goes backwards and starts with the byte before the buffer's
1083 * head. The caller is responsible for ensuring there is enough room left before
1084 * the buffer's head for the string.
1085 */
1086static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1087{
1088 char *beg, *end;
1089
1090 beg = end = b_head(buf);
1091 *--beg = '\n';
1092 *--beg = '\r';
1093 do {
1094 *--beg = hextab[chksz & 0xF];
1095 } while (chksz >>= 4);
1096 buf->head -= (end - beg);
1097 b_add(buf, end - beg);
1098}
1099
1100/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1101 * ensuring there is enough room left in the buffer for the string. */
1102static void h1_emit_chunk_crlf(struct buffer *buf)
1103{
1104 *(b_peek(buf, b_data(buf))) = '\r';
1105 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1106 b_add(buf, 2);
1107}
1108
Christopher Faulet129817b2018-09-20 16:14:40 +02001109/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001110 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001111 * CONNECT requests. On the client side, if the response is not finished, the
1112 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001113 */
1114static void h1_set_req_tunnel_mode(struct h1s *h1s)
1115{
1116 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1117 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001118 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1119
Christopher Faulet76014fd2019-12-10 11:47:22 +01001120 if (!conn_is_back(h1s->h1c->conn)) {
1121 h1s->flags &= ~H1S_F_PARSING_DONE;
1122 if (h1s->res.state < H1_MSG_DONE) {
1123 h1s->h1c->flags |= H1C_F_IN_BUSY;
1124 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1125 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001126 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001127 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1128 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1129 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1130 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1131 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001132}
1133
1134/*
1135 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletf3158e92019-11-15 11:14:23 +01001136 * successfull replies to CONNECT requests or on protocol switching. In this
1137 * last case, this function takes care to switch the request to tunnel mode if
1138 * possible. On the server side, if the request is not finished, the mux is mark
1139 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001140 */
1141static void h1_set_res_tunnel_mode(struct h1s *h1s)
1142{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001143
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001144 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1145 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001146 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1147
Christopher Faulet76014fd2019-12-10 11:47:22 +01001148 if (conn_is_back(h1s->h1c->conn)) {
1149 h1s->flags &= ~H1S_F_PARSING_DONE;
1150 /* On protocol switching, switch the request to tunnel mode if it is in
1151 * DONE state. Otherwise we will wait the end of the request to switch
1152 * it in tunnel mode.
1153 */
1154 if (h1s->req.state < H1_MSG_DONE) {
1155 h1s->h1c->flags |= H1C_F_IN_BUSY;
1156 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1157 }
1158 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1159 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1160 h1s->req.state = H1_MSG_TUNNEL;
1161 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1162 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001163 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001164 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1165 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1166 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1167 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 +01001168 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001169}
1170
1171/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001172 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001173 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001174 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001175 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001176static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001177 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001178{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001179 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001180 int ret = 0;
1181
Christopher Faulet6b81df72019-10-01 22:08:43 +02001182 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1183
Christopher Fauleteec96b52019-09-25 09:10:46 +02001184 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001185 /* Try to match H2 preface before parsing the request headers. */
1186 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001187 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001188 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001189 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001190 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001191 else {
1192 if (h1s->meth == HTTP_METH_CONNECT)
1193 h1m->flags |= H1_MF_METH_CONNECT;
1194 if (h1s->meth == HTTP_METH_HEAD)
1195 h1m->flags |= H1_MF_METH_HEAD;
1196 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001197
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001198 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1199 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001200 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 +02001201 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001202 if (!(h1m->flags & H1_MF_RESP)) {
1203 h1s->flags |= H1S_F_REQ_ERROR;
1204 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1205 }
1206 else {
1207 h1s->flags |= H1S_F_RES_ERROR;
1208 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1209 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001210 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001211 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 +02001212 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1213 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001214 goto end;
1215 }
1216
Christopher Faulet486498c2019-10-11 14:22:00 +02001217 if (h1m->err_pos >= 0) {
1218 /* Maybe we found an error during the parsing while we were
1219 * configured not to block on that, so we have to capture it
1220 * now.
1221 */
1222 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1223 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1224 }
1225
Christopher Faulet129817b2018-09-20 16:14:40 +02001226 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001227 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001228 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001229 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 }
1231 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001232 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001233 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001234 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001235 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001236 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001238
Christopher Faulet129817b2018-09-20 16:14:40 +02001239 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001240 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001241 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001242
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001243 h2c_upgrade:
1244 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001245 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001246 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001247 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1248 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001249}
1250
1251/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001252 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001253 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1254 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001256static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001257 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001258 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001259{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001260 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001261
Christopher Faulet6b81df72019-10-01 22:08:43 +02001262 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001263 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001264 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001265 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 +01001266 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001267 if (!(h1m->flags & H1_MF_RESP)) {
1268 h1s->flags |= H1S_F_REQ_ERROR;
1269 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1270 }
1271 else {
1272 h1s->flags |= H1S_F_RES_ERROR;
1273 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1274 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001275 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001276 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 +02001277 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001278 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001279 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001280 }
1281
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001282 if (h1m->state == H1_MSG_DATA && h1m->curr_len && h1s->cs)
1283 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1284 else if (h1s->cs)
1285 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1286
Christopher Faulet02a02532019-11-15 09:36:28 +01001287 *ofs += ret;
1288
1289 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001290 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001291 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001292}
1293
1294/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001295 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1296 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1297 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1298 * responsible to update the parser state <h1m>.
1299 */
1300static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1301 struct buffer *buf, size_t *ofs, size_t max)
1302{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001303 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001304
Christopher Faulet6b81df72019-10-01 22:08:43 +02001305 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001306 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001307 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001308 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 +01001309 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001310 if (!(h1m->flags & H1_MF_RESP)) {
1311 h1s->flags |= H1S_F_REQ_ERROR;
1312 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1313 }
1314 else {
1315 h1s->flags |= H1S_F_RES_ERROR;
1316 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1317 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001318 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001319 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 +02001320 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1321 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001322 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001323 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001324
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001325 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001326
1327 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001328 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001329 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001330}
1331
1332/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001333 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
1334 * proceed. This functions is responsible to update the parser state <h1m>. It
1335 * also add the flag CS_FL_EOI on the CS.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001336 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001337static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1338 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001339{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001340 int ret;
1341
1342 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){max});
1343 ret = h1_parse_msg_eom(h1m, htx, max);
1344 if (!ret) {
1345 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1346 if (htx->flags & HTX_FL_PARSING_ERROR) {
1347 if (!(h1m->flags & H1_MF_RESP)) {
1348 h1s->flags |= H1S_F_REQ_ERROR;
1349 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1350 }
1351 else {
1352 h1s->flags |= H1S_F_RES_ERROR;
1353 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1354 }
1355 h1s->cs->flags |= CS_FL_EOI;
1356 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1357 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1358 }
1359 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001360 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001361
Christopher Faulet76014fd2019-12-10 11:47:22 +01001362 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001363 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001364 end:
1365 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){ret});
1366 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001367}
1368
1369/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001370 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001371 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1372 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001373 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001374static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001375{
Christopher Faulet539e0292018-11-19 10:40:09 +01001376 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001377 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001378 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001379 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001380 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001381 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001382
Christopher Faulet539e0292018-11-19 10:40:09 +01001383 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001384 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001385
Christopher Fauletf2824e62018-10-01 12:12:37 +02001386 if (!conn_is_back(h1c->conn)) {
1387 h1m = &h1s->req;
1388 errflag = H1S_F_REQ_ERROR;
1389 }
1390 else {
1391 h1m = &h1s->res;
1392 errflag = H1S_F_RES_ERROR;
1393 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001394
Christopher Faulet74027762019-02-26 14:45:05 +01001395 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001396 if (h1s->flags & errflag)
1397 goto end;
1398
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001399 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001400 size_t used = htx_used_space(htx);
1401
Christopher Faulet129817b2018-09-20 16:14:40 +02001402 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001403 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001404 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001405 if (!ret)
1406 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001407
1408 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1409 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1410
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001411 if ((h1m->flags & H1_MF_RESP) &&
1412 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1413 h1m_init_res(&h1s->res);
1414 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001415 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001416 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001417 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001418 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001419 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001420 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001421 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001422 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001423
1424 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1425 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001426 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001427 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001428 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1429 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1430 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001431 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001432
Christopher Faulet76014fd2019-12-10 11:47:22 +01001433 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1434 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001435 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001436 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001437 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1438 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1439 break;
1440
1441 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1442 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1443 }
1444
Christopher Fauletf3158e92019-11-15 11:14:23 +01001445 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1446 h1_set_req_tunnel_mode(h1s);
1447 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001448 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001449 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1450 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001451 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001452 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001453 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001454 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001455 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001456 if (!ret)
1457 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001458
1459 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1460 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001461 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001462 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001463 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001464 break;
1465 }
1466
Christopher Faulet30db3d72019-05-17 15:35:33 +02001467 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001468 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001469
Christopher Faulet6b81df72019-10-01 22:08:43 +02001470 if (h1s->flags & errflag) {
1471 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001472 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001473 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001474
Christopher Faulet539e0292018-11-19 10:40:09 +01001475 b_del(&h1c->ibuf, total);
1476
1477 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001478 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001479 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001480 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001481 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001482 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 +02001483 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001484 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001485
Christopher Fauletcf56b992018-12-11 16:12:31 +01001486 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1487
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001488 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001489 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001490 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001491 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001492 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001493 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001494 if (h1m->state == H1_MSG_TUNNEL)
1495 h1s->cs->flags |= CS_FL_EOI;
1496 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001497 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001498 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001499
Christopher Faulet6b81df72019-10-01 22:08:43 +02001500 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001501 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001502
1503 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001504 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001505 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001506 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001507 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001508}
1509
Christopher Faulet129817b2018-09-20 16:14:40 +02001510/*
1511 * Process outgoing data. It parses data and transfer them from the channel buffer into
1512 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1513 * 0 if it couldn't proceed.
1514 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001515static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1516{
Christopher Faulet129817b2018-09-20 16:14:40 +02001517 struct h1s *h1s = h1c->h1s;
1518 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001519 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001520 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001521 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001522 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001523 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001524
Christopher Faulet47365272018-10-31 17:40:50 +01001525 if (!count)
1526 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001527
Christopher Faulet94b2c762019-05-24 15:28:57 +02001528 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001529 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1530
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001531 if (htx_is_empty(chn_htx))
1532 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001533
Christopher Faulet51dbc942018-09-13 09:05:15 +02001534 if (!h1_get_buf(h1c, &h1c->obuf)) {
1535 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001536 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 +02001537 goto end;
1538 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001539
Christopher Fauletf2824e62018-10-01 12:12:37 +02001540 if (!conn_is_back(h1c->conn)) {
1541 h1m = &h1s->res;
1542 errflag = H1S_F_RES_ERROR;
1543 }
1544 else {
1545 h1m = &h1s->req;
1546 errflag = H1S_F_REQ_ERROR;
1547 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001548
Christopher Faulet0e54d542019-07-04 21:22:34 +02001549 if (h1s->flags & errflag)
1550 goto end;
1551
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001552 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001553 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001554
Willy Tarreau3815b222018-12-11 19:50:43 +01001555 /* Perform some optimizations to reduce the number of buffer copies.
1556 * First, if the mux's buffer is empty and the htx area contains
1557 * exactly one data block of the same size as the requested count,
1558 * then it's possible to simply swap the caller's buffer with the
1559 * mux's output buffer and adjust offsets and length to match the
1560 * entire DATA HTX block in the middle. In this case we perform a
1561 * true zero-copy operation from end-to-end. This is the situation
1562 * that happens all the time with large files. Second, if this is not
1563 * possible, but the mux's output buffer is empty, we still have an
1564 * opportunity to avoid the copy to the intermediary buffer, by making
1565 * the intermediary buffer's area point to the output buffer's area.
1566 * In this case we want to skip the HTX header to make sure that copies
1567 * remain aligned and that this operation remains possible all the
1568 * time. This goes for headers, data blocks and any data extracted from
1569 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001570 */
1571 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001572 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001573 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001574 htx_get_blk_value(chn_htx, blk).len == count) {
1575 void *old_area = h1c->obuf.area;
1576
Christopher Faulet6b81df72019-10-01 22:08:43 +02001577 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 +01001578 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001579 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001580 h1c->obuf.data = count;
1581
1582 buf->area = old_area;
1583 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001584
Christopher Faulet6b81df72019-10-01 22:08:43 +02001585 chn_htx = (struct htx *)buf->area;
1586 htx_reset(chn_htx);
1587
Christopher Fauletadb22202018-12-12 10:32:09 +01001588 /* The message is chunked. We need to emit the chunk
1589 * size. We have at least the size of the struct htx to
1590 * write the chunk envelope. It should be enough.
1591 */
1592 if (h1m->flags & H1_MF_CHNK) {
1593 h1_emit_chunk_size(&h1c->obuf, count);
1594 h1_emit_chunk_crlf(&h1c->obuf);
1595 }
1596
Willy Tarreau3815b222018-12-11 19:50:43 +01001597 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001598 if (h1m->state == H1_MSG_DATA)
1599 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001600 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001601 else
1602 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001603 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001604 goto out;
1605 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001606 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001607 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001608 else
1609 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001610
Christopher Fauletc2518a52019-06-25 21:41:02 +02001611 tmp.data = 0;
1612 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001613 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001614 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001615 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001616 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001617 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001618 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001619
Christopher Fauletb2e84162018-12-06 11:39:49 +01001620 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001621 if (type != HTX_BLK_DATA && vlen > count)
1622 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001623
Christopher Faulet94b2c762019-05-24 15:28:57 +02001624 if (type == HTX_BLK_UNUSED)
1625 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001626
Christopher Faulet94b2c762019-05-24 15:28:57 +02001627 switch (h1m->state) {
1628 case H1_MSG_RQBEFORE:
1629 if (type != HTX_BLK_REQ_SL)
1630 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001631 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 +02001632 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001633 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001634 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001635 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001636 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001637 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001638 if (sl->flags & HTX_SL_F_BODYLESS)
1639 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001640 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001641 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001642
Christopher Faulet94b2c762019-05-24 15:28:57 +02001643 case H1_MSG_RPBEFORE:
1644 if (type != HTX_BLK_RES_SL)
1645 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001646 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 +02001647 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001648 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001650 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001651 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001652 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001653 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001654 if (sl->info.res.status < 200 &&
1655 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001656 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001657 h1m->state = H1_MSG_HDR_FIRST;
1658 break;
1659
Christopher Faulet94b2c762019-05-24 15:28:57 +02001660 case H1_MSG_HDR_FIRST:
1661 case H1_MSG_HDR_NAME:
1662 case H1_MSG_HDR_L2_LWS:
1663 if (type == HTX_BLK_EOH)
1664 goto last_lf;
1665 if (type != HTX_BLK_HDR)
1666 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001667
Christopher Faulet9768c262018-10-22 09:34:31 +02001668 h1m->state = H1_MSG_HDR_NAME;
1669 n = htx_get_blk_name(chn_htx, blk);
1670 v = htx_get_blk_value(chn_htx, blk);
1671
Christopher Faulet86d144c2019-08-14 16:32:25 +02001672 /* Skip all pseudo-headers */
1673 if (*(n.ptr) == ':')
1674 goto skip_hdr;
1675
Christopher Faulet9768c262018-10-22 09:34:31 +02001676 if (isteqi(n, ist("transfer-encoding")))
1677 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001678 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001679 /* Only skip C-L header with invalid value. */
1680 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001681 goto skip_hdr;
1682 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001683 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001684 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 if (!v.len)
1686 goto skip_hdr;
1687 }
1688
Christopher Faulet67d58092019-10-02 10:51:38 +02001689 /* Skip header if same name is used to add the server name */
1690 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1691 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1692 goto skip_hdr;
1693
Christopher Faulet98fbe952019-07-22 16:18:24 +02001694 /* Try to adjust the case of the header name */
1695 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1696 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001697 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001698 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001699 skip_hdr:
1700 h1m->state = H1_MSG_HDR_L2_LWS;
1701 break;
1702
Christopher Faulet94b2c762019-05-24 15:28:57 +02001703 case H1_MSG_LAST_LF:
1704 if (type != HTX_BLK_EOH)
1705 goto error;
1706 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001707 h1m->state = H1_MSG_LAST_LF;
1708 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001709 /* If the reply comes from haproxy while the request is
1710 * not finished, we force the connection close. */
1711 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1712 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1713 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1714 }
1715
1716 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001717 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001718 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001719 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001720 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001721 /* Try to adjust the case of the header name */
1722 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1723 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001724 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001725 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001726 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001727 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001728 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001729
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001730 if ((h1s->meth != HTTP_METH_CONNECT &&
1731 (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 +01001732 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1733 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1734 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1735 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1736 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001737 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001738 n = ist("transfer-encoding");
1739 v = ist("chunked");
1740 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1741 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001742 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001743 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001744 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001745 h1m->flags |= H1_MF_CHNK;
1746 }
1747
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001748 /* Now add the server name to a header (if requested) */
1749 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1750 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1751 struct server *srv = objt_server(h1c->conn->target);
1752
1753 if (srv) {
1754 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1755 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001756
1757 /* Try to adjust the case of the header name */
1758 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1759 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001760 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001761 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001762 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001763 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001764 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1765 }
1766
Christopher Fauletc2518a52019-06-25 21:41:02 +02001767 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001768 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001769
Christopher Faulet6b81df72019-10-01 22:08:43 +02001770 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1771 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1772
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001773 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1774 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1775 h1_set_req_tunnel_mode(h1s);
1776 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001777 else if ((h1m->flags & H1_MF_RESP) &&
1778 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001779 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001780 * to the client. Switch the response to tunnel mode.
1781 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001782 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001783 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 +01001784 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001785 else if ((h1m->flags & H1_MF_RESP) &&
1786 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1787 h1m_init_res(&h1s->res);
1788 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001789 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001790 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001791 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001792 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001793 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001794 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1795 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001796 else
1797 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001798 break;
1799
Christopher Faulet94b2c762019-05-24 15:28:57 +02001800 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001801 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001802 if (type == HTX_BLK_EOM) {
1803 /* Chunked message without explicit trailers */
1804 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001805 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001806 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001807 }
1808 goto done;
1809 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001810 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001811 /* If the message is not chunked, never
1812 * add the last chunk. */
1813 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001814 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001815 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 +02001816 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001817 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001818 else if (type != HTX_BLK_DATA)
1819 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001820
1821 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 +02001822
1823
1824 if (vlen > count) {
1825 /* Get the maximum amount of data we can xferred */
1826 vlen = count;
1827 }
1828
1829 chklen = 0;
1830 if (h1m->flags & H1_MF_CHNK) {
1831 chklen = b_room(&tmp);
1832 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1833 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1834 (chklen < 1048576) ? 5 : 8);
1835 chklen += 4; /* 2 x CRLF */
1836 }
1837
1838 if (vlen + chklen > b_room(&tmp)) {
1839 /* too large for the buffer */
1840 if (chklen >= b_room(&tmp))
1841 goto full;
1842 vlen = b_room(&tmp) - chklen;
1843 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001844 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001845 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001846 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001847 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001848
1849 if (h1m->state == H1_MSG_DATA)
1850 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001851 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001852 else
1853 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001854 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001855 break;
1856
Christopher Faulet94b2c762019-05-24 15:28:57 +02001857 case H1_MSG_TRAILERS:
1858 if (type == HTX_BLK_EOM)
1859 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001860 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001861 goto error;
1862 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001863 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001864 /* If the message is not chunked, ignore
1865 * trailers. It may happen with H2 messages. */
1866 if (!(h1m->flags & H1_MF_CHNK))
1867 break;
1868
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001869 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001870 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001871 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001872 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1873 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001874 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001875 else { // HTX_BLK_TLR
1876 n = htx_get_blk_name(chn_htx, blk);
1877 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001878
1879 /* Try to adjust the case of the header name */
1880 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1881 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001882 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001883 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001884 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001885 break;
1886
Christopher Faulet94b2c762019-05-24 15:28:57 +02001887 case H1_MSG_DONE:
1888 if (type != HTX_BLK_EOM)
1889 goto error;
1890 done:
1891 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001892 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1893 h1_set_req_tunnel_mode(h1s);
1894 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1895 }
1896 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001897 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1898 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001899 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 +02001900 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001901
1902 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1903 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001904 break;
1905
Christopher Faulet9768c262018-10-22 09:34:31 +02001906 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001907 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001908 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001909 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001910 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001911 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001912 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001913 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1914 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001915 break;
1916 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001917
1918 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001919 total += vlen;
1920 count -= vlen;
1921 if (sz == vlen)
1922 blk = htx_remove_blk(chn_htx, blk);
1923 else {
1924 htx_cut_data_blk(chn_htx, blk, vlen);
1925 break;
1926 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001927 }
1928
Christopher Faulet9768c262018-10-22 09:34:31 +02001929 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001930 /* when the output buffer is empty, tmp shares the same area so that we
1931 * only have to update pointers and lengths.
1932 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001933 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1934 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001935 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001936 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001937
Willy Tarreau3815b222018-12-11 19:50:43 +01001938 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001939 out:
1940 if (!buf_room_for_htx_data(&h1c->obuf)) {
1941 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001942 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001943 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001944 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001945 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001946 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001947
1948 full:
1949 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1950 h1c->flags |= H1C_F_OUT_FULL;
1951 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001952}
1953
Christopher Faulet51dbc942018-09-13 09:05:15 +02001954/*********************************************************/
1955/* functions below are I/O callbacks from the connection */
1956/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001957static void h1_wake_stream_for_recv(struct h1s *h1s)
1958{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001959 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001960 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001961 tasklet_wakeup(h1s->subs->tasklet);
1962 h1s->subs->events &= ~SUB_RETRY_RECV;
1963 if (!h1s->subs->events)
1964 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001965 }
1966}
1967static void h1_wake_stream_for_send(struct h1s *h1s)
1968{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001969 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001970 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001971 tasklet_wakeup(h1s->subs->tasklet);
1972 h1s->subs->events &= ~SUB_RETRY_SEND;
1973 if (!h1s->subs->events)
1974 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001975 }
1976}
1977
Christopher Faulet51dbc942018-09-13 09:05:15 +02001978/*
1979 * Attempt to read data, and subscribe if none available
1980 */
1981static int h1_recv(struct h1c *h1c)
1982{
1983 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001984 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001985 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986 int rcvd = 0;
1987
Christopher Faulet6b81df72019-10-01 22:08:43 +02001988 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
1989
1990 if (h1c->wait_event.events & SUB_RETRY_RECV) {
1991 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01001992 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001993 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001994
Olivier Houchard75159a92018-12-03 18:46:09 +01001995 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001996 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01001997 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001998 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001999 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002001 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2002 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002003 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002004 goto end;
2005 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002006
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002007 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002008 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002009 h1_wake_stream_for_recv(h1s);
2010 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002011 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002012 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002013 }
2014
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002015 /*
2016 * If we only have a small amount of data, realign it,
2017 * it's probably cheaper than doing 2 recv() calls.
2018 */
2019 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2020 b_slow_realign(&h1c->ibuf, trash.area, 0);
2021
Willy Tarreau45f2b892018-12-05 07:59:27 +01002022 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002023 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002024 if (h1c->flags & H1C_F_IN_FULL) {
2025 h1c->flags &= ~H1C_F_IN_FULL;
2026 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2027 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002028
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002029 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002030 if (!b_data(&h1c->ibuf)) {
2031 /* try to pre-align the buffer like the rxbufs will be
2032 * to optimize memory copies.
2033 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002034 h1c->ibuf.head = sizeof(struct htx);
2035 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002036 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002037 }
Christopher Faulet47365272018-10-31 17:40:50 +01002038 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002039 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn,,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002040 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002041 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002042 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002043 if (h1s->csinfo.t_idle == -1)
2044 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2045 }
Christopher Faulet47365272018-10-31 17:40:50 +01002046 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002047
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002048 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002049 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002050 goto end;
2051 }
2052
Christopher Faulet6b81df72019-10-01 22:08:43 +02002053 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002054 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002055
Christopher Faulet9768c262018-10-22 09:34:31 +02002056 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002057 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2058 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002059
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002060 if (conn_xprt_read0_pending(conn) && h1s) {
2061 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002062 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002063 rcvd = 1;
2064 }
2065
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066 if (!b_data(&h1c->ibuf))
2067 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002068 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002069 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002070 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2071 }
2072
2073 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002074 return rcvd;
2075}
2076
2077
2078/*
2079 * Try to send data if possible
2080 */
2081static int h1_send(struct h1c *h1c)
2082{
2083 struct connection *conn = h1c->conn;
2084 unsigned int flags = 0;
2085 size_t ret;
2086 int sent = 0;
2087
Christopher Faulet6b81df72019-10-01 22:08:43 +02002088 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2089
2090 if (conn->flags & CO_FL_ERROR) {
2091 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002092 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002093 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002094
2095 if (!b_data(&h1c->obuf))
2096 goto end;
2097
2098 if (h1c->flags & H1C_F_OUT_FULL)
2099 flags |= CO_SFL_MSG_MORE;
2100
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002101 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002102 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002103 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn,,, (size_t[]){ret});
2104 if (h1c->flags & H1C_F_OUT_FULL) {
2105 h1c->flags &= ~H1C_F_OUT_FULL;
2106 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2107 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002108 b_del(&h1c->obuf, ret);
2109 sent = 1;
2110 }
2111
Christopher Faulet145aa472018-12-06 10:56:20 +01002112 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002113 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002114 /* error or output closed, nothing to send, clear the buffer to release it */
2115 b_reset(&h1c->obuf);
2116 }
2117
Christopher Faulet51dbc942018-09-13 09:05:15 +02002118 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002119 if (!(h1c->flags & H1C_F_OUT_FULL))
2120 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002121
Christopher Faulet51dbc942018-09-13 09:05:15 +02002122 /* We're done, no more to send */
2123 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002124 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002125 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002126 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2127 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002128 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002129 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002130 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002131 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2132 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002133 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002134 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002135
Christopher Faulet6b81df72019-10-01 22:08:43 +02002136 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002137 return sent;
2138}
2139
Christopher Faulet51dbc942018-09-13 09:05:15 +02002140
2141/* callback called on any event by the connection handler.
2142 * It applies changes and returns zero, or < 0 if it wants immediate
2143 * destruction of the connection.
2144 */
2145static int h1_process(struct h1c * h1c)
2146{
2147 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002148 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002149
Christopher Faulet6b81df72019-10-01 22:08:43 +02002150 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2151
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002152 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002153 return -1;
2154
Christopher Fauletfeb11742018-11-29 15:12:34 +01002155 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002156 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002157 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002158 goto release;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002159 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002160 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002161 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002162 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002163 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002164 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002165 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002166 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002167 }
2168
Christopher Fauletfeb11742018-11-29 15:12:34 +01002169 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2170 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2171
Christopher Faulet6b81df72019-10-01 22:08:43 +02002172 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002173 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002174 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2175 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002176
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002177 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002178 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002179 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002180 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002181 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002182 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002183 h1s->cs->data_cb->wake(h1s->cs);
2184 }
Christopher Faulet47365272018-10-31 17:40:50 +01002185 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002186 if (h1c->task) {
2187 h1c->task->expire = TICK_ETERNITY;
2188 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002189 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 +01002190 ? h1c->shut_timeout
2191 : h1c->timeout));
2192 task_queue(h1c->task);
2193 }
2194 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002195 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002196 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002197
2198 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002199 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002200 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002201 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002202}
2203
2204static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2205{
2206 struct h1c *h1c = ctx;
2207 int ret = 0;
2208
Christopher Faulet6b81df72019-10-01 22:08:43 +02002209 TRACE_POINT(H1_EV_H1C_WAKE, h1c->conn);
2210
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002211 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002212 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002213 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002214 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002215 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002216 h1_process(h1c);
2217 return NULL;
2218}
2219
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002220static void h1_reset(struct connection *conn)
2221{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002222
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002223}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002224
2225static int h1_wake(struct connection *conn)
2226{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002227 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002228 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002229
Christopher Faulet6b81df72019-10-01 22:08:43 +02002230 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2231
Christopher Faulet539e0292018-11-19 10:40:09 +01002232 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002233 ret = h1_process(h1c);
2234 if (ret == 0) {
2235 struct h1s *h1s = h1c->h1s;
2236
Christopher Faulet6b81df72019-10-01 22:08:43 +02002237 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2238 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002239 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002240 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002241 }
2242 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002243}
2244
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002245/* Connection timeout management. The principle is that if there's no receipt
2246 * nor sending for a certain amount of time, the connection is closed.
2247 */
2248static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2249{
2250 struct h1c *h1c = context;
2251 int expired = tick_is_expired(t->expire, now_ms);
2252
Christopher Faulet6b81df72019-10-01 22:08:43 +02002253 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2254
2255 if (!expired && h1c) {
2256 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002257 return t;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002258 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002259
Olivier Houchard3f795f72019-04-17 22:51:06 +02002260 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002261
2262 if (!h1c) {
2263 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002264 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002265 return NULL;
2266 }
2267
2268 h1c->task = NULL;
2269 /* If a stream is still attached to the mux, just set an error and wait
2270 * for the stream's timeout. Otherwise, release the mux. This is only ok
2271 * because same timeouts are used.
2272 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002273 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002274 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002275 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2276 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002277 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002278 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002279
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002280 return NULL;
2281}
2282
Christopher Faulet51dbc942018-09-13 09:05:15 +02002283/*******************************************/
2284/* functions below are used by the streams */
2285/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002286
Christopher Faulet51dbc942018-09-13 09:05:15 +02002287/*
2288 * Attach a new stream to a connection
2289 * (Used for outgoing connections)
2290 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002291static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002292{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002293 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002294 struct conn_stream *cs = NULL;
2295 struct h1s *h1s;
2296
Christopher Faulet6b81df72019-10-01 22:08:43 +02002297 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2298 if (h1c->flags & H1C_F_CS_ERROR) {
2299 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 +02002300 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002301 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002302
2303 cs = cs_new(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002304 if (!cs) {
2305 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 +02002306 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002307 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002308
Olivier Houchardf502aca2018-12-14 19:42:40 +01002309 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002310 if (h1s == NULL) {
2311 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 +02002312 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002313 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002314
Christopher Faulet6b81df72019-10-01 22:08:43 +02002315 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002316 return cs;
2317 end:
2318 cs_free(cs);
2319 return NULL;
2320}
2321
2322/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2323 * this mux, it's easy as we can only store a single conn_stream.
2324 */
2325static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2326{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002327 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002328 struct h1s *h1s = h1c->h1s;
2329
2330 if (h1s)
2331 return h1s->cs;
2332
2333 return NULL;
2334}
2335
Christopher Faulet73c12072019-04-08 11:23:22 +02002336static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002337{
Christopher Faulet73c12072019-04-08 11:23:22 +02002338 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002339
Christopher Faulet6b81df72019-10-01 22:08:43 +02002340 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002341 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002342 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002343}
2344
2345/*
2346 * Detach the stream from the connection and possibly release the connection.
2347 */
2348static void h1_detach(struct conn_stream *cs)
2349{
2350 struct h1s *h1s = cs->ctx;
2351 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002352 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002353 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354
Christopher Faulet6b81df72019-10-01 22:08:43 +02002355 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2356
Christopher Faulet51dbc942018-09-13 09:05:15 +02002357 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002358 if (!h1s) {
2359 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002360 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002361 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002362
Olivier Houchardf502aca2018-12-14 19:42:40 +01002363 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002364 h1c = h1s->h1c;
2365 h1s->cs = NULL;
2366
Olivier Houchard8a786902018-12-15 16:05:40 +01002367 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2368 h1s_destroy(h1s);
2369
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002370 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002371 /* If there are any excess server data in the input buffer,
2372 * release it and close the connection ASAP (some data may
2373 * remain in the output buffer). This happens if a server sends
2374 * invalid responses. So in such case, we don't want to reuse
2375 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002376 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002377 if (b_data(&h1c->ibuf)) {
2378 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002379 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002380 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002381 goto release;
2382 }
Christopher Faulet03627242019-07-19 11:34:08 +02002383
Christopher Faulet9400a392018-11-23 23:10:39 +01002384 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002385 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002386 h1c->conn->flags |= CO_FL_PRIVATE;
2387
Olivier Houchard44d59142018-12-13 18:46:22 +01002388 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002389 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002390 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2391 h1c->conn->owner = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002392 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) {
Olivier Houchard351411f2018-12-27 17:20:54 +01002393 /* The server doesn't want it, let's kill the connection right away */
2394 h1c->conn->mux->destroy(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002395 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2396 goto end;
2397 }
2398 tasklet_wakeup(h1c->wait_event.tasklet);
2399 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2400 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002401 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002402 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002403 if (h1c->conn->owner == sess) {
2404 int ret = session_check_idle_conn(sess, h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002405 if (ret == -1) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002406 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002407 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2408 goto end;
2409 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002410 else if (ret == 1) {
2411 /* The connection was added to the server list,
2412 * wake the task so we can subscribe to events
2413 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002414 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002415 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2416 goto end;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002417 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002418 TRACE_DEVEL("connection in idle session list", H1_EV_STRM_END, h1c->conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002419 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002420 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002421 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002422 struct server *srv = objt_server(h1c->conn->target);
2423
2424 if (srv) {
2425 if (h1c->conn->flags & CO_FL_PRIVATE)
2426 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002427 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002428 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2429 else
2430 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002431 TRACE_DEVEL("connection in idle server list", H1_EV_STRM_END, h1c->conn);
Christopher Faulet9400a392018-11-23 23:10:39 +01002432 }
2433 }
2434 }
2435
Christopher Fauletf1204b82019-07-19 14:51:06 +02002436 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002437 /* 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 +02002438 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2439 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2440 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002441 !h1c->conn->owner) {
2442 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002443 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002444 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002445 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002446 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002447 if (h1c->task) {
2448 h1c->task->expire = TICK_ETERNITY;
2449 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002450 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 +01002451 ? h1c->shut_timeout
2452 : h1c->timeout));
2453 task_queue(h1c->task);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002454 TRACE_DEVEL("refreshing connection's timeout", H1_EV_STRM_END, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002455 }
2456 }
2457 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002458 end:
2459 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002460}
2461
2462
2463static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2464{
2465 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002466 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002467
2468 if (!h1s)
2469 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002470 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002471
Christopher Faulet6b81df72019-10-01 22:08:43 +02002472 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2473
2474 if (cs->flags & CS_FL_KILL_CONN) {
2475 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2476 goto do_shutr;
2477 }
2478 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2479 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002480 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002481 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002482
Christopher Faulet6b81df72019-10-01 22:08:43 +02002483 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2484 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2485 goto end;
2486 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002487
Christopher Faulet7f366362019-04-08 10:51:20 +02002488 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002489 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2490 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002491 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002492 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002493 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002494 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002495 end:
2496 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002497}
2498
2499static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2500{
2501 struct h1s *h1s = cs->ctx;
2502 struct h1c *h1c;
2503
2504 if (!h1s)
2505 return;
2506 h1c = h1s->h1c;
2507
Christopher Faulet6b81df72019-10-01 22:08:43 +02002508 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2509
2510 if (cs->flags & CS_FL_KILL_CONN) {
2511 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002512 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002513 }
2514 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2515 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2516 goto do_shutw;
2517 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002518
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002519 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002520 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2521 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2522 goto end;
2523 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002524
Christopher Faulet7f366362019-04-08 10:51:20 +02002525 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002526 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2527 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002528 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002529 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002530 end:
2531 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002532}
2533
Christopher Faulet666a0c42019-01-08 11:12:04 +01002534static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002535{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002536 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002537
Christopher Faulet6b81df72019-10-01 22:08:43 +02002538 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002539 conn_xprt_shutw(conn);
2540 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002541 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002542 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002543}
2544
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002545/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2546 * The <es> pointer is not allowed to differ from the one passed to the
2547 * subscribe() call. It always returns zero.
2548 */
2549static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002550{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002551 struct h1s *h1s = cs->ctx;
2552
2553 if (!h1s)
2554 return 0;
2555
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002556 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002557 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002558
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002559 es->events &= ~event_type;
2560 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002561 h1s->subs = NULL;
2562
2563 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002564 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002565
2566 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002567 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002568
Christopher Faulet51dbc942018-09-13 09:05:15 +02002569 return 0;
2570}
2571
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002572/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2573 * event subscriber <es> is not allowed to change from a previous call as long
2574 * as at least one event is still subscribed. The <event_type> must only be a
2575 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2576 * the conn_stream <cs> was already detached, in which case it will return -1.
2577 */
2578static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002579{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002580 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002581 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002582
2583 if (!h1s)
2584 return -1;
2585
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002586 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2587 BUG_ON(h1s->subs && h1s->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002588 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002589
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002590 es->events |= event_type;
2591 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002592
2593 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002594 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002595
2596
Christopher Faulet6b81df72019-10-01 22:08:43 +02002597 if (event_type & SUB_RETRY_SEND) {
2598 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002599 /*
2600 * If the conn_stream attempt to subscribe, and the
2601 * mux isn't subscribed to the connection, then it
2602 * probably means the connection wasn't established
2603 * yet, so we have to subscribe.
2604 */
2605 h1c = h1s->h1c;
2606 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2607 h1c->conn->xprt->subscribe(h1c->conn,
2608 h1c->conn->xprt_ctx,
2609 SUB_RETRY_SEND,
2610 &h1c->wait_event);
2611 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002612 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002613}
2614
2615/* Called from the upper layer, to receive data */
2616static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2617{
2618 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002619 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002620 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002621 size_t ret = 0;
2622
Christopher Faulet6b81df72019-10-01 22:08:43 +02002623 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002624 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002625 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002626 else
2627 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002628
Christopher Faulete18777b2019-04-16 16:46:36 +02002629 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002630 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002631 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002632 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2633 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002634 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002635 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002636 if (h1s->flags & H1S_F_SPLICED_DATA) {
2637 h1s->flags &= ~H1S_F_SPLICED_DATA;
2638 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2639 }
2640 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002641 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002642 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002643 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002644 return ret;
2645}
2646
2647
2648/* Called from the upper layer, to send data */
2649static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2650{
2651 struct h1s *h1s = cs->ctx;
2652 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002653 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002654
2655 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002656 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002657 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002658
Christopher Faulet6b81df72019-10-01 22:08:43 +02002659 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){count});
2660
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002661 /* If we're not connected yet, or we're waiting for a handshake, stop
2662 * now, as we don't want to remove everything from the channel buffer
2663 * before we're sure we can send it.
2664 */
2665 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002666 (h1c->conn->flags & CO_FL_HANDSHAKE)) {
2667 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002668 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002669 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002670
Christopher Fauletc31872f2019-06-04 22:09:36 +02002671 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002672 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002673
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002674 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2675 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002676 else
2677 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002678 if (!ret)
2679 break;
2680 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002681 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002682 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002683 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002684 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002685
Christopher Faulet6b81df72019-10-01 22:08:43 +02002686 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002687 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002688}
2689
Willy Tarreaue5733232019-05-22 19:24:06 +02002690#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002691/* Send and get, using splicing */
2692static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2693{
2694 struct h1s *h1s = cs->ctx;
2695 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2696 int ret = 0;
2697
Christopher Faulet6b81df72019-10-01 22:08:43 +02002698 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){count});
2699
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002700 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002701 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002702 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2703 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2704 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002705 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002706 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002707 goto end;
2708 }
2709
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002710 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002711 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002712 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002713 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002714 }
2715
2716 h1s->flags &= ~H1S_F_BUF_FLUSH;
2717 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002718 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002719 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2720 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002721 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002722 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002723 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002724 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002725 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002726 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2727 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002728 }
2729
Christopher Faulet1be55f92018-10-02 15:59:23 +02002730 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002731 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002732 h1s->flags |= H1S_F_REOS;
Christopher Fauletf3158e92019-11-15 11:14:23 +01002733 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002734 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002735 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002736
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002737 if (h1m->state != H1_MSG_DATA || !h1m->curr_len)
2738 cs->flags &= ~CS_FL_MAY_SPLICE;
2739
Christopher Faulet6b81df72019-10-01 22:08:43 +02002740 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002741 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002742}
2743
2744static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2745{
2746 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002747 int ret = 0;
2748
Christopher Faulet6b81df72019-10-01 22:08:43 +02002749 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){pipe->data});
2750
Christopher Faulet1be55f92018-10-02 15:59:23 +02002751 if (b_data(&h1s->h1c->obuf))
2752 goto end;
2753
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002754 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002755 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002756 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002757 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2758 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002759 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002760 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002761 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002762
2763 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002764 return ret;
2765}
2766#endif
2767
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002768static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2769{
2770 int ret = 0;
2771 switch (mux_ctl) {
2772 case MUX_STATUS:
2773 if (conn->flags & CO_FL_CONNECTED)
2774 ret |= MUX_STATUS_READY;
2775 return ret;
2776 default:
2777 return -1;
2778 }
2779}
2780
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002781/* for debugging with CLI's "show fd" command */
2782static void h1_show_fd(struct buffer *msg, struct connection *conn)
2783{
2784 struct h1c *h1c = conn->ctx;
2785 struct h1s *h1s = h1c->h1s;
2786
Christopher Fauletf376a312019-01-04 15:16:06 +01002787 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2788 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002789 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2790 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2791 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2792 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2793
2794 if (h1s) {
2795 char *method;
2796
2797 if (h1s->meth < HTTP_METH_OTHER)
2798 method = http_known_methods[h1s->meth].ptr;
2799 else
2800 method = "UNKNOWN";
2801 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2802 " .meth=%s status=%d",
2803 h1s, h1s->flags,
2804 h1m_state_str(h1s->req.state),
2805 h1m_state_str(h1s->res.state), method, h1s->status);
2806 if (h1s->cs)
2807 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2808 h1s->cs->flags, h1s->cs->data);
2809 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002810}
2811
2812
2813/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2814static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2815{
2816 struct h1_hdr_entry *entry;
2817
2818 /* Be sure there is a non-empty <to> */
2819 if (!strlen(to)) {
2820 memprintf(err, "expect <to>");
2821 return -1;
2822 }
2823
2824 /* Be sure only the case differs between <from> and <to> */
2825 if (strcasecmp(from, to)) {
2826 memprintf(err, "<from> and <to> must not differ execpt the case");
2827 return -1;
2828 }
2829
2830 /* Be sure <from> does not already existsin the tree */
2831 if (ebis_lookup(&hdrs_map.map, from)) {
2832 memprintf(err, "duplicate entry '%s'", from);
2833 return -1;
2834 }
2835
2836 /* Create the entry and insert it in the tree */
2837 entry = malloc(sizeof(*entry));
2838 if (!entry) {
2839 memprintf(err, "out of memory");
2840 return -1;
2841 }
2842
2843 entry->node.key = strdup(from);
2844 entry->name.ptr = strdup(to);
2845 entry->name.len = strlen(to);
2846 if (!entry->node.key || !entry->name.ptr) {
2847 free(entry->node.key);
2848 free(entry->name.ptr);
2849 free(entry);
2850 memprintf(err, "out of memory");
2851 return -1;
2852 }
2853 ebis_insert(&hdrs_map.map, &entry->node);
2854 return 0;
2855}
2856
2857static void h1_hdeaders_case_adjust_deinit()
2858{
2859 struct ebpt_node *node, *next;
2860 struct h1_hdr_entry *entry;
2861
2862 node = ebpt_first(&hdrs_map.map);
2863 while (node) {
2864 next = ebpt_next(node);
2865 ebpt_delete(node);
2866 entry = container_of(node, struct h1_hdr_entry, node);
2867 free(entry->node.key);
2868 free(entry->name.ptr);
2869 free(entry);
2870 node = next;
2871 }
2872 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002873}
2874
Christopher Faulet98fbe952019-07-22 16:18:24 +02002875static int cfg_h1_headers_case_adjust_postparser()
2876{
2877 FILE *file = NULL;
2878 char *c, *key_beg, *key_end, *value_beg, *value_end;
2879 char *err;
2880 int rc, line = 0, err_code = 0;
2881
2882 if (!hdrs_map.name)
2883 goto end;
2884
2885 file = fopen(hdrs_map.name, "r");
2886 if (!file) {
2887 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2888 hdrs_map.name);
2889 err_code |= ERR_ALERT | ERR_FATAL;
2890 goto end;
2891 }
2892
2893 /* now parse all lines. The file may contain only two header name per
2894 * line, separated by spaces. All heading and trailing spaces will be
2895 * ignored. Lines starting with a # are ignored.
2896 */
2897 while (fgets(trash.area, trash.size, file) != NULL) {
2898 line++;
2899 c = trash.area;
2900
2901 /* strip leading spaces and tabs */
2902 while (*c == ' ' || *c == '\t')
2903 c++;
2904
2905 /* ignore emptu lines, or lines beginning with a dash */
2906 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2907 continue;
2908
2909 /* look for the end of the key */
2910 key_beg = c;
2911 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2912 c++;
2913 key_end = c;
2914
2915 /* strip middle spaces and tabs */
2916 while (*c == ' ' || *c == '\t')
2917 c++;
2918
2919 /* look for the end of the value, it is the end of the line */
2920 value_beg = c;
2921 while (*c && *c != '\n' && *c != '\r')
2922 c++;
2923 value_end = c;
2924
2925 /* trim possibly trailing spaces and tabs */
2926 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2927 value_end--;
2928
2929 /* set final \0 and check entries */
2930 *key_end = '\0';
2931 *value_end = '\0';
2932
2933 err = NULL;
2934 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2935 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002936 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2937 hdrs_map.name, err, line);
2938 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002939 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002940 goto end;
2941 }
2942 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002943 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2944 hdrs_map.name, err, line);
2945 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002946 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002947 }
2948 }
2949
2950 end:
2951 if (file)
2952 fclose(file);
2953 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2954 return err_code;
2955}
2956
2957
2958/* config parser for global "h1-outgoing-header-case-adjust" */
2959static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2960 struct proxy *defpx, const char *file, int line,
2961 char **err)
2962{
2963 if (too_many_args(2, args, err, NULL))
2964 return -1;
2965 if (!*(args[1]) || !*(args[2])) {
2966 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2967 return -1;
2968 }
2969 return add_hdr_case_adjust(args[1], args[2], err);
2970}
2971
2972/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2973static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2974 struct proxy *defpx, const char *file, int line,
2975 char **err)
2976{
2977 if (too_many_args(1, args, err, NULL))
2978 return -1;
2979 if (!*(args[1])) {
2980 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2981 return -1;
2982 }
2983 free(hdrs_map.name);
2984 hdrs_map.name = strdup(args[1]);
2985 return 0;
2986}
2987
2988
2989/* config keyword parsers */
2990static struct cfg_kw_list cfg_kws = {{ }, {
2991 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2992 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2993 { 0, NULL, NULL },
2994 }
2995};
2996
2997INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2998REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2999
3000
Christopher Faulet51dbc942018-09-13 09:05:15 +02003001/****************************************/
3002/* MUX initialization and instanciation */
3003/****************************************/
3004
3005/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003006static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003007 .init = h1_init,
3008 .wake = h1_wake,
3009 .attach = h1_attach,
3010 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01003011 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003012 .detach = h1_detach,
3013 .destroy = h1_destroy,
3014 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003015 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003016 .rcv_buf = h1_rcv_buf,
3017 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003018#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003019 .rcv_pipe = h1_rcv_pipe,
3020 .snd_pipe = h1_snd_pipe,
3021#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003022 .subscribe = h1_subscribe,
3023 .unsubscribe = h1_unsubscribe,
3024 .shutr = h1_shutr,
3025 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003026 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003027 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003028 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003029 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003030 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003031};
3032
3033
3034/* this mux registers default HTX proto */
3035static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003036{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003037
Willy Tarreau0108d902018-11-25 19:14:37 +01003038INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3039
Christopher Faulet51dbc942018-09-13 09:05:15 +02003040/*
3041 * Local variables:
3042 * c-indent-level: 8
3043 * c-basic-offset: 8
3044 * End:
3045 */