1 /*
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 <import/ebistree.h>
13 
14 #include <haproxy/api.h>
15 #include <haproxy/cfgparse.h>
16 #include <haproxy/connection.h>
17 #include <haproxy/h1.h>
18 #include <haproxy/h1_htx.h>
19 #include <haproxy/h2.h>
20 #include <haproxy/http_htx.h>
21 #include <haproxy/htx.h>
22 #include <haproxy/istbuf.h>
23 #include <haproxy/log.h>
24 #include <haproxy/pipe-t.h>
25 #include <haproxy/proxy-t.h>
26 #include <haproxy/session-t.h>
27 #include <haproxy/stream.h>
28 #include <haproxy/stream_interface.h>
29 #include <haproxy/trace.h>
30 
31 /*
32  *  H1 Connection flags (32 bits)
33  */
34 #define H1C_F_NONE           0x00000000
35 
36 /* Flags indicating why writing output data are blocked */
37 #define H1C_F_OUT_ALLOC      0x00000001 /* mux is blocked on lack of output buffer */
38 #define H1C_F_OUT_FULL       0x00000002 /* mux is blocked on output buffer full */
39 /* 0x00000004 - 0x00000008 unused */
40 
41 /* Flags indicating why reading input data are blocked. */
42 #define H1C_F_IN_ALLOC       0x00000010 /* mux is blocked on lack of input buffer */
43 #define H1C_F_IN_FULL        0x00000020 /* mux is blocked on input buffer full */
44 #define H1C_F_IN_BUSY        0x00000040 /* mux is blocked on input waiting the other side */
45 /* 0x00000080 unused */
46 
47 #define H1C_F_ST_SILENT_SHUT 0x00000100 /* silent (or dirty) shutdown must be performed */
48 /*  0x000000200 - 0x000000800 unused */
49 
50 /* Flags indicating the connection state */
51 #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 */
53 #define H1C_F_CS_SHUTDOWN    0x00004000 /* connection is shut down */
54 #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) */
56 
57 #define H1C_F_WAIT_NEXT_REQ  0x00010000 /*  waiting for the next request to start, use keep-alive timeout */
58 #define H1C_F_UPG_H2C        0x00020000 /* set if an upgrade to h2 should be done */
59 
60 #define H1C_F_CO_MSG_MORE    0x00040000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
61 #define H1C_F_CO_STREAMER    0x00080000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
62 
63 /*
64  * H1 Stream flags (32 bits)
65  */
66 #define H1S_F_NONE           0x00000000
67 #define H1S_F_ERROR          0x00000001 /* An error occurred on the H1 stream */
68 #define H1S_F_REQ_ERROR      0x00000002 /* An error occurred during the request parsing/xfer */
69 #define H1S_F_RES_ERROR      0x00000004 /* An error occurred during the response parsing/xfer */
70 
71 #define H1S_F_REOS           0x00000008 /* End of input stream seen even if not delivered yet */
72 #define H1S_F_WANT_KAL       0x00000010
73 #define H1S_F_WANT_TUN       0x00000020
74 #define H1S_F_WANT_CLO       0x00000040
75 #define H1S_F_WANT_MSK       0x00000070
76 #define H1S_F_NOT_FIRST      0x00000080 /* The H1 stream is not the first one */
77 #define H1S_F_BUF_FLUSH      0x00000100 /* Flush input buffer and don't read more data */
78 #define H1S_F_SPLICED_DATA   0x00000200 /* Set when the kernel splicing is in used */
79 #define H1S_F_PARSING_DONE   0x00000400 /* Set when incoming message parsing is finished (EOM added) */
80 /* 0x00000800 unused */
81 #define H1S_F_RX_CONGESTED   0x00001000 /* Cannot process input data RX path is congested (waiting for more space in channel's buffer) */
82 #define H1S_F_HAVE_SRV_NAME  0x00002000 /* Set during output process if the server name header was added to the request */
83 #define H1S_F_HAVE_O_CONN    0x00004000 /* Set during output process to know connection mode was processed */
84 
85 /* H1 connection descriptor */
86 struct h1c {
87 	struct connection *conn;
88 	struct proxy *px;
89 	uint32_t flags;                  /* Connection flags: H1C_F_* */
90 
91 	struct buffer ibuf;              /* Input buffer to store data before parsing */
92 	struct buffer obuf;              /* Output buffer to store data after reformatting */
93 
94 	struct buffer_wait buf_wait;     /* Wait list for buffer allocation */
95 	struct wait_event wait_event;    /* To be used if we're waiting for I/Os */
96 
97 	struct h1s *h1s;                 /* H1 stream descriptor */
98 	struct task *task;               /* timeout management task */
99 	int timeout;                     /* idle timeout duration in ticks */
100 	int shut_timeout;                /* idle timeout duration in ticks after stream shutdown */
101 };
102 
103 /* H1 stream descriptor */
104 struct h1s {
105 	struct h1c *h1c;
106 	struct conn_stream *cs;
107 	struct cs_info csinfo;         /* CS info, only used for client connections */
108 	uint32_t flags;                /* Connection flags: H1S_F_* */
109 
110 	struct wait_event *subs;      /* Address of the wait_event the conn_stream associated is waiting on */
111 
112 	struct session *sess;         /* Associated session */
113 	struct h1m req;
114 	struct h1m res;
115 
116 	enum http_meth_t meth; /* HTTP request method  */
117 	uint16_t status;       /* HTTP response status */
118 };
119 
120 /* Map of headers used to convert outgoing headers */
121 struct h1_hdrs_map {
122 	char *name;
123 	struct eb_root map;
124 };
125 
126 /* An entry in a headers map */
127 struct h1_hdr_entry  {
128 	struct ist name;
129 	struct ebpt_node node;
130 };
131 
132 /* Declare the headers map */
133 static struct h1_hdrs_map hdrs_map = { .name = NULL, .map  = EB_ROOT };
134 
135 
136 /* trace source and events */
137 static void h1_trace(enum trace_level level, uint64_t mask,
138                      const struct trace_source *src,
139                      const struct ist where, const struct ist func,
140                      const void *a1, const void *a2, const void *a3, const void *a4);
141 
142 /* The event representation is split like this :
143  *   h1c   - internal H1 connection
144  *   h1s   - internal H1 stream
145  *   strm  - application layer
146  *   rx    - data receipt
147  *   tx    - data transmission
148  *
149  */
150 static const struct trace_event h1_trace_events[] = {
151 #define           H1_EV_H1C_NEW       (1ULL <<  0)
152 	{ .mask = H1_EV_H1C_NEW,      .name = "h1c_new",      .desc = "new H1 connection" },
153 #define           H1_EV_H1C_RECV      (1ULL <<  1)
154 	{ .mask = H1_EV_H1C_RECV,     .name = "h1c_recv",     .desc = "Rx on H1 connection" },
155 #define           H1_EV_H1C_SEND      (1ULL <<  2)
156 	{ .mask = H1_EV_H1C_SEND,     .name = "h1c_send",     .desc = "Tx on H1 connection" },
157 #define           H1_EV_H1C_BLK       (1ULL <<  3)
158 	{ .mask = H1_EV_H1C_BLK,      .name = "h1c_blk",      .desc = "H1 connection blocked" },
159 #define           H1_EV_H1C_WAKE      (1ULL <<  4)
160 	{ .mask = H1_EV_H1C_WAKE,     .name = "h1c_wake",     .desc = "H1 connection woken up" },
161 #define           H1_EV_H1C_END       (1ULL <<  5)
162 	{ .mask = H1_EV_H1C_END,      .name = "h1c_end",      .desc = "H1 connection terminated" },
163 #define           H1_EV_H1C_ERR       (1ULL <<  6)
164 	{ .mask = H1_EV_H1C_ERR,      .name = "h1c_err",      .desc = "error on H1 connection" },
165 
166 #define           H1_EV_RX_DATA       (1ULL <<  7)
167 	{ .mask = H1_EV_RX_DATA,      .name = "rx_data",      .desc = "receipt of any H1 data" },
168 #define           H1_EV_RX_EOI        (1ULL <<  8)
169 	{ .mask = H1_EV_RX_EOI,       .name = "rx_eoi",       .desc = "receipt of end of H1 input" },
170 #define           H1_EV_RX_HDRS       (1ULL <<  9)
171 	{ .mask = H1_EV_RX_HDRS,      .name = "rx_headers",   .desc = "receipt of H1 headers" },
172 #define           H1_EV_RX_BODY       (1ULL << 10)
173 	{ .mask = H1_EV_RX_BODY,      .name = "rx_body",      .desc = "receipt of H1 body" },
174 #define           H1_EV_RX_TLRS       (1ULL << 11)
175 	{ .mask = H1_EV_RX_TLRS,      .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
176 
177 #define           H1_EV_TX_DATA       (1ULL << 12)
178 	{ .mask = H1_EV_TX_DATA,      .name = "tx_data",      .desc = "transmission of any H1 data" },
179 #define           H1_EV_TX_EOI        (1ULL << 13)
180 	{ .mask = H1_EV_TX_EOI,       .name = "tx_eoi",       .desc = "transmission of end of H1 input" },
181 #define           H1_EV_TX_HDRS       (1ULL << 14)
182 	{ .mask = H1_EV_TX_HDRS,      .name = "tx_headers",   .desc = "transmission of all headers" },
183 #define           H1_EV_TX_BODY       (1ULL << 15)
184 	{ .mask = H1_EV_TX_BODY,      .name = "tx_body",      .desc = "transmission of H1 body" },
185 #define           H1_EV_TX_TLRS       (1ULL << 16)
186 	{ .mask = H1_EV_TX_TLRS,      .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
187 
188 #define           H1_EV_H1S_NEW       (1ULL << 17)
189 	{ .mask = H1_EV_H1S_NEW,      .name = "h1s_new",     .desc = "new H1 stream" },
190 #define           H1_EV_H1S_BLK       (1ULL << 18)
191 	{ .mask = H1_EV_H1S_BLK,      .name = "h1s_blk",     .desc = "H1 stream blocked" },
192 #define           H1_EV_H1S_END       (1ULL << 19)
193 	{ .mask = H1_EV_H1S_END,      .name = "h1s_end",     .desc = "H1 stream terminated" },
194 #define           H1_EV_H1S_ERR       (1ULL << 20)
195 	{ .mask = H1_EV_H1S_ERR,      .name = "h1s_err",     .desc = "error on H1 stream" },
196 
197 #define           H1_EV_STRM_NEW      (1ULL << 21)
198 	{ .mask = H1_EV_STRM_NEW,     .name = "strm_new",    .desc = "app-layer stream creation" },
199 #define           H1_EV_STRM_RECV     (1ULL << 22)
200 	{ .mask = H1_EV_STRM_RECV,    .name = "strm_recv",   .desc = "receiving data for stream" },
201 #define           H1_EV_STRM_SEND     (1ULL << 23)
202 	{ .mask = H1_EV_STRM_SEND,    .name = "strm_send",   .desc = "sending data for stream" },
203 #define           H1_EV_STRM_WAKE     (1ULL << 24)
204 	{ .mask = H1_EV_STRM_WAKE,    .name = "strm_wake",   .desc = "stream woken up" },
205 #define           H1_EV_STRM_SHUT     (1ULL << 25)
206 	{ .mask = H1_EV_STRM_SHUT,    .name = "strm_shut",   .desc = "stream shutdown" },
207 #define           H1_EV_STRM_END      (1ULL << 26)
208 	{ .mask = H1_EV_STRM_END,     .name = "strm_end",    .desc = "detaching app-layer stream" },
209 #define           H1_EV_STRM_ERR      (1ULL << 27)
210 	{ .mask = H1_EV_STRM_ERR,     .name = "strm_err",    .desc = "stream error" },
211 
212 	{ }
213 };
214 
215 static const struct name_desc h1_trace_lockon_args[4] = {
216 	/* arg1 */ { /* already used by the connection */ },
217 	/* arg2 */ { .name="h1s", .desc="H1 stream" },
218 	/* arg3 */ { },
219 	/* arg4 */ { }
220 };
221 
222 static const struct name_desc h1_trace_decoding[] = {
223 #define H1_VERB_CLEAN    1
224 	{ .name="clean",    .desc="only user-friendly stuff, generally suitable for level \"user\"" },
225 #define H1_VERB_MINIMAL  2
226 	{ .name="minimal",  .desc="report only h1c/h1s state and flags, no real decoding" },
227 #define H1_VERB_SIMPLE   3
228 	{ .name="simple",   .desc="add request/response status line or htx info when available" },
229 #define H1_VERB_ADVANCED 4
230 	{ .name="advanced", .desc="add header fields or frame decoding when available" },
231 #define H1_VERB_COMPLETE 5
232 	{ .name="complete", .desc="add full data dump when available" },
233 	{ /* end */ }
234 };
235 
236 static struct trace_source trace_h1 = {
237 	.name = IST("h1"),
238 	.desc = "HTTP/1 multiplexer",
239 	.arg_def = TRC_ARG1_CONN,  // TRACE()'s first argument is always a connection
240 	.default_cb = h1_trace,
241 	.known_events = h1_trace_events,
242 	.lockon_args = h1_trace_lockon_args,
243 	.decoding = h1_trace_decoding,
244 	.report_events = ~0,  // report everything by default
245 };
246 
247 #define TRACE_SOURCE &trace_h1
248 INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
249 
250 /* the h1c and h1s pools */
251 DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
252 DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
253 
254 static int h1_recv(struct h1c *h1c);
255 static int h1_send(struct h1c *h1c);
256 static int h1_process(struct h1c *h1c);
257 static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
258 static void h1_shutw_conn(struct connection *conn);
259 static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
260 static void h1_wake_stream_for_recv(struct h1s *h1s);
261 static void h1_wake_stream_for_send(struct h1s *h1s);
262 
263 /* the H1 traces always expect that arg1, if non-null, is of type connection
264  * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
265  * that arg3, if non-null, is a htx for rx/tx headers.
266  */
h1_trace(enum trace_level level,uint64_t mask,const struct trace_source * src,const struct ist where,const struct ist func,const void * a1,const void * a2,const void * a3,const void * a4)267 static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
268                      const struct ist where, const struct ist func,
269                      const void *a1, const void *a2, const void *a3, const void *a4)
270 {
271 	const struct connection *conn = a1;
272 	const struct h1c *h1c = conn ? conn->ctx : NULL;
273 	const struct h1s *h1s = a2;
274 	const struct htx *htx = a3;
275 	const size_t     *val = a4;
276 
277 	if (!h1c)
278 		h1c = (h1s ? h1s->h1c : NULL);
279 
280 	if (!h1c || src->verbosity < H1_VERB_CLEAN)
281 		return;
282 
283 	/* Display frontend/backend info by default */
284 	chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
285 
286 	/* Display request and response states if h1s is defined */
287 	if (h1s)
288 		chunk_appendf(&trace_buf, " [%s, %s]",
289 			      h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
290 
291 	if (src->verbosity == H1_VERB_CLEAN)
292 		return;
293 
294 	/* Display the value to the 4th argument (level > STATE) */
295 	if (src->level > TRACE_LEVEL_STATE && val)
296 		chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
297 
298 	/* Display status-line if possible (verbosity > MINIMAL) */
299 	if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
300 		const struct htx_blk *blk = htx_get_head_blk(htx);
301 		const struct htx_sl  *sl  = htx_get_blk_ptr(htx, blk);
302 		enum htx_blk_type    type = htx_get_blk_type(blk);
303 
304 		if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
305 			chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
306 				      HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
307 				      HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
308 				      HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
309 	}
310 
311 	/* Display h1c info and, if defined, h1s info (pointer + flags) */
312 	chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
313 	if (h1s)
314 		chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
315 
316 	if (src->verbosity == H1_VERB_MINIMAL)
317 		return;
318 
319 	/* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
320 	if (src->level > TRACE_LEVEL_USER) {
321 		if (src->verbosity == H1_VERB_COMPLETE ||
322 		    (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
323 			chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
324 				      (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
325 				      (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
326 		if (src->verbosity == H1_VERB_COMPLETE ||
327 		    (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
328 			chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
329 				      (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
330 				      (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
331 	}
332 
333 	/* Display htx info if defined (level > USER) */
334 	if (src->level > TRACE_LEVEL_USER && htx) {
335 		int full = 0;
336 
337 		/* Full htx info (level > STATE && verbosity > SIMPLE) */
338 		if (src->level > TRACE_LEVEL_STATE) {
339 			if (src->verbosity == H1_VERB_COMPLETE)
340 				full = 1;
341 			else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
342 				full = 1;
343 		}
344 
345 		chunk_memcat(&trace_buf, "\n\t", 2);
346 		htx_dump(&trace_buf, htx, full);
347 	}
348 }
349 
350 
351 /*****************************************************/
352 /* functions below are for dynamic buffer management */
353 /*****************************************************/
354 /*
355  * Indicates whether or not we may receive data. The rules are the following :
356  *   - if an error or a shutdown for reads was detected on the connection we
357        must not attempt to receive
358  *   - if the input buffer failed to be allocated or is full , we must not try
359  *     to receive
360  *   - if he input processing is busy waiting for the output side, we may
361  *     attempt to receive
362  *   - otherwise must may not attempt to receive
363  */
h1_recv_allowed(const struct h1c * h1c)364 static inline int h1_recv_allowed(const struct h1c *h1c)
365 {
366 	if (h1c->flags & H1C_F_CS_ERROR) {
367 		TRACE_DEVEL("recv not allowed because of error on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
368 		return 0;
369 	}
370 
371 	if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH)) {
372 		TRACE_DEVEL("recv not allowed because of (error|read0) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
373 		return 0;
374 	}
375 
376 	if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL)))
377 		return 1;
378 
379 	TRACE_DEVEL("recv not allowed because input is blocked", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
380 	return 0;
381 }
382 
383 /*
384  * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
385  * flags are used to figure what buffer was requested. It returns 1 if the
386  * allocation succeeds, in which case the connection is woken up, or 0 if it's
387  * impossible to wake up and we prefer to be woken up later.
388  */
h1_buf_available(void * target)389 static int h1_buf_available(void *target)
390 {
391 	struct h1c *h1c = target;
392 
393 	if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
394 		TRACE_STATE("unblocking h1c, ibuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
395 		h1c->flags &= ~H1C_F_IN_ALLOC;
396 		if (h1_recv_allowed(h1c))
397 			tasklet_wakeup(h1c->wait_event.tasklet);
398 		return 1;
399 	}
400 
401 	if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
402 		TRACE_STATE("unblocking h1s, obuf allocated", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1c->h1s);
403 		h1c->flags &= ~H1C_F_OUT_ALLOC;
404 		tasklet_wakeup(h1c->wait_event.tasklet);
405 		if (h1c->h1s)
406 			h1_wake_stream_for_send(h1c->h1s);
407 		return 1;
408 	}
409 
410 	return 0;
411 }
412 
413 /*
414  * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
415  */
h1_get_buf(struct h1c * h1c,struct buffer * bptr)416 static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
417 {
418 	struct buffer *buf = NULL;
419 
420 	if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
421 	    unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
422 		h1c->buf_wait.target = h1c;
423 		h1c->buf_wait.wakeup_cb = h1_buf_available;
424 		MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
425 	}
426 	return buf;
427 }
428 
429 /*
430  * Release a buffer, if any, and try to wake up entities waiting in the buffer
431  * wait queue.
432  */
h1_release_buf(struct h1c * h1c,struct buffer * bptr)433 static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
434 {
435 	if (bptr->size) {
436 		b_free(bptr);
437 		offer_buffers(h1c->buf_wait.target, tasks_run_queue);
438 	}
439 }
440 
441 /* returns the number of streams in use on a connection to figure if it's idle
442  * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
443  * not. This flag is only set when no H1S is attached and when the previous
444  * stream, if any, was fully terminated without any error and in K/A mode.
445  */
h1_used_streams(struct connection * conn)446 static int h1_used_streams(struct connection *conn)
447 {
448 	struct h1c *h1c = conn->ctx;
449 
450 	return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
451 }
452 
453 /* returns the number of streams still available on a connection */
h1_avail_streams(struct connection * conn)454 static int h1_avail_streams(struct connection *conn)
455 {
456 	return 1 - h1_used_streams(conn);
457 }
458 
459 /* Refresh the h1c task timeout if necessary */
h1_refresh_timeout(struct h1c * h1c)460 static void h1_refresh_timeout(struct h1c *h1c)
461 {
462 	if (h1c->task) {
463 		h1c->task->expire = TICK_ETERNITY;
464 		if (h1c->flags & H1C_F_CS_SHUTDOWN) {
465 			/* half-closed connections switch to clientfin/serverfin
466 			 * timeouts so that we don't hang too long on clients
467 			 * that have gone away (especially in tunnel mode).
468 			 */
469 			h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
470 			task_queue(h1c->task);
471 			TRACE_DEVEL("refreshing connection's timeout (half-closed)", H1_EV_H1C_SEND, h1c->conn);
472 		} else if (b_data(&h1c->obuf)) {
473 			/* any connection with pending data, need a timeout (server or client).
474 			 */
475 			h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
476 							      ? h1c->shut_timeout
477 							      : h1c->timeout));
478 			task_queue(h1c->task);
479 			TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
480 		} else if ((h1c->flags & (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ)) && !conn_is_back(h1c->conn)) {
481 			/* front connections waiting for a stream need a timeout. client timeout by
482 			 * default but http-keep-alive if defined
483 			 */
484 			int timeout = h1c->timeout;
485 
486 			if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
487 				timeout = tick_first(timeout, h1c->px->timeout.httpka);
488 
489 			h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
490 							      ? h1c->shut_timeout
491 							      : timeout));
492 			task_queue(h1c->task);
493 			TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
494 		}
495 	}
496 }
497 /*****************************************************************/
498 /* functions below are dedicated to the mux setup and management */
499 /*****************************************************************/
500 
501 /* returns non-zero if there are input data pending for stream h1s. */
h1s_data_pending(const struct h1s * h1s)502 static inline size_t h1s_data_pending(const struct h1s *h1s)
503 {
504 	const struct h1m *h1m;
505 
506 	h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
507 	if (h1m->state == H1_MSG_DONE)
508 		return !(h1s->flags & H1S_F_PARSING_DONE);
509 
510 	return b_data(&h1s->h1c->ibuf);
511 }
512 
h1s_new_cs(struct h1s * h1s)513 static struct conn_stream *h1s_new_cs(struct h1s *h1s)
514 {
515 	struct conn_stream *cs;
516 
517 	TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
518 	cs = cs_new(h1s->h1c->conn);
519 	if (!cs) {
520 		TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
521 		goto err;
522 	}
523 	h1s->cs = cs;
524 	cs->ctx = h1s;
525 
526 	if (h1s->flags & H1S_F_NOT_FIRST)
527 		cs->flags |= CS_FL_NOT_FIRST;
528 
529 	if (global.tune.options & GTUNE_USE_SPLICE) {
530 		TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
531 		cs->flags |= CS_FL_MAY_SPLICE;
532 	}
533 
534 	if (stream_create_from_cs(cs) < 0) {
535 		TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
536 		goto err;
537 	}
538 
539 	TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
540 	return cs;
541 
542   err:
543 	cs_free(cs);
544 	h1s->cs = NULL;
545 	return NULL;
546 }
547 
h1s_create(struct h1c * h1c,struct conn_stream * cs,struct session * sess)548 static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
549 {
550 	struct h1s *h1s;
551 
552 	TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
553 
554 	h1s = pool_alloc(pool_head_h1s);
555 	if (!h1s)
556 		goto fail;
557 
558 	h1s->h1c = h1c;
559 	h1c->h1s = h1s;
560 
561 	h1s->sess = sess;
562 
563 	h1s->cs    = NULL;
564 	h1s->flags = H1S_F_WANT_KAL;
565 
566 	h1s->subs = NULL;
567 
568 	h1m_init_req(&h1s->req);
569 	h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
570 
571 	h1m_init_res(&h1s->res);
572 	h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
573 
574 	h1s->status = 0;
575 	h1s->meth   = HTTP_METH_OTHER;
576 
577 	if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
578 		h1s->flags |= H1S_F_NOT_FIRST;
579 	h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
580 
581 	if (!conn_is_back(h1c->conn)) {
582 		if (h1c->px->options2 & PR_O2_REQBUG_OK)
583 			h1s->req.err_pos = -1;
584 
585 		/* For frontend connections we should always have a session */
586 		if (!sess)
587 			h1s->sess = sess = h1c->conn->owner;
588 
589 		/* Timers for subsequent sessions on the same HTTP 1.x connection
590 		 * measure from `now`, not from the connection accept time */
591 		if (h1s->flags & H1S_F_NOT_FIRST) {
592 			h1s->csinfo.create_date = date;
593 			h1s->csinfo.tv_create   = now;
594 			h1s->csinfo.t_handshake = 0;
595 			h1s->csinfo.t_idle      = -1;
596 		}
597 		else {
598 			h1s->csinfo.create_date = sess->accept_date;
599 			h1s->csinfo.tv_create   = sess->tv_accept;
600 			h1s->csinfo.t_handshake = sess->t_handshake;
601 			h1s->csinfo.t_idle      = -1;
602 		}
603 	}
604 	else {
605 		if (h1c->px->options2 & PR_O2_RSPBUG_OK)
606 			h1s->res.err_pos = -1;
607 
608 		h1s->csinfo.create_date = date;
609 		h1s->csinfo.tv_create   = now;
610 		h1s->csinfo.t_handshake = 0;
611 		h1s->csinfo.t_idle      = -1;
612 	}
613 
614 	/* If a conn_stream already exists, attach it to this H1S. Otherwise we
615 	 * create a new one.
616 	 */
617 	if (cs) {
618 		cs->ctx = h1s;
619 		h1s->cs = cs;
620 	}
621 	else {
622 		cs = h1s_new_cs(h1s);
623 		if (!cs)
624 			goto fail;
625 	}
626 	TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
627 	return h1s;
628 
629   fail:
630 	pool_free(pool_head_h1s, h1s);
631 	TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
632 	return NULL;
633 }
634 
h1s_destroy(struct h1s * h1s)635 static void h1s_destroy(struct h1s *h1s)
636 {
637 	if (h1s) {
638 		struct h1c *h1c = h1s->h1c;
639 
640 		TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
641 		h1c->h1s = NULL;
642 
643 		if (h1s->subs)
644 			h1s->subs->events = 0;
645 
646 		h1c->flags &= ~H1C_F_IN_BUSY;
647 		if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
648 			h1c->flags |= H1C_F_CS_ERROR;
649 			TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
650 		}
651 
652 		if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
653 		    !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
654 		    (h1s->flags & (H1S_F_WANT_KAL|H1S_F_PARSING_DONE)) == (H1S_F_WANT_KAL|H1S_F_PARSING_DONE) && /* K/A possible */
655 		    h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {        /* req/res in DONE state */
656 			h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
657 			TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
658 		}
659 		pool_free(pool_head_h1s, h1s);
660 	}
661 }
662 
h1_get_cs_info(struct conn_stream * cs)663 static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
664 {
665 	struct h1s *h1s = cs->ctx;
666 
667 	if (h1s && !conn_is_back(cs->conn))
668 		return &h1s->csinfo;
669 	return NULL;
670 }
671 
672 /*
673  * Initialize the mux once it's attached. It is expected that conn->ctx points
674  * to the existing conn_stream (for outgoing connections or for incoming ones
675  * during a mux upgrade) or NULL (for incoming ones during the connection
676  * establishment). <input> is always used as Input buffer and may contain
677  * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
678  * error.
679  */
h1_init(struct connection * conn,struct proxy * proxy,struct session * sess,struct buffer * input)680 static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
681 		   struct buffer *input)
682 {
683 	struct h1c *h1c;
684 	struct task *t = NULL;
685 	void *conn_ctx = conn->ctx;
686 
687 	TRACE_ENTER(H1_EV_H1C_NEW);
688 
689 	h1c = pool_alloc(pool_head_h1c);
690 	if (!h1c)
691 		goto fail_h1c;
692 	h1c->conn = conn;
693 	h1c->px   = proxy;
694 
695 	h1c->flags = H1C_F_CS_IDLE;
696 	h1c->ibuf  = *input;
697 	h1c->obuf  = BUF_NULL;
698 	h1c->h1s   = NULL;
699 	h1c->task  = NULL;
700 
701 	MT_LIST_INIT(&h1c->buf_wait.list);
702 	h1c->wait_event.tasklet = tasklet_new();
703 	if (!h1c->wait_event.tasklet)
704 		goto fail;
705 	h1c->wait_event.tasklet->process = h1_io_cb;
706 	h1c->wait_event.tasklet->context = h1c;
707 	h1c->wait_event.events   = 0;
708 
709 	if (conn_is_back(conn)) {
710 		h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
711 		if (tick_isset(proxy->timeout.serverfin))
712 			h1c->shut_timeout = proxy->timeout.serverfin;
713 	} else {
714 		h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
715 		if (tick_isset(proxy->timeout.clientfin))
716 			h1c->shut_timeout = proxy->timeout.clientfin;
717 	}
718 	if (tick_isset(h1c->timeout)) {
719 		t = task_new(tid_bit);
720 		if (!t)
721 			goto fail;
722 
723 		h1c->task = t;
724 		t->process = h1_timeout_task;
725 		t->context = h1c;
726 		t->expire = tick_add(now_ms, h1c->timeout);
727 	}
728 
729 	conn->ctx = h1c;
730 
731 	/* Always Create a new H1S */
732 	if (!h1s_create(h1c, conn_ctx, sess))
733 		goto fail;
734 
735 	if (t)
736 		task_queue(t);
737 
738 	/* Try to read, if nothing is available yet we'll just subscribe */
739 	h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
740 
741 	/* mux->wake will be called soon to complete the operation */
742 	TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
743 	return 0;
744 
745   fail:
746 	task_destroy(t);
747 	if (h1c->wait_event.tasklet)
748 		tasklet_free(h1c->wait_event.tasklet);
749 	pool_free(pool_head_h1c, h1c);
750  fail_h1c:
751 	conn->ctx = conn_ctx; // restore saved context
752 	TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
753 	return -1;
754 }
755 
756 /* release function. This one should be called to free all resources allocated
757  * to the mux.
758  */
h1_release(struct h1c * h1c)759 static void h1_release(struct h1c *h1c)
760 {
761 	struct connection *conn = NULL;
762 
763 	TRACE_POINT(H1_EV_H1C_END);
764 
765 	if (h1c) {
766 		/* The connection must be aattached to this mux to be released */
767 		if (h1c->conn && h1c->conn->ctx == h1c)
768 			conn = h1c->conn;
769 
770 		TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
771 
772 		if (conn && h1c->flags & H1C_F_UPG_H2C) {
773 			TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
774 			h1c->flags &= ~H1C_F_UPG_H2C;
775 			/* Make sure we're no longer subscribed to anything */
776 			if (h1c->wait_event.events)
777 				conn->xprt->unsubscribe(conn, conn->xprt_ctx,
778 				    h1c->wait_event.events, &h1c->wait_event);
779 			if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
780 				/* connection successfully upgraded to H2, this
781 				 * mux was already released */
782 				return;
783 			}
784 			TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
785 			sess_log(conn->owner); /* Log if the upgrade failed */
786 		}
787 
788 
789 		if (MT_LIST_ADDED(&h1c->buf_wait.list))
790 			MT_LIST_DEL(&h1c->buf_wait.list);
791 
792 		h1_release_buf(h1c, &h1c->ibuf);
793 		h1_release_buf(h1c, &h1c->obuf);
794 
795 		if (h1c->task) {
796 			h1c->task->context = NULL;
797 			task_wakeup(h1c->task, TASK_WOKEN_OTHER);
798 			h1c->task = NULL;
799 		}
800 
801 		if (h1c->wait_event.tasklet)
802 			tasklet_free(h1c->wait_event.tasklet);
803 
804 		h1s_destroy(h1c->h1s);
805 		if (conn) {
806 			if (h1c->wait_event.events != 0)
807 				conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
808 							&h1c->wait_event);
809 			h1_shutw_conn(conn);
810 		}
811 		pool_free(pool_head_h1c, h1c);
812 	}
813 
814 	if (conn) {
815 		conn->mux = NULL;
816 		conn->ctx = NULL;
817 		TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
818 
819 		conn_stop_tracking(conn);
820 		conn_full_close(conn);
821 		if (conn->destroy_cb)
822 			conn->destroy_cb(conn);
823 		conn_free(conn);
824 	}
825 }
826 
827 /******************************************************/
828 /* functions below are for the H1 protocol processing */
829 /******************************************************/
830 /* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
831  * greater or equal to 1.1
832  */
h1_parse_req_vsn(struct h1m * h1m,const struct htx_sl * sl)833 static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
834 {
835 	const char *p = HTX_SL_REQ_VPTR(sl);
836 
837 	if ((HTX_SL_REQ_VLEN(sl) == 8) &&
838 	    (*(p + 5) > '1' ||
839 	     (*(p + 5) == '1' && *(p + 7) >= '1')))
840 		h1m->flags |= H1_MF_VER_11;
841 }
842 
843 /* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
844  * greater or equal to 1.1
845  */
h1_parse_res_vsn(struct h1m * h1m,const struct htx_sl * sl)846 static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
847 {
848 	const char *p = HTX_SL_RES_VPTR(sl);
849 
850 	if ((HTX_SL_RES_VLEN(sl) == 8) &&
851 	    (*(p + 5) > '1' ||
852 	     (*(p + 5) == '1' && *(p + 7) >= '1')))
853 		h1m->flags |= H1_MF_VER_11;
854 }
855 
856 /* Deduce the connection mode of the client connection, depending on the
857  * configuration and the H1 message flags. This function is called twice, the
858  * first time when the request is parsed and the second time when the response
859  * is parsed.
860  */
h1_set_cli_conn_mode(struct h1s * h1s,struct h1m * h1m)861 static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
862 {
863 	struct proxy *fe = h1s->h1c->px;
864 
865 	if (h1m->flags & H1_MF_RESP) {
866 		/* Output direction: second pass */
867 		if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
868 		    h1s->status == 101) {
869 			/* Either we've established an explicit tunnel, or we're
870 			 * switching the protocol. In both cases, we're very unlikely to
871 			 * understand the next protocols. We have to switch to tunnel
872 			 * mode, so that we transfer the request and responses then let
873 			 * this protocol pass unmodified. When we later implement
874 			 * specific parsers for such protocols, we'll want to check the
875 			 * Upgrade header which contains information about that protocol
876 			 * for responses with status 101 (eg: see RFC2817 about TLS).
877 			 */
878 			h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
879 			TRACE_STATE("set tunnel mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
880 		}
881 		else if (h1s->flags & H1S_F_WANT_KAL) {
882 			/* By default the client is in KAL mode. CLOSE mode mean
883 			 * it is imposed by the client itself. So only change
884 			 * KAL mode here. */
885 			if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
886 				/* no length known or explicit close => close */
887 				h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
888 				TRACE_STATE("detect close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
889 			}
890 			else if (!(h1m->flags & H1_MF_CONN_KAL) &&
891 				 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
892 				/* no explicit keep-alive and option httpclose => close */
893 				h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
894 				TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
895 			}
896 		}
897 	}
898 	else {
899 		/* Input direction: first pass */
900 		if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO)  {
901 			/* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
902 			h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
903 			TRACE_STATE("detect close mode (req)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
904 		}
905 	}
906 
907 	/* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
908 	if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
909 		h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
910 		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);
911 	}
912 }
913 
914 /* Deduce the connection mode of the client connection, depending on the
915  * configuration and the H1 message flags. This function is called twice, the
916  * first time when the request is parsed and the second time when the response
917  * is parsed.
918  */
h1_set_srv_conn_mode(struct h1s * h1s,struct h1m * h1m)919 static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
920 {
921 	struct session *sess = h1s->sess;
922 	struct proxy *be = h1s->h1c->px;
923 	int fe_flags = sess ? sess->fe->options : 0;
924 
925 	if (h1m->flags & H1_MF_RESP) {
926 		/* Input direction: second pass */
927 		if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
928 		    h1s->status == 101) {
929 			/* Either we've established an explicit tunnel, or we're
930 			 * switching the protocol. In both cases, we're very unlikely to
931 			 * understand the next protocols. We have to switch to tunnel
932 			 * mode, so that we transfer the request and responses then let
933 			 * this protocol pass unmodified. When we later implement
934 			 * specific parsers for such protocols, we'll want to check the
935 			 * Upgrade header which contains information about that protocol
936 			 * for responses with status 101 (eg: see RFC2817 about TLS).
937 			 */
938 			h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
939 			TRACE_STATE("set tunnel mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
940 		}
941 		else if (h1s->flags & H1S_F_WANT_KAL) {
942 			/* By default the server is in KAL mode. CLOSE mode mean
943 			 * it is imposed by haproxy itself. So only change KAL
944 			 * mode here. */
945 			if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
946 			    !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
947 				/* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
948 				h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
949 				TRACE_STATE("detect close mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
950 			}
951 		}
952 	}
953 	else {
954 		/* Output direction: first pass */
955 		if (h1m->flags & H1_MF_CONN_CLO) {
956 			/* explicit close => close */
957 			h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
958 			TRACE_STATE("detect close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
959 		}
960 		else if (!(h1m->flags & H1_MF_CONN_KAL) &&
961 			 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
962 			  (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
963 			  (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
964 			  (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
965 			/* no explicit keep-alive option httpclose/server-close => close */
966 			h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
967 			TRACE_STATE("force close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
968 		}
969 	}
970 
971 	/* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
972 	if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
973 		h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
974 		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);
975 	}
976 }
977 
h1_update_req_conn_value(struct h1s * h1s,struct h1m * h1m,struct ist * conn_val)978 static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
979 {
980 	struct proxy *px = h1s->h1c->px;
981 
982 	/* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
983 	 * token is found
984 	 */
985 	if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
986 		return;
987 
988 	if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
989 		if (!(h1m->flags & H1_MF_VER_11)) {
990 			TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
991 			*conn_val = ist("keep-alive");
992 		}
993 	}
994 	else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
995 		if (h1m->flags & H1_MF_VER_11) {
996 			TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
997 			*conn_val = ist("close");
998 		}
999 	}
1000 }
1001 
h1_update_res_conn_value(struct h1s * h1s,struct h1m * h1m,struct ist * conn_val)1002 static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1003 {
1004 	/* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1005 	 * token is found
1006 	 */
1007 	if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
1008 		return;
1009 
1010 	if (h1s->flags & H1S_F_WANT_KAL) {
1011 		if (!(h1m->flags & H1_MF_VER_11) ||
1012 		    !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1013 			TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1014 			*conn_val = ist("keep-alive");
1015 		}
1016 	}
1017 	else { /* H1S_F_WANT_CLO */
1018 		if (h1m->flags & H1_MF_VER_11) {
1019 			TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1020 			*conn_val = ist("close");
1021 		}
1022 	}
1023 }
1024 
h1_process_input_conn_mode(struct h1s * h1s,struct h1m * h1m,struct htx * htx)1025 static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1026 {
1027 	if (!conn_is_back(h1s->h1c->conn))
1028 		h1_set_cli_conn_mode(h1s, h1m);
1029 	else
1030 		h1_set_srv_conn_mode(h1s, h1m);
1031 }
1032 
h1_process_output_conn_mode(struct h1s * h1s,struct h1m * h1m,struct ist * conn_val)1033 static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1034 {
1035 	if (!conn_is_back(h1s->h1c->conn))
1036 		h1_set_cli_conn_mode(h1s, h1m);
1037 	else
1038 		h1_set_srv_conn_mode(h1s, h1m);
1039 
1040 	if (!(h1m->flags & H1_MF_RESP))
1041 		h1_update_req_conn_value(h1s, h1m, conn_val);
1042 	else
1043 		h1_update_res_conn_value(h1s, h1m, conn_val);
1044 }
1045 
1046 /* Try to adjust the case of the message header name using the global map
1047  * <hdrs_map>.
1048  */
h1_adjust_case_outgoing_hdr(struct h1s * h1s,struct h1m * h1m,struct ist * name)1049 static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1050 {
1051 	struct ebpt_node *node;
1052 	struct h1_hdr_entry *entry;
1053 
1054 	/* No entry in the map, do nothing */
1055 	if (eb_is_empty(&hdrs_map.map))
1056 		return;
1057 
1058 	/* No conversion fo the request headers */
1059 	if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1060 		return;
1061 
1062 	/* No conversion fo the response headers */
1063 	if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1064 		return;
1065 
1066 	node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1067 	if (!node)
1068 		return;
1069 	entry = container_of(node, struct h1_hdr_entry, node);
1070 	name->ptr = entry->name.ptr;
1071 	name->len = entry->name.len;
1072 }
1073 
1074 /* Append the description of what is present in error snapshot <es> into <out>.
1075  * The description must be small enough to always fit in a buffer. The output
1076  * buffer may be the trash so the trash must not be used inside this function.
1077  */
h1_show_error_snapshot(struct buffer * out,const struct error_snapshot * es)1078 static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1079 {
1080 	chunk_appendf(out,
1081 		      "  H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1082 		      "  H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1083 		      "  H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1084 		      es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1085 		      h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1086 		      es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
1087 }
1088 /*
1089  * Capture a bad request or response and archive it in the proxy's structure.
1090  * By default it tries to report the error position as h1m->err_pos. However if
1091  * this one is not set, it will then report h1m->next, which is the last known
1092  * parsing point. The function is able to deal with wrapping buffers. It always
1093  * displays buffers as a contiguous area starting at buf->p. The direction is
1094  * determined thanks to the h1m's flags.
1095  */
h1_capture_bad_message(struct h1c * h1c,struct h1s * h1s,struct h1m * h1m,struct buffer * buf)1096 static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1097 				   struct h1m *h1m, struct buffer *buf)
1098 {
1099 	struct session *sess = h1s->sess;
1100 	struct proxy *proxy = h1c->px;
1101 	struct proxy *other_end;
1102 	union error_snapshot_ctx ctx;
1103 
1104 	if (h1s->cs && h1s->cs->data) {
1105 		if (sess == NULL)
1106 			sess = si_strm(h1s->cs->data)->sess;
1107 		if (!(h1m->flags & H1_MF_RESP))
1108 			other_end = si_strm(h1s->cs->data)->be;
1109 		else
1110 			other_end = sess->fe;
1111 	} else
1112 		other_end = NULL;
1113 
1114 	/* http-specific part now */
1115 	ctx.h1.state   = h1m->state;
1116 	ctx.h1.c_flags = h1c->flags;
1117 	ctx.h1.s_flags = h1s->flags;
1118 	ctx.h1.m_flags = h1m->flags;
1119 	ctx.h1.m_clen  = h1m->curr_len;
1120 	ctx.h1.m_blen  = h1m->body_len;
1121 
1122 	proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1123 			    h1c->conn->target, sess, buf, 0, 0,
1124 			    (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1125 			    &ctx, h1_show_error_snapshot);
1126 }
1127 
1128 /* Emit the chunksize followed by a CRLF in front of data of the buffer
1129  * <buf>. It goes backwards and starts with the byte before the buffer's
1130  * head. The caller is responsible for ensuring there is enough room left before
1131  * the buffer's head for the string.
1132  */
h1_emit_chunk_size(struct buffer * buf,size_t chksz)1133 static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1134 {
1135 	char *beg, *end;
1136 
1137 	beg = end = b_head(buf);
1138 	*--beg = '\n';
1139 	*--beg = '\r';
1140 	do {
1141 		*--beg = hextab[chksz & 0xF];
1142 	} while (chksz >>= 4);
1143 	buf->head -= (end - beg);
1144 	b_add(buf, end - beg);
1145 }
1146 
1147 /* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1148  * ensuring there is enough room left in the buffer for the string. */
h1_emit_chunk_crlf(struct buffer * buf)1149 static void h1_emit_chunk_crlf(struct buffer *buf)
1150 {
1151 	*(b_peek(buf, b_data(buf)))     = '\r';
1152 	*(b_peek(buf, b_data(buf) + 1)) = '\n';
1153 	b_add(buf, 2);
1154 }
1155 
1156 /*
1157  * Switch the request to tunnel mode. This function must only be called for
1158  * CONNECT requests. On the client side, if the response is not finished, the
1159  * mux is mark as busy on input.
1160  */
h1_set_req_tunnel_mode(struct h1s * h1s)1161 static void h1_set_req_tunnel_mode(struct h1s *h1s)
1162 {
1163 	h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1164 	h1s->req.state = H1_MSG_TUNNEL;
1165 	TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1166 
1167 	if (!conn_is_back(h1s->h1c->conn)) {
1168 		h1s->flags &= ~H1S_F_PARSING_DONE;
1169 		if (h1s->res.state < H1_MSG_DONE) {
1170 			h1s->h1c->flags |= H1C_F_IN_BUSY;
1171 			TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1172 		}
1173 	}
1174 	else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1175 		h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1176 		tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1177 		TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1178 	}
1179 }
1180 
1181 /*
1182  * Switch the response to tunnel mode. This function must only be called on
1183  * successful replies to CONNECT requests or on protocol switching. In this
1184  * last case, this function takes care to switch the request to tunnel mode if
1185  * possible. On the server side, if the request is not finished, the mux is mark
1186  * as busy on input.
1187  */
h1_set_res_tunnel_mode(struct h1s * h1s)1188 static void h1_set_res_tunnel_mode(struct h1s *h1s)
1189 {
1190 
1191 	h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1192 	h1s->res.state = H1_MSG_TUNNEL;
1193 	TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1194 
1195 	if (conn_is_back(h1s->h1c->conn)) {
1196 		h1s->flags &= ~H1S_F_PARSING_DONE;
1197 		/* On protocol switching, switch the request to tunnel mode if it is in
1198 		 * DONE state. Otherwise we will wait the end of the request to switch
1199 		 * it in tunnel mode.
1200 		 */
1201 		if (h1s->req.state < H1_MSG_DONE) {
1202 			h1s->h1c->flags |= H1C_F_IN_BUSY;
1203 			TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1204 		}
1205 		else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1206 			h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1207 			h1s->req.state = H1_MSG_TUNNEL;
1208 			TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1209 		}
1210 	}
1211 	else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1212 		h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1213 		tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1214 		TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1215 	}
1216 }
1217 
1218 /*
1219  * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
1220  * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1221  * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1222  * the function http_parse_msg_hdrs() to do the parsing.
1223  */
h1_process_headers(struct h1s * h1s,struct h1m * h1m,struct htx * htx,struct buffer * buf,size_t * ofs,size_t max)1224 static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1225 				 struct buffer *buf, size_t *ofs, size_t max)
1226 {
1227 	union h1_sl h1sl;
1228 	int ret = 0;
1229 
1230 	TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1231 
1232 	if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
1233 	    !(h1s->flags & H1S_F_NOT_FIRST) &&                 /* It is the first transaction */
1234 	    !(h1m->flags & H1_MF_RESP)) {                      /* It is a request */
1235 		/* Try to match H2 preface before parsing the request headers. */
1236 		ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1237 		if (ret > 0) {
1238 			goto h2c_upgrade;
1239 		}
1240 	}
1241 	else {
1242 		if (h1s->meth == HTTP_METH_CONNECT)
1243 			h1m->flags |= H1_MF_METH_CONNECT;
1244 		if (h1s->meth == HTTP_METH_HEAD)
1245 			h1m->flags |= H1_MF_METH_HEAD;
1246 	}
1247 
1248 	ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1249 	if (ret <= 0) {
1250 		TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1251 		if (ret == -1) {
1252 			if (!(h1m->flags & H1_MF_RESP)) {
1253 				h1s->flags |= H1S_F_REQ_ERROR;
1254 				TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1255 			}
1256 			else {
1257 				h1s->flags |= H1S_F_RES_ERROR;
1258 				TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1259 			}
1260 			h1s->cs->flags |= CS_FL_EOI;
1261 			TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1262 			h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1263 		}
1264 		else if (ret == -2) {
1265 			TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_BLK, h1s->h1c->conn, h1s);
1266 			h1s->flags |= H1S_F_RX_CONGESTED;
1267 		}
1268 		ret = 0;
1269 		goto end;
1270 	}
1271 
1272 	if (h1m->err_pos >= 0)  {
1273 		/* Maybe we found an error during the parsing while we were
1274 		 * configured not to block on that, so we have to capture it
1275 		 * now.
1276 		 */
1277 		TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1278 		h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1279 	}
1280 
1281 	if (!(h1m->flags & H1_MF_RESP)) {
1282 		h1s->meth = h1sl.rq.meth;
1283 		if (h1m->state == H1_MSG_TUNNEL)
1284 			h1_set_req_tunnel_mode(h1s);
1285 	}
1286 	else {
1287 		h1s->status = h1sl.st.status;
1288 		if (h1m->state == H1_MSG_TUNNEL)
1289 			h1_set_res_tunnel_mode(h1s);
1290 	}
1291 	h1_process_input_conn_mode(h1s, h1m, htx);
1292 	*ofs += ret;
1293 
1294   end:
1295 	TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
1296 	return ret;
1297 
1298   h2c_upgrade:
1299 	h1s->h1c->flags |= H1C_F_UPG_H2C;
1300 	h1s->cs->flags |= CS_FL_EOI;
1301 	htx->flags |= HTX_FL_UPGRADE;
1302 	TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1303 	return 0;
1304 }
1305 
1306 /*
1307  * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1308  * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1309  * If relies on the function http_parse_msg_data() to do the parsing.
1310  */
h1_process_data(struct h1s * h1s,struct h1m * h1m,struct htx ** htx,struct buffer * buf,size_t * ofs,size_t max,struct buffer * htxbuf)1311 static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1312 			      struct buffer *buf, size_t *ofs, size_t max,
1313 			      struct buffer *htxbuf)
1314 {
1315 	int ret;
1316 
1317 	TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){max});
1318 	ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
1319 	if (!ret) {
1320 		TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
1321 		if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
1322 			if (!(h1m->flags & H1_MF_RESP)) {
1323 				h1s->flags |= H1S_F_REQ_ERROR;
1324 				TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1325 			}
1326 			else {
1327 				h1s->flags |= H1S_F_RES_ERROR;
1328 				TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1329 			}
1330 			h1s->cs->flags |= CS_FL_EOI;
1331 			TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1332 			h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1333 		}
1334 		goto end;
1335 	}
1336 
1337 	if (h1s->cs && !(h1m->flags & H1_MF_CHNK) &&
1338 	    ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1339 		TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
1340 		h1s->cs->flags |= CS_FL_MAY_SPLICE;
1341 	}
1342 	else if (h1s->cs) {
1343 		TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
1344 		h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1345 	}
1346 
1347 	*ofs += ret;
1348 
1349   end:
1350 	if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1351 		TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_BLK, h1s->h1c->conn, h1s);
1352 		h1s->flags |= H1S_F_RX_CONGESTED;
1353 	}
1354 
1355 	TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){ret});
1356 	return ret;
1357 }
1358 
1359 /*
1360  * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1361  * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1362  * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1363  * responsible to update the parser state <h1m>. If more room is requested,
1364  * H1S_F_RX_CONGESTED flag is set.
1365  */
h1_process_trailers(struct h1s * h1s,struct h1m * h1m,struct htx * htx,struct buffer * buf,size_t * ofs,size_t max)1366 static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1367 				  struct buffer *buf, size_t *ofs, size_t max)
1368 {
1369 	int ret;
1370 
1371 	TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1372 	ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
1373 	if (ret <= 0) {
1374 		TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
1375 		if (ret == -1) {
1376 			if (!(h1m->flags & H1_MF_RESP)) {
1377 				h1s->flags |= H1S_F_REQ_ERROR;
1378 				TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1379 			}
1380 			else {
1381 				h1s->flags |= H1S_F_RES_ERROR;
1382 				TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1383 			}
1384 			h1s->cs->flags |= CS_FL_EOI;
1385 			TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1386 			h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1387 		}
1388 		else if (ret == -2) {
1389 			TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_BLK, h1s->h1c->conn, h1s);
1390 			h1s->flags |= H1S_F_RX_CONGESTED;
1391 		}
1392 		ret = 0;
1393 		goto end;
1394 	}
1395 
1396 	*ofs += ret;
1397 
1398   end:
1399 	TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
1400 	return ret;
1401 }
1402 
1403 /*
1404  * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
1405  * proceed. This functions is responsible to update the parser state <h1m>. It
1406  * also add the flag CS_FL_EOI on the CS.
1407  */
h1_process_eom(struct h1s * h1s,struct h1m * h1m,struct htx * htx,struct buffer * buf,size_t * ofs,size_t max)1408 static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1409 			     struct buffer *buf, size_t *ofs, size_t max)
1410 {
1411 	int ret;
1412 
1413 	TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){max});
1414 	ret = h1_parse_msg_eom(h1m, htx, max);
1415 	if (!ret) {
1416 		TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1417 		if (htx->flags & HTX_FL_PARSING_ERROR) {
1418 			if (!(h1m->flags & H1_MF_RESP)) {
1419 				h1s->flags |= H1S_F_REQ_ERROR;
1420 				TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1421 			}
1422 			else {
1423 				h1s->flags |= H1S_F_RES_ERROR;
1424 				TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1425 			}
1426 			h1s->cs->flags |= CS_FL_EOI;
1427 			TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1428 			h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1429 		}
1430 		goto end;
1431 	}
1432 
1433 	h1s->flags |= H1S_F_PARSING_DONE;
1434 	/* Set EOI on conn-stream in DONE state iff:
1435 	 *  - it is a response
1436 	 *  - it is a request but no a protocol upgrade nor a CONNECT
1437 	 *
1438 	 * If not set, Wait the response to do so or not depending on the status
1439          * code.
1440          */
1441 	if ((h1m->flags & H1_MF_RESP) || ((h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
1442 		h1s->cs->flags |= CS_FL_EOI;
1443   end:
1444 	TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){ret});
1445 	return ret;
1446 }
1447 
1448 /*
1449  * Process incoming data. It parses data and transfer them from h1c->ibuf into
1450  * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1451  * it couldn't proceed.
1452  *
1453  * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
1454  */
h1_process_input(struct h1c * h1c,struct buffer * buf,size_t count)1455 static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
1456 {
1457 	struct h1s *h1s = h1c->h1s;
1458 	struct h1m *h1m;
1459 	struct htx *htx;
1460 	size_t ret, data;
1461 	size_t total = 0;
1462 	int errflag;
1463 
1464 	htx = htx_from_buf(buf);
1465 	TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
1466 
1467 	if (!conn_is_back(h1c->conn)) {
1468 		h1m = &h1s->req;
1469 		errflag = H1S_F_REQ_ERROR;
1470 	}
1471 	else {
1472 		h1m = &h1s->res;
1473 		errflag = H1S_F_RES_ERROR;
1474 	}
1475 
1476 	data = htx->data;
1477 	if (h1s->flags & errflag)
1478 		goto end;
1479 
1480 	if (h1c->flags & H1C_F_IN_BUSY)
1481 		goto end;
1482 
1483 	/* Always remove congestion flags and try to process more input data */
1484 	h1s->flags &= ~H1S_F_RX_CONGESTED;
1485 
1486 	do {
1487 		size_t used = htx_used_space(htx);
1488 
1489 		if (h1m->state <= H1_MSG_LAST_LF) {
1490 			TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
1491 			ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1492 			if (!ret)
1493 				break;
1494 
1495 			TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1496 				   H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1497 
1498 			if ((h1m->flags & H1_MF_RESP) &&
1499 			    h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1500 				h1m_init_res(&h1s->res);
1501 				h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1502 				TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
1503 			}
1504 		}
1505 		else if (h1m->state < H1_MSG_TRAILERS) {
1506 			TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1507 			ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
1508 			if (!ret && h1m->state != H1_MSG_DONE)
1509 				break;
1510 
1511 			TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1512 				    H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
1513 		}
1514 		else if (h1m->state == H1_MSG_TRAILERS) {
1515 			TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1516 			ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1517 			if (!ret && h1m->state != H1_MSG_DONE)
1518 				break;
1519 
1520 			TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1521 				    H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
1522 		}
1523 		else if (h1m->state == H1_MSG_DONE) {
1524 			if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1525 				if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1526 					break;
1527 
1528 				TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1529 					   H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1530 			}
1531 
1532 			if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1533 				h1_set_req_tunnel_mode(h1s);
1534 			else if ((h1m->flags & H1_MF_RESP) && h1s->req.state == H1_MSG_TUNNEL) {
1535 				TRACE_STATE("switch back H1 request from tunnel mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1536 				h1s->req.state = H1_MSG_DONE;
1537 			}
1538 			else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1539 				h1c->flags |= H1C_F_IN_BUSY;
1540 				TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1541 				break;
1542 			}
1543 			else
1544 				break;
1545 		}
1546 		else if (h1m->state == H1_MSG_TUNNEL) {
1547 			TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
1548 			ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
1549 			if (!ret)
1550 				break;
1551 
1552 			TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1553 				    H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
1554 		}
1555 		else {
1556 			h1s->flags |= errflag;
1557 			break;
1558 		}
1559 
1560 		count -= htx_used_space(htx) - used;
1561 	} while (!(h1s->flags & (errflag|H1S_F_RX_CONGESTED)));
1562 
1563 	if (h1s->flags & errflag) {
1564 		TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
1565 		goto parsing_err;
1566 	}
1567 
1568 	b_del(&h1c->ibuf, total);
1569 
1570   end:
1571 	htx_to_buf(htx, buf);
1572 	ret = htx->data - data;
1573 	if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
1574 		h1c->flags &= ~H1C_F_IN_FULL;
1575 		TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE);
1576 		h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1577 	}
1578 
1579 	h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1580 
1581 	if (!b_data(&h1c->ibuf))
1582 		h1_release_buf(h1c, &h1c->ibuf);
1583 
1584 	/* When Input data are pending for this message, notify upper layer that
1585 	 * the mux need more space in the HTX buffer to continue if :
1586 	 *
1587 	 *   - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1588 	 *   - Headers or trailers are pending to be copied.
1589 	 */
1590 	if (h1s->flags & (H1S_F_RX_CONGESTED)) {
1591 		h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
1592 		TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1593 	}
1594 	else if (h1s->flags & H1S_F_REOS) {
1595 		h1s->cs->flags |= CS_FL_EOS;
1596 		if (h1m->state >= H1_MSG_DONE)
1597 			h1s->cs->flags |= CS_FL_EOI;
1598 		else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
1599 			h1s->cs->flags |= CS_FL_ERROR;
1600 	}
1601 
1602 	TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1603 	return ret;
1604 
1605   parsing_err:
1606 	b_reset(&h1c->ibuf);
1607 	htx_to_buf(htx, buf);
1608 	TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
1609 	return 0;
1610 }
1611 
1612 /*
1613  * Process outgoing data. It parses data and transfer them from the channel buffer into
1614  * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1615  * 0 if it couldn't proceed.
1616  */
h1_process_output(struct h1c * h1c,struct buffer * buf,size_t count)1617 static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1618 {
1619 	struct h1s *h1s = h1c->h1s;
1620 	struct h1m *h1m;
1621 	struct htx *chn_htx = NULL;
1622 	struct htx_blk *blk;
1623 	struct buffer tmp;
1624 	size_t total = 0;
1625 	int errflag;
1626 
1627 	if (!count)
1628 		goto end;
1629 
1630 	chn_htx = htxbuf(buf);
1631 	TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1632 
1633 	if (htx_is_empty(chn_htx))
1634 		goto end;
1635 
1636 	if (!h1_get_buf(h1c, &h1c->obuf)) {
1637 		h1c->flags |= H1C_F_OUT_ALLOC;
1638 		TRACE_STATE("waiting for h1c obuf allocation", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1639 		goto end;
1640 	}
1641 
1642 	if (!conn_is_back(h1c->conn)) {
1643 		h1m = &h1s->res;
1644 		errflag = H1S_F_RES_ERROR;
1645 	}
1646 	else {
1647 		h1m = &h1s->req;
1648 		errflag = H1S_F_REQ_ERROR;
1649 	}
1650 
1651 	if (h1s->flags & errflag)
1652 		goto end;
1653 
1654 	/* the htx is non-empty thus has at least one block */
1655 	blk = htx_get_head_blk(chn_htx);
1656 
1657 	/* Perform some optimizations to reduce the number of buffer copies.
1658 	 * First, if the mux's buffer is empty and the htx area contains
1659 	 * exactly one data block of the same size as the requested count,
1660 	 * then it's possible to simply swap the caller's buffer with the
1661 	 * mux's output buffer and adjust offsets and length to match the
1662 	 * entire DATA HTX block in the middle. In this case we perform a
1663 	 * true zero-copy operation from end-to-end. This is the situation
1664 	 * that happens all the time with large files. Second, if this is not
1665 	 * possible, but the mux's output buffer is empty, we still have an
1666 	 * opportunity to avoid the copy to the intermediary buffer, by making
1667 	 * the intermediary buffer's area point to the output buffer's area.
1668 	 * In this case we want to skip the HTX header to make sure that copies
1669 	 * remain aligned and that this operation remains possible all the
1670 	 * time. This goes for headers, data blocks and any data extracted from
1671 	 * the HTX blocks.
1672 	 */
1673 	if (!b_data(&h1c->obuf)) {
1674 		if (htx_nbblks(chn_htx) == 1 &&
1675 		    htx_get_blk_type(blk) == HTX_BLK_DATA &&
1676 		    htx_get_blk_value(chn_htx, blk).len == count) {
1677 			void *old_area = h1c->obuf.area;
1678 
1679 			TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
1680 			h1c->obuf.area = buf->area;
1681 			h1c->obuf.head = sizeof(struct htx) + blk->addr;
1682 			h1c->obuf.data = count;
1683 
1684 			buf->area = old_area;
1685 			buf->data = buf->head = 0;
1686 
1687 			chn_htx = (struct htx *)buf->area;
1688 			htx_reset(chn_htx);
1689 
1690 			/* The message is chunked. We need to emit the chunk
1691 			 * size. We have at least the size of the struct htx to
1692 			 * write the chunk envelope. It should be enough.
1693 			 */
1694 			if (h1m->flags & H1_MF_CHNK) {
1695 				h1_emit_chunk_size(&h1c->obuf, count);
1696 				h1_emit_chunk_crlf(&h1c->obuf);
1697 			}
1698 
1699 			total += count;
1700 			if (h1m->state == H1_MSG_DATA)
1701 				TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
1702 					    H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
1703 			else
1704 				TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
1705 					    H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
1706 			goto out;
1707 		}
1708 		tmp.area = h1c->obuf.area + h1c->obuf.head;
1709 	}
1710 	else
1711 		tmp.area = trash.area;
1712 
1713 	tmp.data = 0;
1714 	tmp.size = b_room(&h1c->obuf);
1715 	while (count && !(h1s->flags & errflag) && blk) {
1716 		struct htx_sl *sl;
1717 		struct ist n, v;
1718 		enum htx_blk_type type = htx_get_blk_type(blk);
1719 		uint32_t sz = htx_get_blksz(blk);
1720 		uint32_t vlen, chklen;
1721 
1722 		vlen = sz;
1723 		if (type != HTX_BLK_DATA && vlen > count)
1724 			goto full;
1725 
1726 		if (type == HTX_BLK_UNUSED)
1727 			goto nextblk;
1728 
1729 		switch (h1m->state) {
1730 			case H1_MSG_RQBEFORE:
1731 				if (type != HTX_BLK_REQ_SL)
1732 					goto error;
1733 				TRACE_USER("sending request headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx);
1734 				sl = htx_get_blk_ptr(chn_htx, blk);
1735 				h1s->meth = sl->info.req.meth;
1736 				h1_parse_req_vsn(h1m, sl);
1737 				if (!h1_format_htx_reqline(sl, &tmp))
1738 					goto full;
1739 				h1m->flags |= H1_MF_XFER_LEN;
1740 				if (sl->flags & HTX_SL_F_BODYLESS)
1741 					h1m->flags |= H1_MF_CLEN;
1742 				h1m->state = H1_MSG_HDR_FIRST;
1743 				break;
1744 
1745 			case H1_MSG_RPBEFORE:
1746 				if (type != HTX_BLK_RES_SL)
1747 					goto error;
1748 				TRACE_USER("sending response headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx);
1749 				sl = htx_get_blk_ptr(chn_htx, blk);
1750 				h1s->status = sl->info.res.status;
1751 				h1_parse_res_vsn(h1m, sl);
1752 				if (!h1_format_htx_stline(sl, &tmp))
1753 					goto full;
1754 				if (sl->flags & HTX_SL_F_XFER_LEN)
1755 					h1m->flags |= H1_MF_XFER_LEN;
1756 				if (sl->info.res.status < 200 &&
1757 				    (sl->info.res.status == 100 || sl->info.res.status >= 102))
1758 					h1s->flags |= H1S_F_HAVE_O_CONN;
1759 				h1m->state = H1_MSG_HDR_FIRST;
1760 				break;
1761 
1762 			case H1_MSG_HDR_FIRST:
1763 			case H1_MSG_HDR_NAME:
1764 			case H1_MSG_HDR_L2_LWS:
1765 				if (type == HTX_BLK_EOH)
1766 					goto last_lf;
1767 				if (type != HTX_BLK_HDR)
1768 					goto error;
1769 
1770 				h1m->state = H1_MSG_HDR_NAME;
1771 				n = htx_get_blk_name(chn_htx, blk);
1772 				v = htx_get_blk_value(chn_htx, blk);
1773 
1774 				/* Skip all pseudo-headers */
1775 				if (*(n.ptr) == ':')
1776 					goto skip_hdr;
1777 
1778 				if (isteq(n, ist("transfer-encoding")))
1779 					h1_parse_xfer_enc_header(h1m, v);
1780 				else if (isteq(n, ist("content-length"))) {
1781 					/* Only skip C-L header with invalid value. */
1782 					if (h1_parse_cont_len_header(h1m, &v) < 0)
1783 						goto skip_hdr;
1784 				}
1785 				else if (isteq(n, ist("connection"))) {
1786 					h1_parse_connection_header(h1m, &v);
1787 					if (!v.len)
1788 						goto skip_hdr;
1789 				}
1790 				else if (isteq(n, ist("te"))) {
1791 					/* "te" may only be sent with "trailers" if this value
1792 					 * is present, otherwise it must be deleted.
1793 					 */
1794 					v = istist(v, ist("trailers"));
1795 					if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
1796 						goto skip_hdr;
1797 					v = ist("trailers");
1798 				}
1799 
1800 				/* Skip header if same name is used to add the server name */
1801 				if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1802 				    isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1803 					goto skip_hdr;
1804 
1805 				/* Try to adjust the case of the header name */
1806 				if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1807 					h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1808 				if (!h1_format_htx_hdr(n, v, &tmp))
1809 					goto full;
1810 			  skip_hdr:
1811 				h1m->state = H1_MSG_HDR_L2_LWS;
1812 				break;
1813 
1814 			case H1_MSG_LAST_LF:
1815 				if (type != HTX_BLK_EOH)
1816 					goto error;
1817 			  last_lf:
1818 				h1m->state = H1_MSG_LAST_LF;
1819 				if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
1820 					/* If the reply comes from haproxy while the request is
1821 					 * not finished, we force the connection close. */
1822 					if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1823 						h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1824 						TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1825 					}
1826 
1827 					/* the conn_mode must be processed. So do it */
1828 					n = ist("connection");
1829 					v = ist("");
1830 					h1_process_output_conn_mode(h1s, h1m, &v);
1831 					if (v.len) {
1832 						/* Try to adjust the case of the header name */
1833 						if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1834 							h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1835 						if (!h1_format_htx_hdr(n, v, &tmp))
1836 							goto full;
1837 					}
1838 					h1s->flags |= H1S_F_HAVE_O_CONN;
1839 				}
1840 
1841 				if ((h1s->meth != HTTP_METH_CONNECT &&
1842 				     (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1843 				     (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1844 				    (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1845 				     h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1846 				     (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1847 				     (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
1848 					/* chunking needed but header not seen */
1849 					n = ist("transfer-encoding");
1850 					v = ist("chunked");
1851 					if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1852 						h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1853 					if (!h1_format_htx_hdr(n, v, &tmp))
1854 						goto full;
1855 					TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1856 					h1m->flags |= H1_MF_CHNK;
1857 				}
1858 
1859 				/* Now add the server name to a header (if requested) */
1860 				if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1861 				    !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1862 					struct server *srv = objt_server(h1c->conn->target);
1863 
1864 					if (srv) {
1865 						n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1866 						v = ist(srv->id);
1867 
1868 						/* Try to adjust the case of the header name */
1869 						if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1870 							h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1871 						if (!h1_format_htx_hdr(n, v, &tmp))
1872 							goto full;
1873 					}
1874 					TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1875 					h1s->flags |= H1S_F_HAVE_SRV_NAME;
1876 				}
1877 
1878 				if (!chunk_memcat(&tmp, "\r\n", 2))
1879 					goto full;
1880 
1881 				TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1882 					    H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1883 
1884 				if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1885 					/* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1886 					h1_set_req_tunnel_mode(h1s);
1887 				}
1888 				else if ((h1m->flags & H1_MF_RESP) &&
1889 					 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
1890 					/* a successful reply to a CONNECT or a protocol switching is sent
1891 					 * to the client. Switch the response to tunnel mode.
1892 					 */
1893 					h1_set_res_tunnel_mode(h1s);
1894 					TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1895 				}
1896 				else if ((h1m->flags & H1_MF_RESP) &&
1897 					 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1898 					h1m_init_res(&h1s->res);
1899 					h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1900 					h1s->flags &= ~H1S_F_HAVE_O_CONN;
1901 					TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1902 				}
1903 				else if ((h1m->flags & H1_MF_RESP) &&  h1s->meth == HTTP_METH_HEAD) {
1904 					h1m->state = H1_MSG_DONE;
1905 					TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1906 				}
1907 				else
1908 					h1m->state = H1_MSG_DATA;
1909 				break;
1910 
1911 			case H1_MSG_DATA:
1912 			case H1_MSG_TUNNEL:
1913 				if (type == HTX_BLK_EOM) {
1914 					/* Chunked message without explicit trailers */
1915 					if (h1m->flags & H1_MF_CHNK) {
1916 						if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
1917 							goto full;
1918 					}
1919 					goto done;
1920 				}
1921 				else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
1922 					/* If the message is not chunked, never
1923 					 * add the last chunk. */
1924 					if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
1925 						goto full;
1926 					TRACE_PROTO("sending message trailers", H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s, chn_htx);
1927 					goto trailers;
1928 				}
1929 				else if (type != HTX_BLK_DATA)
1930 					goto error;
1931 
1932 				TRACE_PROTO("sending message data", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){sz});
1933 
1934 
1935 				if (vlen > count) {
1936 					/* Get the maximum amount of data we can xferred */
1937 					vlen = count;
1938 				}
1939 
1940 				chklen = 0;
1941 				if (h1m->flags & H1_MF_CHNK) {
1942 					chklen = b_room(&tmp);
1943 					chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1944 						  (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1945 						  (chklen < 1048576) ? 5 : 8);
1946 					chklen += 4; /* 2 x CRLF */
1947 				}
1948 
1949 				if (vlen + chklen > b_room(&tmp)) {
1950 					/* too large for the buffer */
1951 					if (chklen >= b_room(&tmp))
1952 						goto full;
1953 					vlen = b_room(&tmp) - chklen;
1954 				}
1955 				v = htx_get_blk_value(chn_htx, blk);
1956 				v.len = vlen;
1957 				if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
1958 					goto full;
1959 
1960 				if (h1m->state == H1_MSG_DATA)
1961 					TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
1962 						    H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
1963 				else
1964 					TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
1965 						    H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
1966 				break;
1967 
1968 			case H1_MSG_TRAILERS:
1969 				if (type == HTX_BLK_EOM)
1970 					goto done;
1971 				else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
1972 					goto error;
1973 			  trailers:
1974 				h1m->state = H1_MSG_TRAILERS;
1975 				/* If the message is not chunked, ignore
1976 				 * trailers. It may happen with H2 messages. */
1977 				if (!(h1m->flags & H1_MF_CHNK))
1978 					break;
1979 
1980 				if (type == HTX_BLK_EOT) {
1981 					if (!chunk_memcat(&tmp, "\r\n", 2))
1982 						goto full;
1983 					TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1984 						    H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
1985 				}
1986 				else { // HTX_BLK_TLR
1987 					n = htx_get_blk_name(chn_htx, blk);
1988 					v = htx_get_blk_value(chn_htx, blk);
1989 
1990 					/* Try to adjust the case of the header name */
1991 					if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1992 						h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1993 					if (!h1_format_htx_hdr(n, v, &tmp))
1994 						goto full;
1995 				}
1996 				break;
1997 
1998 			case H1_MSG_DONE:
1999 				if (type != HTX_BLK_EOM)
2000 					goto error;
2001 			  done:
2002 				h1m->state = H1_MSG_DONE;
2003 				if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
2004 					h1_set_req_tunnel_mode(h1s);
2005 					TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2006 				}
2007 				else if ((h1m->flags & H1_MF_RESP) && h1s->req.state == H1_MSG_TUNNEL) {
2008 					TRACE_STATE("switch back H1 request from tunnel mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
2009 					h1s->req.state = H1_MSG_DONE;
2010 					h1s->flags |= H1S_F_PARSING_DONE;
2011 				}
2012 
2013 				if (h1s->h1c->flags & H1C_F_IN_BUSY) {
2014 					h1s->h1c->flags &= ~H1C_F_IN_BUSY;
2015 					h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2016 					TRACE_STATE("h1c no more busy", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
2017 				}
2018 
2019 				TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2020 					   H1_EV_TX_DATA, h1c->conn, h1s);
2021 				break;
2022 
2023 			default:
2024 			  error:
2025 				TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
2026 				/* Unexpected error during output processing */
2027 				chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
2028 				h1s->flags |= errflag;
2029 				h1c->flags |= H1C_F_CS_ERROR;
2030 				TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2031 				TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
2032 				break;
2033 		}
2034 
2035 	  nextblk:
2036 		total += vlen;
2037 		count -= vlen;
2038 		if (sz == vlen)
2039 			blk = htx_remove_blk(chn_htx, blk);
2040 		else {
2041 			htx_cut_data_blk(chn_htx, blk, vlen);
2042 			break;
2043 		}
2044 	}
2045 
2046   copy:
2047 	/* when the output buffer is empty, tmp shares the same area so that we
2048 	 * only have to update pointers and lengths.
2049 	 */
2050 	if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2051 		h1c->obuf.data = tmp.data;
2052 	else
2053 		b_putblk(&h1c->obuf, tmp.area, tmp.data);
2054 
2055 	htx_to_buf(chn_htx, buf);
2056   out:
2057 	/* Both the request and the response reached the DONE state. So set EOI
2058 	 * flag on the conn-stream. Most of time, the flag will already be set,
2059 	 * except for protocol upgrades.
2060 	 */
2061 	if (h1s->cs && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2062 			h1s->cs->flags |= CS_FL_EOI;
2063 
2064 	if (!buf_room_for_htx_data(&h1c->obuf)) {
2065 		TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2066 		h1c->flags |= H1C_F_OUT_FULL;
2067 	}
2068   end:
2069 	TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
2070 	return total;
2071 
2072   full:
2073 	TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2074 	h1c->flags |= H1C_F_OUT_FULL;
2075 	goto copy;
2076 }
2077 
2078 /*********************************************************/
2079 /* functions below are I/O callbacks from the connection */
2080 /*********************************************************/
h1_wake_stream_for_recv(struct h1s * h1s)2081 static void h1_wake_stream_for_recv(struct h1s *h1s)
2082 {
2083 	if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
2084 		TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2085 		tasklet_wakeup(h1s->subs->tasklet);
2086 		h1s->subs->events &= ~SUB_RETRY_RECV;
2087 		if (!h1s->subs->events)
2088 			h1s->subs = NULL;
2089 	}
2090 }
h1_wake_stream_for_send(struct h1s * h1s)2091 static void h1_wake_stream_for_send(struct h1s *h1s)
2092 {
2093 	if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
2094 		TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2095 		tasklet_wakeup(h1s->subs->tasklet);
2096 		h1s->subs->events &= ~SUB_RETRY_SEND;
2097 		if (!h1s->subs->events)
2098 			h1s->subs = NULL;
2099 	}
2100 }
2101 
2102 /*
2103  * Attempt to read data, and subscribe if none available
2104  */
h1_recv(struct h1c * h1c)2105 static int h1_recv(struct h1c *h1c)
2106 {
2107 	struct connection *conn = h1c->conn;
2108 	struct h1s *h1s = h1c->h1s;
2109 	size_t ret = 0, max;
2110 	int rcvd = 0;
2111 	int flags = 0;
2112 
2113 	TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2114 
2115 	if (h1c->wait_event.events & SUB_RETRY_RECV) {
2116 		TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
2117 		return (b_data(&h1c->ibuf));
2118 	}
2119 
2120 	if (!h1_recv_allowed(h1c)) {
2121 		TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
2122 		rcvd = 1;
2123 		goto end;
2124 	}
2125 
2126 	if (!h1_get_buf(h1c, &h1c->ibuf)) {
2127 		h1c->flags |= H1C_F_IN_ALLOC;
2128 		TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
2129 		goto end;
2130 	}
2131 
2132 	if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
2133 		if (!h1s_data_pending(h1s))
2134 			h1_wake_stream_for_recv(h1s);
2135 		rcvd = 1;
2136 		TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
2137 		goto end;
2138 	}
2139 
2140 	/*
2141 	 * If we only have a small amount of data, realign it,
2142 	 * it's probably cheaper than doing 2 recv() calls.
2143 	 */
2144 	if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2145 		b_slow_realign(&h1c->ibuf, trash.area, 0);
2146 
2147 	/* avoid useless reads after first responses */
2148 	if (h1s && ((!conn_is_back(conn) && h1s->req.state == H1_MSG_RQBEFORE) ||
2149 		    (conn_is_back(conn) && h1s->res.state == H1_MSG_RPBEFORE)))
2150 		flags |= CO_RFL_READ_ONCE;
2151 
2152 	max = buf_room_for_htx_data(&h1c->ibuf);
2153 	if (max) {
2154 		if (h1c->flags & H1C_F_IN_FULL) {
2155 			h1c->flags &= ~H1C_F_IN_FULL;
2156 			TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2157 		}
2158 
2159 		b_realign_if_empty(&h1c->ibuf);
2160 		if (!b_data(&h1c->ibuf)) {
2161 			/* try to pre-align the buffer like the rxbufs will be
2162 			 * to optimize memory copies.
2163 			 */
2164 			h1c->ibuf.head  = sizeof(struct htx);
2165 		}
2166 		ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
2167 	}
2168 	if (ret > 0) {
2169 		TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn,,, (size_t[]){ret});
2170 		rcvd = 1;
2171 		if (h1s && h1s->cs) {
2172 			h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
2173 			if (h1s->csinfo.t_idle == -1)
2174 				h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2175 		}
2176 	}
2177 
2178 	if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
2179 		rcvd = 1;
2180 		goto end;
2181 	}
2182 
2183 	TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2184 	conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2185 
2186   end:
2187 	if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2188 		h1_wake_stream_for_recv(h1s);
2189 
2190 	if (conn_xprt_read0_pending(conn) && h1s) {
2191 		h1s->flags |= H1S_F_REOS;
2192 		TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2193 		rcvd = 1;
2194 	}
2195 
2196 	if (!b_data(&h1c->ibuf))
2197 		h1_release_buf(h1c, &h1c->ibuf);
2198 	else if (!buf_room_for_htx_data(&h1c->ibuf)) {
2199 		h1c->flags |= H1C_F_IN_FULL;
2200 		TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2201 	}
2202 
2203 	TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
2204 	return rcvd;
2205 }
2206 
2207 
2208 /*
2209  * Try to send data if possible
2210  */
h1_send(struct h1c * h1c)2211 static int h1_send(struct h1c *h1c)
2212 {
2213 	struct connection *conn = h1c->conn;
2214 	unsigned int flags = 0;
2215 	size_t ret;
2216 	int sent = 0;
2217 
2218 	TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2219 
2220 	if (conn->flags & CO_FL_ERROR) {
2221 		TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
2222 		return 0;
2223 	}
2224 
2225 	if (!b_data(&h1c->obuf))
2226 		goto end;
2227 
2228 	if (h1c->flags & H1C_F_CO_MSG_MORE)
2229 		flags |= CO_SFL_MSG_MORE;
2230 	if (h1c->flags & H1C_F_CO_STREAMER)
2231 		flags |= CO_SFL_STREAMER;
2232 
2233 	ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
2234 	if (ret > 0) {
2235 		TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn,,, (size_t[]){ret});
2236 		if (h1c->flags & H1C_F_OUT_FULL) {
2237 			h1c->flags &= ~H1C_F_OUT_FULL;
2238 			TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2239 		}
2240 		b_del(&h1c->obuf, ret);
2241 		sent = 1;
2242 	}
2243 
2244 	if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
2245 		TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
2246 		/* error or output closed, nothing to send, clear the buffer to release it */
2247 		b_reset(&h1c->obuf);
2248 	}
2249 
2250   end:
2251 	if (!(h1c->flags & H1C_F_OUT_FULL))
2252 		h1_wake_stream_for_send(h1c->h1s);
2253 
2254 	/* We're done, no more to send */
2255 	if (!b_data(&h1c->obuf)) {
2256 		TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
2257 		h1_release_buf(h1c, &h1c->obuf);
2258 		if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2259 			TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
2260 			h1_shutw_conn(conn);
2261 		}
2262 	}
2263 	else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2264 		TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
2265 		conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
2266 	}
2267 
2268 	TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
2269 	return sent;
2270 }
2271 
2272 
2273 /* callback called on any event by the connection handler.
2274  * It applies changes and returns zero, or < 0 if it wants immediate
2275  * destruction of the connection.
2276  */
h1_process(struct h1c * h1c)2277 static int h1_process(struct h1c * h1c)
2278 {
2279 	struct connection *conn = h1c->conn;
2280 	struct h1s *h1s = h1c->h1s;
2281 
2282 	TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2283 
2284 	if (!conn->ctx)
2285 		return -1;
2286 
2287 	if (!h1s) {
2288 		if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
2289 		    conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2290 			goto release;
2291 		if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
2292 			TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
2293 			if (!h1s_create(h1c, NULL, NULL))
2294 				goto release;
2295 		}
2296 		else if (conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE) && b_data(&h1c->ibuf))
2297 			goto release;
2298 		else
2299 			goto end;
2300 		h1s = h1c->h1s;
2301 	}
2302 
2303 	if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2304 		h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2305 
2306 	if (conn_xprt_read0_pending(conn)) {
2307 		h1s->flags |= H1S_F_REOS;
2308 		TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2309 	}
2310 
2311 	if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
2312 	    (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
2313 	    conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
2314 		if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
2315 			h1s->cs->flags |= CS_FL_ERROR;
2316 		TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
2317 		h1s->cs->data_cb->wake(h1s->cs);
2318 	}
2319   end:
2320 	h1_refresh_timeout(h1c);
2321 	TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
2322 	return 0;
2323 
2324   release:
2325 	h1_release(h1c);
2326 	TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2327 	return -1;
2328 }
2329 
h1_io_cb(struct task * t,void * ctx,unsigned short status)2330 struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2331 {
2332 	struct connection *conn;
2333 	struct tasklet *tl = (struct tasklet *)t;
2334 	int conn_in_list;
2335 	struct h1c *h1c;
2336 	int ret = 0;
2337 
2338 
2339 	HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2340 	if (tl->context == NULL) {
2341 		/* The connection has been taken over by another thread,
2342 		 * we're no longer responsible for it, so just free the
2343 		 * tasklet, and do nothing.
2344 		 */
2345 		HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2346 		tasklet_free(tl);
2347 		return NULL;
2348 	}
2349 	h1c = ctx;
2350 	conn = h1c->conn;
2351 
2352 	TRACE_POINT(H1_EV_H1C_WAKE, conn);
2353 
2354 	/* Remove the connection from the list, to be sure nobody attempts
2355 	 * to use it while we handle the I/O events
2356 	 */
2357 	conn_in_list = conn->flags & CO_FL_LIST_MASK;
2358 	if (conn_in_list)
2359 		MT_LIST_DEL(&conn->list);
2360 
2361 	HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2362 
2363 	if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2364 		ret = h1_send(h1c);
2365 	if (!(h1c->wait_event.events & SUB_RETRY_RECV))
2366 		ret |= h1_recv(h1c);
2367 	if (ret || !h1c->h1s)
2368 		ret = h1_process(h1c);
2369 	/* If we were in an idle list, we want to add it back into it,
2370 	 * unless h1_process() returned -1, which mean it has destroyed
2371 	 * the connection (testing !ret is enough, if h1_process() wasn't
2372 	 * called then ret will be 0 anyway.
2373 	 */
2374 	if (!ret && conn_in_list) {
2375 		struct server *srv = objt_server(conn->target);
2376 
2377 		if (conn_in_list == CO_FL_SAFE_LIST)
2378 			MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
2379 		else
2380 			MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
2381 	}
2382 	return NULL;
2383 }
2384 
h1_reset(struct connection * conn)2385 static void h1_reset(struct connection *conn)
2386 {
2387 
2388 }
2389 
h1_wake(struct connection * conn)2390 static int h1_wake(struct connection *conn)
2391 {
2392 	struct h1c *h1c = conn->ctx;
2393 	int ret;
2394 
2395 	TRACE_POINT(H1_EV_H1C_WAKE, conn);
2396 
2397 	h1_send(h1c);
2398 	ret = h1_process(h1c);
2399 	if (ret == 0) {
2400 		struct h1s *h1s = h1c->h1s;
2401 
2402 		if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2403 			TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
2404 			ret = h1s->cs->data_cb->wake(h1s->cs);
2405 		}
2406 	}
2407 	return ret;
2408 }
2409 
2410 /* Connection timeout management. The principle is that if there's no receipt
2411  * nor sending for a certain amount of time, the connection is closed.
2412  */
h1_timeout_task(struct task * t,void * context,unsigned short state)2413 static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2414 {
2415 	struct h1c *h1c = context;
2416 	int expired = tick_is_expired(t->expire, now_ms);
2417 
2418 	TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2419 
2420 	if (h1c) {
2421 		if (!expired) {
2422 			TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
2423 			return t;
2424 		}
2425 
2426 		/* We're about to destroy the connection, so make sure nobody attempts
2427 		 * to steal it from us.
2428 		 */
2429 		HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2430 
2431 		/* Somebody already stole the connection from us, so we should not
2432 		 * free it, we just have to free the task.
2433 		 */
2434 		if (!t->context)
2435 			h1c = NULL;
2436 		else if (h1c->conn->flags & CO_FL_LIST_MASK)
2437 			MT_LIST_DEL(&h1c->conn->list);
2438 
2439 		HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2440 	}
2441 
2442 	task_destroy(t);
2443 
2444 	if (!h1c) {
2445 		/* resources were already deleted */
2446 		TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
2447 		return NULL;
2448 	}
2449 
2450 	h1c->task = NULL;
2451 	/* If a stream is still attached to the mux, just set an error and wait
2452 	 * for the stream's timeout. Otherwise, release the mux. This is only ok
2453 	 * because same timeouts are used.
2454 	 */
2455 	if (h1c->h1s && h1c->h1s->cs) {
2456 		h1c->flags |= H1C_F_CS_ERROR;
2457 		TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2458 	}
2459 	else
2460 		h1_release(h1c);
2461 
2462 	return NULL;
2463 }
2464 
2465 /*******************************************/
2466 /* functions below are used by the streams */
2467 /*******************************************/
2468 
2469 /*
2470  * Attach a new stream to a connection
2471  * (Used for outgoing connections)
2472  */
h1_attach(struct connection * conn,struct session * sess)2473 static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
2474 {
2475 	struct h1c *h1c = conn->ctx;
2476 	struct conn_stream *cs = NULL;
2477 	struct h1s *h1s;
2478 
2479 	TRACE_ENTER(H1_EV_STRM_NEW, conn);
2480 	if (h1c->flags & H1C_F_CS_ERROR) {
2481 		TRACE_DEVEL("leaving on h1c error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
2482 		goto end;
2483 	}
2484 
2485 	cs = cs_new(h1c->conn);
2486 	if (!cs) {
2487 		TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
2488 		goto end;
2489 	}
2490 
2491 	h1s = h1s_create(h1c, cs, sess);
2492 	if (h1s == NULL) {
2493 		TRACE_DEVEL("leaving on h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
2494 		goto end;
2495 	}
2496 
2497 	TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
2498 	return cs;
2499   end:
2500 	cs_free(cs);
2501 	return NULL;
2502 }
2503 
2504 /* Retrieves a valid conn_stream from this connection, or returns NULL. For
2505  * this mux, it's easy as we can only store a single conn_stream.
2506  */
h1_get_first_cs(const struct connection * conn)2507 static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2508 {
2509 	struct h1c *h1c = conn->ctx;
2510 	struct h1s *h1s = h1c->h1s;
2511 
2512 	if (h1s)
2513 		return h1s->cs;
2514 
2515 	return NULL;
2516 }
2517 
h1_destroy(void * ctx)2518 static void h1_destroy(void *ctx)
2519 {
2520 	struct h1c *h1c = ctx;
2521 
2522 	TRACE_POINT(H1_EV_H1C_END, h1c->conn);
2523 	if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
2524 		h1_release(h1c);
2525 }
2526 
2527 /*
2528  * Detach the stream from the connection and possibly release the connection.
2529  */
h1_detach(struct conn_stream * cs)2530 static void h1_detach(struct conn_stream *cs)
2531 {
2532 	struct h1s *h1s = cs->ctx;
2533 	struct h1c *h1c;
2534 	struct session *sess;
2535 	int is_not_first;
2536 
2537 	TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2538 
2539 	cs->ctx = NULL;
2540 	if (!h1s) {
2541 		TRACE_LEAVE(H1_EV_STRM_END);
2542 		return;
2543 	}
2544 
2545 	sess = h1s->sess;
2546 	h1c = h1s->h1c;
2547 	h1s->cs = NULL;
2548 
2549 	is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2550 	h1s_destroy(h1s);
2551 
2552 	if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
2553 		/* If there are any excess server data in the input buffer,
2554 		 * release it and close the connection ASAP (some data may
2555 		 * remain in the output buffer). This happens if a server sends
2556 		 * invalid responses. So in such case, we don't want to reuse
2557 		 * the connection
2558 		 */
2559 		if (b_data(&h1c->ibuf)) {
2560 			h1_release_buf(h1c, &h1c->ibuf);
2561 			h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
2562 			TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
2563 			goto release;
2564 		}
2565 
2566 		/* Never ever allow to reuse a connection from a non-reuse backend */
2567 		if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2568 			h1c->conn->flags |= CO_FL_PRIVATE;
2569 
2570 		if (!(h1c->conn->owner) && (h1c->conn->flags & CO_FL_PRIVATE)) {
2571 			h1c->conn->owner = sess;
2572 			if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2573 				h1c->conn->owner = NULL;
2574 				h1c->conn->mux->destroy(h1c);
2575 				goto end;
2576 			}
2577 			if (session_check_idle_conn(sess, h1c->conn)) {
2578 				/* The connection got destroyed, let's leave */
2579 				TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2580 				goto end;
2581 			}
2582 		}
2583 		if (!(h1c->conn->flags & CO_FL_PRIVATE)) {
2584 			if (h1c->conn->owner == sess)
2585 				h1c->conn->owner = NULL;
2586 			h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2587 			if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
2588 				/* The server doesn't want it, let's kill the connection right away */
2589 				h1c->conn->mux->destroy(h1c);
2590 				TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2591 				goto end;
2592 			}
2593 			/* At this point, the connection has been added to the
2594 			 * server idle list, so another thread may already have
2595 			 * hijacked it, so we can't do anything with it.
2596 			 */
2597 			return;
2598 		}
2599 	}
2600 
2601   release:
2602 	/* We don't want to close right now unless the connection is in error or shut down for writes */
2603 	if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2604 	    (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2605 	    ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2606 	    !h1c->conn->owner) {
2607 		TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
2608 		h1_release(h1c);
2609 	}
2610 	else {
2611 		/* If we have a new request, process it immediately */
2612 		if (unlikely(b_data(&h1c->ibuf))) {
2613 			if (h1_process(h1c) == -1)
2614 				goto end;
2615 		}
2616 		else
2617 			h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2618 		h1_refresh_timeout(h1c);
2619 	}
2620   end:
2621 	TRACE_LEAVE(H1_EV_STRM_END);
2622 }
2623 
2624 
h1_shutr(struct conn_stream * cs,enum cs_shr_mode mode)2625 static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2626 {
2627 	struct h1s *h1s = cs->ctx;
2628 	struct h1c *h1c;
2629 
2630 	if (!h1s)
2631 		return;
2632 	h1c = h1s->h1c;
2633 
2634 	TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2635 
2636 	if (cs->flags & CS_FL_KILL_CONN) {
2637 		TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2638 		goto do_shutr;
2639 	}
2640 	if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2641 		TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2642 		goto do_shutr;
2643 	}
2644 
2645 	if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2646 		TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2647 		goto end;
2648 	}
2649 
2650   do_shutr:
2651 	/* NOTE: Be sure to handle abort (cf. h2_shutr) */
2652 	if (cs->flags & CS_FL_SHR)
2653 		goto end;
2654 	if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2655 		cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2656 				      (mode == CS_SHR_DRAIN));
2657   end:
2658 	TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
2659 }
2660 
h1_shutw(struct conn_stream * cs,enum cs_shw_mode mode)2661 static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2662 {
2663 	struct h1s *h1s = cs->ctx;
2664 	struct h1c *h1c;
2665 
2666 	if (!h1s)
2667 		return;
2668 	h1c = h1s->h1c;
2669 
2670 	TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2671 
2672 	if (cs->flags & CS_FL_KILL_CONN) {
2673 		TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2674 		goto do_shutw;
2675 	}
2676 	if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2677 		TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2678 		goto do_shutw;
2679 	}
2680 
2681 	if ((h1c->flags & H1C_F_UPG_H2C) ||
2682 	    ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2683 		TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2684 		goto end;
2685 	}
2686 
2687   do_shutw:
2688 	h1c->flags |= H1C_F_CS_SHUTW_NOW;
2689 	if (mode != CS_SHW_NORMAL)
2690 		h1c->flags |= H1C_F_ST_SILENT_SHUT;
2691 	if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2692 		goto end;
2693 	h1_shutw_conn(cs->conn);
2694   end:
2695 	TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
2696 }
2697 
h1_shutw_conn(struct connection * conn)2698 static void h1_shutw_conn(struct connection *conn)
2699 {
2700 	struct h1c *h1c = conn->ctx;
2701 
2702 	if (conn->flags & CO_FL_SOCK_WR_SH)
2703 		return;
2704 
2705 	TRACE_ENTER(H1_EV_H1C_END, conn);
2706 	conn_xprt_shutw(conn);
2707 	conn_sock_shutw(conn, (h1c && !(h1c->flags & H1C_F_ST_SILENT_SHUT)));
2708 	h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
2709 	TRACE_LEAVE(H1_EV_STRM_SHUT, conn);
2710 }
2711 
2712 /* Called from the upper layer, to unsubscribe <es> from events <event_type>
2713  * The <es> pointer is not allowed to differ from the one passed to the
2714  * subscribe() call. It always returns zero.
2715  */
h1_unsubscribe(struct conn_stream * cs,int event_type,struct wait_event * es)2716 static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
2717 {
2718 	struct h1s *h1s = cs->ctx;
2719 
2720 	if (!h1s)
2721 		return 0;
2722 
2723 	BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2724 	BUG_ON(h1s->subs && h1s->subs != es);
2725 
2726 	es->events &= ~event_type;
2727 	if (!es->events)
2728 		h1s->subs = NULL;
2729 
2730 	if (event_type & SUB_RETRY_RECV)
2731 		TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
2732 
2733 	if (event_type & SUB_RETRY_SEND)
2734 		TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
2735 
2736 	return 0;
2737 }
2738 
2739 /* Called from the upper layer, to subscribe <es> to events <event_type>. The
2740  * event subscriber <es> is not allowed to change from a previous call as long
2741  * as at least one event is still subscribed. The <event_type> must only be a
2742  * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2743  * the conn_stream <cs> was already detached, in which case it will return -1.
2744  */
h1_subscribe(struct conn_stream * cs,int event_type,struct wait_event * es)2745 static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
2746 {
2747 	struct h1s *h1s = cs->ctx;
2748 	struct h1c *h1c;
2749 
2750 	if (!h1s)
2751 		return -1;
2752 
2753 	BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2754 	BUG_ON(h1s->subs && h1s->subs != es);
2755 
2756 	es->events |= event_type;
2757 	h1s->subs = es;
2758 
2759 	if (event_type & SUB_RETRY_RECV)
2760 		TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
2761 
2762 
2763 	if (event_type & SUB_RETRY_SEND) {
2764 		TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
2765 		/*
2766 		 * If the conn_stream attempt to subscribe, and the
2767 		 * mux isn't subscribed to the connection, then it
2768 		 * probably means the connection wasn't established
2769 		 * yet, so we have to subscribe.
2770 		 */
2771 		h1c = h1s->h1c;
2772 		if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2773 			h1c->conn->xprt->subscribe(h1c->conn,
2774 						   h1c->conn->xprt_ctx,
2775 						   SUB_RETRY_SEND,
2776 						   &h1c->wait_event);
2777 	}
2778 	return 0;
2779 }
2780 
2781 /* Called from the upper layer, to receive data */
h1_rcv_buf(struct conn_stream * cs,struct buffer * buf,size_t count,int flags)2782 static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2783 {
2784 	struct h1s *h1s = cs->ctx;
2785 	struct h1c *h1c = h1s->h1c;
2786 	struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2787 	size_t ret = 0;
2788 
2789 	TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){count});
2790 	if (!(h1c->flags & H1C_F_IN_ALLOC))
2791 		ret = h1_process_input(h1c, buf, count);
2792 	else
2793 		TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
2794 
2795 	if (flags & CO_RFL_BUF_FLUSH) {
2796 		if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
2797 			h1s->flags |= H1S_F_BUF_FLUSH;
2798 			TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2799 		}
2800 	}
2801 	else {
2802 		if (ret && h1s->flags & H1S_F_SPLICED_DATA) {
2803 			h1s->flags &= ~H1S_F_SPLICED_DATA;
2804 			TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2805 		}
2806 		if (((flags & CO_RFL_KEEP_RECV) || (h1m->state != H1_MSG_DONE)) && !(h1c->wait_event.events & SUB_RETRY_RECV))
2807 			h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2808 	}
2809 	TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){ret});
2810 	return ret;
2811 }
2812 
2813 
2814 /* Called from the upper layer, to send data */
h1_snd_buf(struct conn_stream * cs,struct buffer * buf,size_t count,int flags)2815 static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2816 {
2817 	struct h1s *h1s = cs->ctx;
2818 	struct h1c *h1c;
2819 	size_t total = 0;
2820 
2821 	if (!h1s)
2822 		return 0;
2823 	h1c = h1s->h1c;
2824 
2825 	TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){count});
2826 
2827 	/* If we're not connected yet, or we're waiting for a handshake, stop
2828 	 * now, as we don't want to remove everything from the channel buffer
2829 	 * before we're sure we can send it.
2830 	 */
2831 	if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
2832 		TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
2833 		return 0;
2834 	}
2835 
2836 	if (h1c->flags & H1C_F_CS_ERROR) {
2837 		cs->flags |= CS_FL_ERROR;
2838 		TRACE_DEVEL("H1 connection is in error, leaving in error", H1_EV_STRM_SEND|H1_EV_H1C_ERR|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
2839 		return 0;
2840 	}
2841 
2842 	/* Inherit some flags from the upper layer */
2843 	h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
2844 	if (flags & CO_SFL_MSG_MORE)
2845 		h1c->flags |= H1C_F_CO_MSG_MORE;
2846 	if (flags & CO_SFL_STREAMER)
2847 		h1c->flags |= H1C_F_CO_STREAMER;
2848 
2849 	while (count) {
2850 		size_t ret = 0;
2851 
2852 		if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2853 			ret = h1_process_output(h1c, buf, count);
2854 		else
2855 			TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
2856 
2857 		if ((count - ret) > 0)
2858 			h1c->flags |= H1C_F_CO_MSG_MORE;
2859 
2860 		if (!ret)
2861 			break;
2862 		total += ret;
2863 		count -= ret;
2864 		if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
2865 			break;
2866 	}
2867 
2868 	if (h1c->flags & H1C_F_CS_ERROR) {
2869 		TRACE_DEVEL("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
2870 		cs->flags |= CS_FL_ERROR;
2871 	}
2872 
2873 	h1_refresh_timeout(h1c);
2874 	TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){total});
2875 	return total;
2876 }
2877 
2878 #if defined(USE_LINUX_SPLICE)
2879 /* Send and get, using splicing */
h1_rcv_pipe(struct conn_stream * cs,struct pipe * pipe,unsigned int count)2880 static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2881 {
2882 	struct h1s *h1s = cs->ctx;
2883 	struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2884 	int ret = 0;
2885 
2886 	TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){count});
2887 
2888 	if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
2889 		h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2890 		TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2891 		if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2892 			TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
2893 			cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
2894 		}
2895 		goto end;
2896 	}
2897 
2898 	if (h1s_data_pending(h1s)) {
2899 		h1s->flags |= H1S_F_BUF_FLUSH;
2900 		TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
2901 		goto end;
2902 	}
2903 
2904 	if (!(h1s->flags & H1S_F_SPLICED_DATA)) {
2905 		h1s->flags &= ~H1S_F_BUF_FLUSH;
2906 		h1s->flags |= H1S_F_SPLICED_DATA;
2907 		TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
2908 	}
2909 
2910 	if (!h1_recv_allowed(h1s->h1c)) {
2911 		TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
2912 		goto end;
2913 	}
2914 
2915 	if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2916 		count = h1m->curr_len;
2917 	ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
2918 	if (h1m->state == H1_MSG_DATA && ret >= 0) {
2919 		h1m->curr_len -= ret;
2920 		if (!h1m->curr_len) {
2921 			h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2922 			TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2923 		}
2924 	}
2925 
2926   end:
2927 	if (conn_xprt_read0_pending(cs->conn)) {
2928 		h1s->flags |= H1S_F_REOS;
2929 		h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2930 		TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
2931 	}
2932 
2933 	if ((h1s->flags & H1S_F_REOS) ||
2934 	    (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
2935 	    (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
2936 		TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
2937 		cs->flags &= ~CS_FL_MAY_SPLICE;
2938 	}
2939 
2940 	TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){ret});
2941 	return ret;
2942 }
2943 
h1_snd_pipe(struct conn_stream * cs,struct pipe * pipe)2944 static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2945 {
2946 	struct h1s *h1s = cs->ctx;
2947 	int ret = 0;
2948 
2949 	TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){pipe->data});
2950 
2951 	if (b_data(&h1s->h1c->obuf))
2952 		goto end;
2953 
2954 	ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
2955   end:
2956 	if (pipe->data) {
2957 		if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2958 			TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
2959 			cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
2960 		}
2961 	}
2962 
2963 	TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){ret});
2964 	return ret;
2965 }
2966 #endif
2967 
h1_ctl(struct connection * conn,enum mux_ctl_type mux_ctl,void * output)2968 static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2969 {
2970 	int ret = 0;
2971 	switch (mux_ctl) {
2972 	case MUX_STATUS:
2973 		if (!(conn->flags & CO_FL_WAIT_XPRT))
2974 			ret |= MUX_STATUS_READY;
2975 		return ret;
2976 	default:
2977 		return -1;
2978 	}
2979 }
2980 
2981 /* for debugging with CLI's "show fd" command */
h1_show_fd(struct buffer * msg,struct connection * conn)2982 static int h1_show_fd(struct buffer *msg, struct connection *conn)
2983 {
2984 	struct h1c *h1c = conn->ctx;
2985 	struct h1s *h1s = h1c->h1s;
2986 	int ret = 0;
2987 
2988 	chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2989 		      h1c->flags,  h1c->wait_event.events,
2990 		      (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2991 		      (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2992 		       (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2993 		      (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2994 
2995 	if (h1s) {
2996 		char *method;
2997 
2998 		if (h1s->meth < HTTP_METH_OTHER)
2999 			method = http_known_methods[h1s->meth].ptr;
3000 		else
3001 			method = "UNKNOWN";
3002 		chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3003 		    " .meth=%s status=%d",
3004 			      h1s, h1s->flags,
3005 			      h1m_state_str(h1s->req.state),
3006 			      h1m_state_str(h1s->res.state), method, h1s->status);
3007 		if (h1s->cs)
3008 			chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3009 				      h1s->cs->flags, h1s->cs->data);
3010 
3011 		chunk_appendf(&trash, " .subs=%p", h1s->subs);
3012 		if (h1s->subs) {
3013 			chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3014 			chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3015 				      h1s->subs->tasklet->calls,
3016 				      h1s->subs->tasklet->context);
3017 			if (h1s->subs->tasklet->calls >= 1000000)
3018 				ret = 1;
3019 			resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3020 			chunk_appendf(&trash, ")");
3021 		}
3022 	}
3023 	return ret;
3024 }
3025 
3026 
3027 /* Add an entry in the headers map. Returns -1 on error and 0 on success. */
add_hdr_case_adjust(const char * from,const char * to,char ** err)3028 static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3029 {
3030 	struct h1_hdr_entry *entry;
3031 
3032 	/* Be sure there is a non-empty <to> */
3033 	if (!strlen(to)) {
3034 		memprintf(err, "expect <to>");
3035 		return -1;
3036 	}
3037 
3038 	/* Be sure only the case differs between <from> and <to> */
3039 	if (strcasecmp(from, to)) {
3040 		memprintf(err, "<from> and <to> must not differ execpt the case");
3041 		return -1;
3042 	}
3043 
3044 	/* Be sure <from> does not already existsin the tree */
3045 	if (ebis_lookup(&hdrs_map.map, from)) {
3046 		memprintf(err, "duplicate entry '%s'", from);
3047 		return -1;
3048 	}
3049 
3050 	/* Create the entry and insert it in the tree */
3051 	entry = malloc(sizeof(*entry));
3052 	if (!entry) {
3053 		memprintf(err, "out of memory");
3054 		return -1;
3055 	}
3056 
3057 	entry->node.key = strdup(from);
3058 	entry->name.ptr = strdup(to);
3059 	entry->name.len = strlen(to);
3060 	if (!entry->node.key || !entry->name.ptr) {
3061 		free(entry->node.key);
3062 		istfree(&entry->name);
3063 		free(entry);
3064 		memprintf(err, "out of memory");
3065 		return -1;
3066 	}
3067 	ebis_insert(&hdrs_map.map, &entry->node);
3068 	return 0;
3069 }
3070 
3071 /* Migrate the the connection to the current thread.
3072  * Return 0 if successful, non-zero otherwise.
3073  * Expected to be called with the old thread lock held.
3074  */
h1_takeover(struct connection * conn,int orig_tid)3075 static int h1_takeover(struct connection *conn, int orig_tid)
3076 {
3077 	struct h1c *h1c = conn->ctx;
3078 	struct task *task;
3079 
3080 	if (fd_takeover(conn->handle.fd, conn) != 0)
3081 		return -1;
3082 
3083 	if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3084 		/* We failed to takeover the xprt, even if the connection may
3085 		 * still be valid, flag it as error'd, as we have already
3086 		 * taken over the fd, and wake the tasklet, so that it will
3087 		 * destroy it.
3088 		 */
3089 		conn->flags |= CO_FL_ERROR;
3090 		tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3091 		return -1;
3092 	}
3093 
3094 	if (h1c->wait_event.events)
3095 		h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3096 		    h1c->wait_event.events, &h1c->wait_event);
3097 	/* To let the tasklet know it should free itself, and do nothing else,
3098 	 * set its context to NULL.
3099 	 */
3100 	h1c->wait_event.tasklet->context = NULL;
3101 	tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3102 
3103 	task = h1c->task;
3104 	if (task) {
3105 		task->context = NULL;
3106 		h1c->task = NULL;
3107 		__ha_barrier_store();
3108 		task_kill(task);
3109 
3110 		h1c->task = task_new(tid_bit);
3111 		if (!h1c->task) {
3112 			h1_release(h1c);
3113 			return -1;
3114 		}
3115 		h1c->task->process = h1_timeout_task;
3116 		h1c->task->context = h1c;
3117 	}
3118 	h1c->wait_event.tasklet = tasklet_new();
3119 	if (!h1c->wait_event.tasklet) {
3120 		h1_release(h1c);
3121 		return -1;
3122 	}
3123 	h1c->wait_event.tasklet->process = h1_io_cb;
3124 	h1c->wait_event.tasklet->context = h1c;
3125 	h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3126 		                   SUB_RETRY_RECV, &h1c->wait_event);
3127 
3128 	return 0;
3129 }
3130 
3131 
h1_hdeaders_case_adjust_deinit()3132 static void h1_hdeaders_case_adjust_deinit()
3133 {
3134 	struct ebpt_node *node, *next;
3135 	struct h1_hdr_entry *entry;
3136 
3137 	node = ebpt_first(&hdrs_map.map);
3138 	while (node) {
3139 		next = ebpt_next(node);
3140 		ebpt_delete(node);
3141 		entry = container_of(node, struct h1_hdr_entry, node);
3142 		free(entry->node.key);
3143 		istfree(&entry->name);
3144 		free(entry);
3145 		node = next;
3146 	}
3147 	free(hdrs_map.name);
3148 }
3149 
cfg_h1_headers_case_adjust_postparser()3150 static int cfg_h1_headers_case_adjust_postparser()
3151 {
3152 	FILE *file = NULL;
3153 	char *c, *key_beg, *key_end, *value_beg, *value_end;
3154 	char *err;
3155 	int rc, line = 0, err_code = 0;
3156 
3157 	if (!hdrs_map.name)
3158 		goto end;
3159 
3160 	file = fopen(hdrs_map.name, "r");
3161 	if (!file) {
3162 		ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3163 			 hdrs_map.name);
3164                 err_code |= ERR_ALERT | ERR_FATAL;
3165 		goto end;
3166 	}
3167 
3168 	/* now parse all lines. The file may contain only two header name per
3169 	 * line, separated by spaces. All heading and trailing spaces will be
3170 	 * ignored. Lines starting with a # are ignored.
3171 	 */
3172 	while (fgets(trash.area, trash.size, file) != NULL) {
3173 		line++;
3174 		c = trash.area;
3175 
3176 		/* strip leading spaces and tabs */
3177 		while (*c == ' ' || *c == '\t')
3178 			c++;
3179 
3180 		/* ignore emptu lines, or lines beginning with a dash */
3181 		if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3182 			continue;
3183 
3184 		/* look for the end of the key */
3185 		key_beg = c;
3186 		while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3187 			c++;
3188 		key_end = c;
3189 
3190 		/* strip middle spaces and tabs */
3191 		while (*c == ' ' || *c == '\t')
3192 			c++;
3193 
3194 		/* look for the end of the value, it is the end of the line */
3195 		value_beg = c;
3196 		while (*c && *c != '\n' && *c != '\r')
3197 			c++;
3198 		value_end = c;
3199 
3200 		/* trim possibly trailing spaces and tabs */
3201 		while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3202 			value_end--;
3203 
3204 		/* set final \0 and check entries */
3205 		*key_end = '\0';
3206 		*value_end = '\0';
3207 
3208 		err = NULL;
3209 		rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3210 		if (rc < 0) {
3211 			ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3212 				 hdrs_map.name, err, line);
3213 			err_code |= ERR_ALERT | ERR_FATAL;
3214 			free(err);
3215 			goto end;
3216 		}
3217 		if (rc > 0) {
3218 			ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3219 				   hdrs_map.name, err, line);
3220 			err_code |= ERR_WARN;
3221 			free(err);
3222 		}
3223 	}
3224 
3225   end:
3226 	if (file)
3227 		fclose(file);
3228 	hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3229 	return err_code;
3230 }
3231 
3232 
3233 /* config parser for global "h1-outgoing-header-case-adjust" */
cfg_parse_h1_header_case_adjust(char ** args,int section_type,struct proxy * curpx,struct proxy * defpx,const char * file,int line,char ** err)3234 static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3235 					   struct proxy *defpx, const char *file, int line,
3236 					   char **err)
3237 {
3238         if (too_many_args(2, args, err, NULL))
3239                 return -1;
3240         if (!*(args[1]) || !*(args[2])) {
3241                 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3242 		return -1;
3243 	}
3244 	return add_hdr_case_adjust(args[1], args[2], err);
3245 }
3246 
3247 /* config parser for global "h1-outgoing-headers-case-adjust-file" */
cfg_parse_h1_headers_case_adjust_file(char ** args,int section_type,struct proxy * curpx,struct proxy * defpx,const char * file,int line,char ** err)3248 static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3249 						 struct proxy *defpx, const char *file, int line,
3250 						 char **err)
3251 {
3252         if (too_many_args(1, args, err, NULL))
3253                 return -1;
3254         if (!*(args[1])) {
3255                 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3256 		return -1;
3257 	}
3258 	free(hdrs_map.name);
3259 	hdrs_map.name = strdup(args[1]);
3260         return 0;
3261 }
3262 
3263 
3264 /* config keyword parsers */
3265 static struct cfg_kw_list cfg_kws = {{ }, {
3266 		{ CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3267 		{ CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3268 		{ 0, NULL, NULL },
3269 	}
3270 };
3271 
3272 INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3273 REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3274 
3275 
3276 /****************************************/
3277 /* MUX initialization and instantiation */
3278 /****************************************/
3279 
3280 /* The mux operations */
3281 static const struct mux_ops mux_h1_ops = {
3282 	.init        = h1_init,
3283 	.wake        = h1_wake,
3284 	.attach      = h1_attach,
3285 	.get_first_cs = h1_get_first_cs,
3286 	.get_cs_info = h1_get_cs_info,
3287 	.detach      = h1_detach,
3288 	.destroy     = h1_destroy,
3289 	.avail_streams = h1_avail_streams,
3290 	.used_streams = h1_used_streams,
3291 	.rcv_buf     = h1_rcv_buf,
3292 	.snd_buf     = h1_snd_buf,
3293 #if defined(USE_LINUX_SPLICE)
3294 	.rcv_pipe    = h1_rcv_pipe,
3295 	.snd_pipe    = h1_snd_pipe,
3296 #endif
3297 	.subscribe   = h1_subscribe,
3298 	.unsubscribe = h1_unsubscribe,
3299 	.shutr       = h1_shutr,
3300 	.shutw       = h1_shutw,
3301 	.show_fd     = h1_show_fd,
3302 	.reset       = h1_reset,
3303 	.ctl         = h1_ctl,
3304 	.takeover    = h1_takeover,
3305 	.flags       = MX_FL_HTX,
3306 	.name        = "H1",
3307 };
3308 
3309 
3310 /* this mux registers default HTX proto */
3311 static struct mux_proto_list mux_proto_htx =
3312 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
3313 
3314 INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3315 
3316 /*
3317  * Local variables:
3318  *  c-indent-level: 8
3319  *  c-basic-offset: 8
3320  * End:
3321  */
3322