blob: e68b50c927343f2a42421503da869843c5934924 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Christopher Faulet0ef372a2019-04-08 10:57:20 +020015#include <common/h2.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Christopher Faulet98fbe952019-07-22 16:18:24 +020019#include <ebistree.h>
20
Christopher Faulet1be55f92018-10-02 15:59:23 +020021#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020022#include <types/proxy.h>
23#include <types/session.h>
24
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/connection.h>
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020026#include <proto/h1_htx.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020027#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020028#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010029#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030#include <proto/stream.h>
31#include <proto/stream_interface.h>
Christopher Faulet6b81df72019-10-01 22:08:43 +020032#include <proto/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020033
34/*
35 * H1 Connection flags (32 bits)
36 */
37#define H1C_F_NONE 0x00000000
38
39/* Flags indicating why writing output data are blocked */
40#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
41#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
42/* 0x00000004 - 0x00000008 unused */
43
44/* Flags indicating why reading input data are blocked. */
45#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
46#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Faulet7b109f22019-12-05 11:18:31 +010047#define H1C_F_IN_BUSY 0x00000040 /* mux is blocked on input waiting the other side */
Christopher Faulet539e0292018-11-19 10:40:09 +010048/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
Christopher Faulet7b109f22019-12-05 11:18:31 +010050/* Flags indicating the connection state */
Christopher Faulet51dbc942018-09-13 09:05:15 +020051#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
52#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet7b109f22019-12-05 11:18:31 +010053#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +010054#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
55 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020056
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020058#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020059
Christopher Faulet51dbc942018-09-13 09:05:15 +020060/*
61 * H1 Stream flags (32 bits)
62 */
Christopher Faulet129817b2018-09-20 16:14:40 +020063#define H1S_F_NONE 0x00000000
64#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020065#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
66#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020067#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020068#define H1S_F_WANT_KAL 0x00000010
69#define H1S_F_WANT_TUN 0x00000020
70#define H1S_F_WANT_CLO 0x00000040
71#define H1S_F_WANT_MSK 0x00000070
72#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010073#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010074#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet76014fd2019-12-10 11:47:22 +010075#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
76/* 0x00000800 .. 0x00001000 unused */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020077#define H1S_F_HAVE_SRV_NAME 0x00002000 /* Set during output process if the server name header was added to the request */
Christopher Fauletada34b62019-05-24 16:36:43 +020078#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020079
80/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020081struct h1c {
82 struct connection *conn;
83 struct proxy *px;
84 uint32_t flags; /* Connection flags: H1C_F_* */
85
86 struct buffer ibuf; /* Input buffer to store data before parsing */
87 struct buffer obuf; /* Output buffer to store data after reformatting */
88
89 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
90 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
91
92 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010093 struct task *task; /* timeout management task */
94 int timeout; /* idle timeout duration in ticks */
95 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096};
97
98/* H1 stream descriptor */
99struct h1s {
100 struct h1c *h1c;
101 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100102 struct cs_info csinfo; /* CS info, only used for client connections */
103 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200104
Christopher Faulet51dbc942018-09-13 09:05:15 +0200105 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
106 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200107
Olivier Houchardf502aca2018-12-14 19:42:40 +0100108 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200109 struct h1m req;
110 struct h1m res;
111
112 enum http_meth_t meth; /* HTTP resquest method */
113 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200114};
115
Christopher Faulet98fbe952019-07-22 16:18:24 +0200116/* Map of headers used to convert outgoing headers */
117struct h1_hdrs_map {
118 char *name;
119 struct eb_root map;
120};
121
122/* An entry in a headers map */
123struct h1_hdr_entry {
124 struct ist name;
125 struct ebpt_node node;
126};
127
128/* Declare the headers map */
129static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
130
131
Christopher Faulet6b81df72019-10-01 22:08:43 +0200132/* trace source and events */
133static void h1_trace(enum trace_level level, uint64_t mask,
134 const struct trace_source *src,
135 const struct ist where, const struct ist func,
136 const void *a1, const void *a2, const void *a3, const void *a4);
137
138/* The event representation is split like this :
139 * h1c - internal H1 connection
140 * h1s - internal H1 stream
141 * strm - application layer
142 * rx - data receipt
143 * tx - data transmission
144 *
145 */
146static const struct trace_event h1_trace_events[] = {
147#define H1_EV_H1C_NEW (1ULL << 0)
148 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
149#define H1_EV_H1C_RECV (1ULL << 1)
150 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
151#define H1_EV_H1C_SEND (1ULL << 2)
152 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
153#define H1_EV_H1C_BLK (1ULL << 3)
154 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
155#define H1_EV_H1C_WAKE (1ULL << 4)
156 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
157#define H1_EV_H1C_END (1ULL << 5)
158 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
159#define H1_EV_H1C_ERR (1ULL << 6)
160 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
161
162#define H1_EV_RX_DATA (1ULL << 7)
163 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
164#define H1_EV_RX_EOI (1ULL << 8)
165 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
166#define H1_EV_RX_HDRS (1ULL << 9)
167 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
168#define H1_EV_RX_BODY (1ULL << 10)
169 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
170#define H1_EV_RX_TLRS (1ULL << 11)
171 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
172
173#define H1_EV_TX_DATA (1ULL << 12)
174 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
175#define H1_EV_TX_EOI (1ULL << 13)
176 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
177#define H1_EV_TX_HDRS (1ULL << 14)
178 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
179#define H1_EV_TX_BODY (1ULL << 15)
180 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
181#define H1_EV_TX_TLRS (1ULL << 16)
182 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
183
184#define H1_EV_H1S_NEW (1ULL << 17)
185 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
186#define H1_EV_H1S_BLK (1ULL << 18)
187 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
188#define H1_EV_H1S_END (1ULL << 19)
189 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
190#define H1_EV_H1S_ERR (1ULL << 20)
191 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
192
193#define H1_EV_STRM_NEW (1ULL << 21)
194 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
195#define H1_EV_STRM_RECV (1ULL << 22)
196 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
197#define H1_EV_STRM_SEND (1ULL << 23)
198 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
199#define H1_EV_STRM_WAKE (1ULL << 24)
200 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
201#define H1_EV_STRM_SHUT (1ULL << 25)
202 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
203#define H1_EV_STRM_END (1ULL << 26)
204 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
205#define H1_EV_STRM_ERR (1ULL << 27)
206 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
207
208 { }
209};
210
211static const struct name_desc h1_trace_lockon_args[4] = {
212 /* arg1 */ { /* already used by the connection */ },
213 /* arg2 */ { .name="h1s", .desc="H1 stream" },
214 /* arg3 */ { },
215 /* arg4 */ { }
216};
217
218static const struct name_desc h1_trace_decoding[] = {
219#define H1_VERB_CLEAN 1
220 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
221#define H1_VERB_MINIMAL 2
222 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
223#define H1_VERB_SIMPLE 3
224 { .name="simple", .desc="add request/response status line or htx info when available" },
225#define H1_VERB_ADVANCED 4
226 { .name="advanced", .desc="add header fields or frame decoding when available" },
227#define H1_VERB_COMPLETE 5
228 { .name="complete", .desc="add full data dump when available" },
229 { /* end */ }
230};
231
232static struct trace_source trace_h1 = {
233 .name = IST("h1"),
234 .desc = "HTTP/1 multiplexer",
235 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
236 .default_cb = h1_trace,
237 .known_events = h1_trace_events,
238 .lockon_args = h1_trace_lockon_args,
239 .decoding = h1_trace_decoding,
240 .report_events = ~0, // report everything by default
241};
242
243#define TRACE_SOURCE &trace_h1
244INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
245
Christopher Faulet51dbc942018-09-13 09:05:15 +0200246/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100247DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
248DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249
Christopher Faulet51dbc942018-09-13 09:05:15 +0200250static int h1_recv(struct h1c *h1c);
251static int h1_send(struct h1c *h1c);
252static int h1_process(struct h1c *h1c);
253static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100254static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100255static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200256static void h1_wake_stream_for_recv(struct h1s *h1s);
257static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200258
Christopher Faulet6b81df72019-10-01 22:08:43 +0200259/* the H1 traces always expect that arg1, if non-null, is of type connection
260 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
261 * that arg3, if non-null, is a htx for rx/tx headers.
262 */
263static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
264 const struct ist where, const struct ist func,
265 const void *a1, const void *a2, const void *a3, const void *a4)
266{
267 const struct connection *conn = a1;
268 const struct h1c *h1c = conn ? conn->ctx : NULL;
269 const struct h1s *h1s = a2;
270 const struct htx *htx = a3;
271 const size_t *val = a4;
272
273 if (!h1c)
274 h1c = (h1s ? h1s->h1c : NULL);
275
276 if (!h1c || src->verbosity < H1_VERB_CLEAN)
277 return;
278
279 /* Display frontend/backend info by default */
280 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
281
282 /* Display request and response states if h1s is defined */
283 if (h1s)
284 chunk_appendf(&trace_buf, " [%s, %s]",
285 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
286
287 if (src->verbosity == H1_VERB_CLEAN)
288 return;
289
290 /* Display the value to the 4th argument (level > STATE) */
291 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100292 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200293
294 /* Display status-line if possible (verbosity > MINIMAL) */
295 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
296 const struct htx_blk *blk = htx_get_head_blk(htx);
297 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
298 enum htx_blk_type type = htx_get_blk_type(blk);
299
300 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
301 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
302 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
303 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
304 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
305 }
306
307 /* Display h1c info and, if defined, h1s info (pointer + flags) */
308 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
309 if (h1s)
310 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
311
312 if (src->verbosity == H1_VERB_MINIMAL)
313 return;
314
315 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
316 if (src->level > TRACE_LEVEL_USER) {
317 if (src->verbosity == H1_VERB_COMPLETE ||
318 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
319 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
320 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
321 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
322 if (src->verbosity == H1_VERB_COMPLETE ||
323 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
324 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
325 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
326 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
327 }
328
329 /* Display htx info if defined (level > USER) */
330 if (src->level > TRACE_LEVEL_USER && htx) {
331 int full = 0;
332
333 /* Full htx info (level > STATE && verbosity > SIMPLE) */
334 if (src->level > TRACE_LEVEL_STATE) {
335 if (src->verbosity == H1_VERB_COMPLETE)
336 full = 1;
337 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
338 full = 1;
339 }
340
341 chunk_memcat(&trace_buf, "\n\t", 2);
342 htx_dump(&trace_buf, htx, full);
343 }
344}
345
346
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347/*****************************************************/
348/* functions below are for dynamic buffer management */
349/*****************************************************/
350/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100351 * Indicates whether or not we may receive data. The rules are the following :
352 * - if an error or a shutdown for reads was detected on the connection we
353 must not attempt to receive
354 * - if the input buffer failed to be allocated or is full , we must not try
355 * to receive
356 * - if he input processing is busy waiting for the output side, we may
357 * attemp to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200358 * - otherwise must may not attempt to receive
359 */
360static inline int h1_recv_allowed(const struct h1c *h1c)
361{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100362 if (h1c->flags & H1C_F_CS_ERROR) {
363 TRACE_DEVEL("recv not allowed because of error on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200364 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200365 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100367 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200368 TRACE_DEVEL("recv not allowed because of (error|read0) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletcf56b992018-12-11 16:12:31 +0100369 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200370 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100371
Christopher Fauletcb55f482018-12-10 11:56:47 +0100372 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200373 return 1;
374
Christopher Faulet6b81df72019-10-01 22:08:43 +0200375 TRACE_DEVEL("recv not allowed because input is blocked", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376 return 0;
377}
378
379/*
380 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
381 * flags are used to figure what buffer was requested. It returns 1 if the
382 * allocation succeeds, in which case the connection is woken up, or 0 if it's
383 * impossible to wake up and we prefer to be woken up later.
384 */
385static int h1_buf_available(void *target)
386{
387 struct h1c *h1c = target;
388
389 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200390 TRACE_STATE("unblocking h1c, ibuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200391 h1c->flags &= ~H1C_F_IN_ALLOC;
392 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200393 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200394 return 1;
395 }
396
397 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200398 TRACE_STATE("unblocking h1s, obuf allocated", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200399 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200400 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200401 if (h1c->h1s)
402 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200403 return 1;
404 }
405
Christopher Faulet51dbc942018-09-13 09:05:15 +0200406 return 0;
407}
408
409/*
410 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
411 */
412static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
413{
414 struct buffer *buf = NULL;
415
416 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
417 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
418 h1c->buf_wait.target = h1c;
419 h1c->buf_wait.wakeup_cb = h1_buf_available;
420 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
421 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
422 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
423 __conn_xprt_stop_recv(h1c->conn);
424 }
425 return buf;
426}
427
428/*
429 * Release a buffer, if any, and try to wake up entities waiting in the buffer
430 * wait queue.
431 */
432static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
433{
434 if (bptr->size) {
435 b_free(bptr);
436 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
437 }
438}
439
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100440/* returns the number of streams in use on a connection to figure if it's idle
441 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
442 * not. This flag is only set when no H1S is attached and when the previous
443 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100444 */
445static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200446{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100447 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200448
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100449 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200450}
451
Willy Tarreau00f18a32019-01-26 12:19:01 +0100452/* returns the number of streams still available on a connection */
453static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100454{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100455 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100456}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200457
Willy Tarreau00f18a32019-01-26 12:19:01 +0100458
Christopher Faulet51dbc942018-09-13 09:05:15 +0200459/*****************************************************************/
460/* functions below are dedicated to the mux setup and management */
461/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200462
463/* returns non-zero if there are input data pending for stream h1s. */
464static inline size_t h1s_data_pending(const struct h1s *h1s)
465{
466 const struct h1m *h1m;
467
468 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
469 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100470 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200471
472 return b_data(&h1s->h1c->ibuf);
473}
474
Christopher Faulet47365272018-10-31 17:40:50 +0100475static struct conn_stream *h1s_new_cs(struct h1s *h1s)
476{
477 struct conn_stream *cs;
478
Christopher Faulet6b81df72019-10-01 22:08:43 +0200479 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100480 cs = cs_new(h1s->h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200481 if (!cs) {
482 TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100483 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200484 }
Christopher Faulet47365272018-10-31 17:40:50 +0100485 h1s->cs = cs;
486 cs->ctx = h1s;
487
488 if (h1s->flags & H1S_F_NOT_FIRST)
489 cs->flags |= CS_FL_NOT_FIRST;
490
Christopher Faulet6b81df72019-10-01 22:08:43 +0200491 if (stream_create_from_cs(cs) < 0) {
492 TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100493 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200494 }
495
496 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100497 return cs;
498
499 err:
500 cs_free(cs);
501 h1s->cs = NULL;
502 return NULL;
503}
504
Olivier Houchardf502aca2018-12-14 19:42:40 +0100505static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200506{
507 struct h1s *h1s;
508
Christopher Faulet6b81df72019-10-01 22:08:43 +0200509 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
510
Christopher Faulet51dbc942018-09-13 09:05:15 +0200511 h1s = pool_alloc(pool_head_h1s);
512 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100513 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200514
515 h1s->h1c = h1c;
516 h1c->h1s = h1s;
517
Olivier Houchardf502aca2018-12-14 19:42:40 +0100518 h1s->sess = sess;
519
Christopher Faulet51dbc942018-09-13 09:05:15 +0200520 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100521 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200522
523 h1s->recv_wait = NULL;
524 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200525
526 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100527 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200528
Christopher Faulet129817b2018-09-20 16:14:40 +0200529 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100530 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200531
532 h1s->status = 0;
533 h1s->meth = HTTP_METH_OTHER;
534
Christopher Faulet47365272018-10-31 17:40:50 +0100535 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
536 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100537 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100538
Christopher Faulet129817b2018-09-20 16:14:40 +0200539 if (!conn_is_back(h1c->conn)) {
540 if (h1c->px->options2 & PR_O2_REQBUG_OK)
541 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200542
543 /* For frontend connections we should always have a session */
544 if (!sess)
545 sess = h1c->conn->owner;
546
Dave Pirotte234740f2019-07-10 13:57:38 +0000547 /* Timers for subsequent sessions on the same HTTP 1.x connection
548 * measure from `now`, not from the connection accept time */
549 if (h1s->flags & H1S_F_NOT_FIRST) {
550 h1s->csinfo.create_date = date;
551 h1s->csinfo.tv_create = now;
552 h1s->csinfo.t_handshake = 0;
553 h1s->csinfo.t_idle = -1;
554 }
555 else {
556 h1s->csinfo.create_date = sess->accept_date;
557 h1s->csinfo.tv_create = sess->tv_accept;
558 h1s->csinfo.t_handshake = sess->t_handshake;
559 h1s->csinfo.t_idle = -1;
560 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200561 }
562 else {
563 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
564 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200565
Christopher Fauletfeb11742018-11-29 15:12:34 +0100566 h1s->csinfo.create_date = date;
567 h1s->csinfo.tv_create = now;
568 h1s->csinfo.t_handshake = 0;
569 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200570 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100571
Christopher Faulete9b70722019-04-08 10:46:02 +0200572 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
573 * create a new one.
574 */
575 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200576 cs->ctx = h1s;
577 h1s->cs = cs;
578 }
Christopher Faulet47365272018-10-31 17:40:50 +0100579 else {
580 cs = h1s_new_cs(h1s);
581 if (!cs)
582 goto fail;
583 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200584 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200585 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100586
587 fail:
588 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200589 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100590 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200591}
592
593static void h1s_destroy(struct h1s *h1s)
594{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200595 if (h1s) {
596 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200597
Christopher Faulet6b81df72019-10-01 22:08:43 +0200598 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200599 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200600
Christopher Fauletf2824e62018-10-01 12:12:37 +0200601 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100602 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200603 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100604 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200605
Christopher Fauletcb55f482018-12-10 11:56:47 +0100606 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200607 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100608 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200609 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
610 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100611
612 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
613 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100614 (h1s->flags & (H1S_F_WANT_KAL|H1S_F_PARSING_DONE)) == (H1S_F_WANT_KAL|H1S_F_PARSING_DONE) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100615 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
616 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
617 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
618 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200619 pool_free(pool_head_h1s, h1s);
620 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200621}
622
Christopher Fauletfeb11742018-11-29 15:12:34 +0100623static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
624{
625 struct h1s *h1s = cs->ctx;
626
627 if (h1s && !conn_is_back(cs->conn))
628 return &h1s->csinfo;
629 return NULL;
630}
631
Christopher Faulet51dbc942018-09-13 09:05:15 +0200632/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200633 * Initialize the mux once it's attached. It is expected that conn->ctx points
634 * to the existing conn_stream (for outgoing connections or for incoming onces
635 * during a mux upgrade) or NULL (for incoming ones during the connexion
636 * establishment). <input> is always used as Input buffer and may contain
637 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
638 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200639 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200640static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
641 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200642{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200643 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100644 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200645 void *conn_ctx = conn->ctx;
646
647 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200648
649 h1c = pool_alloc(pool_head_h1c);
650 if (!h1c)
651 goto fail_h1c;
652 h1c->conn = conn;
653 h1c->px = proxy;
654
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100655 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200656 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200657 h1c->obuf = BUF_NULL;
658 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200659 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200660
Christopher Faulet51dbc942018-09-13 09:05:15 +0200661 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200662 h1c->wait_event.tasklet = tasklet_new();
663 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200664 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200665 h1c->wait_event.tasklet->process = h1_io_cb;
666 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100667 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200668
Christopher Faulete9b70722019-04-08 10:46:02 +0200669 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100670 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
671 if (tick_isset(proxy->timeout.serverfin))
672 h1c->shut_timeout = proxy->timeout.serverfin;
673 } else {
674 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
675 if (tick_isset(proxy->timeout.clientfin))
676 h1c->shut_timeout = proxy->timeout.clientfin;
677 }
678 if (tick_isset(h1c->timeout)) {
679 t = task_new(tid_bit);
680 if (!t)
681 goto fail;
682
683 h1c->task = t;
684 t->process = h1_timeout_task;
685 t->context = h1c;
686 t->expire = tick_add(now_ms, h1c->timeout);
687 }
688
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100689 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200690
Christopher Faulet6b81df72019-10-01 22:08:43 +0200691 /* Always Create a new H1S */
692 if (!h1s_create(h1c, conn_ctx, sess))
693 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100694
695 if (t)
696 task_queue(t);
697
Christopher Faulet51dbc942018-09-13 09:05:15 +0200698 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200699 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200700
701 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200702 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200703 return 0;
704
705 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200706 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200707 if (h1c->wait_event.tasklet)
708 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200709 pool_free(pool_head_h1c, h1c);
710 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200711 conn->ctx = conn_ctx; // restore saved context
712 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200713 return -1;
714}
715
Christopher Faulet73c12072019-04-08 11:23:22 +0200716/* release function. This one should be called to free all resources allocated
717 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200718 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200719static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200720{
Christopher Faulet61840e72019-04-15 09:33:32 +0200721 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200722
Christopher Faulet6b81df72019-10-01 22:08:43 +0200723 TRACE_POINT(H1_EV_H1C_END);
724
Christopher Faulet51dbc942018-09-13 09:05:15 +0200725 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200726 /* The connection must be aattached to this mux to be released */
727 if (h1c->conn && h1c->conn->ctx == h1c)
728 conn = h1c->conn;
729
Christopher Faulet6b81df72019-10-01 22:08:43 +0200730 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
731
Christopher Faulet61840e72019-04-15 09:33:32 +0200732 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200733 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200734 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200735 /* Make sure we're no longer subscribed to anything */
736 if (h1c->wait_event.events)
737 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
738 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200739 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200740 /* connection successfully upgraded to H2, this
741 * mux was already released */
742 return;
743 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200744 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200745 sess_log(conn->owner); /* Log if the upgrade failed */
746 }
747
Olivier Houchard45c44372019-06-11 14:06:23 +0200748
Christopher Faulet51dbc942018-09-13 09:05:15 +0200749 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
750 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
751 LIST_DEL(&h1c->buf_wait.list);
752 LIST_INIT(&h1c->buf_wait.list);
753 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
754 }
755
756 h1_release_buf(h1c, &h1c->ibuf);
757 h1_release_buf(h1c, &h1c->obuf);
758
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100759 if (h1c->task) {
760 h1c->task->context = NULL;
761 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
762 h1c->task = NULL;
763 }
764
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200765 if (h1c->wait_event.tasklet)
766 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200767
Christopher Fauletf2824e62018-10-01 12:12:37 +0200768 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200769 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100770 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200771 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200772 pool_free(pool_head_h1c, h1c);
773 }
774
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200775 if (conn) {
776 conn->mux = NULL;
777 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200778 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200779
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200780 conn_stop_tracking(conn);
781 conn_full_close(conn);
782 if (conn->destroy_cb)
783 conn->destroy_cb(conn);
784 conn_free(conn);
785 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200786}
787
788/******************************************************/
789/* functions below are for the H1 protocol processing */
790/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200791/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
792 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200793 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100794static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200795{
Christopher Faulet570d1612018-11-26 11:13:57 +0100796 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200797
Christopher Faulet570d1612018-11-26 11:13:57 +0100798 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200799 (*(p + 5) > '1' ||
800 (*(p + 5) == '1' && *(p + 7) >= '1')))
801 h1m->flags |= H1_MF_VER_11;
802}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200803
Christopher Faulet9768c262018-10-22 09:34:31 +0200804/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
805 * greater or equal to 1.1
806 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100807static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200808{
Christopher Faulet570d1612018-11-26 11:13:57 +0100809 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200810
Christopher Faulet570d1612018-11-26 11:13:57 +0100811 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200812 (*(p + 5) > '1' ||
813 (*(p + 5) == '1' && *(p + 7) >= '1')))
814 h1m->flags |= H1_MF_VER_11;
815}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200816
Christopher Fauletf2824e62018-10-01 12:12:37 +0200817/* Deduce the connection mode of the client connection, depending on the
818 * configuration and the H1 message flags. This function is called twice, the
819 * first time when the request is parsed and the second time when the response
820 * is parsed.
821 */
822static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
823{
824 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200825
826 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100827 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200828 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100829 h1s->status == 101) {
830 /* Either we've established an explicit tunnel, or we're
831 * switching the protocol. In both cases, we're very unlikely to
832 * understand the next protocols. We have to switch to tunnel
833 * mode, so that we transfer the request and responses then let
834 * this protocol pass unmodified. When we later implement
835 * specific parsers for such protocols, we'll want to check the
836 * Upgrade header which contains information about that protocol
837 * for responses with status 101 (eg: see RFC2817 about TLS).
838 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200839 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200840 TRACE_STATE("set tunnel mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100841 }
842 else if (h1s->flags & H1S_F_WANT_KAL) {
843 /* By default the client is in KAL mode. CLOSE mode mean
844 * it is imposed by the client itself. So only change
845 * KAL mode here. */
846 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
847 /* no length known or explicit close => close */
848 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200849 TRACE_STATE("detect close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100850 }
851 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
852 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
853 /* no explict keep-alive and option httpclose => close */
854 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200855 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100856 }
857 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200858 }
859 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100860 /* Input direction: first pass */
861 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
862 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200863 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200864 TRACE_STATE("detect close mode (req)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100865 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200866 }
867
868 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200869 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200870 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200871 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);
872 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200873}
874
875/* Deduce the connection mode of the client connection, depending on the
876 * configuration and the H1 message flags. This function is called twice, the
877 * first time when the request is parsed and the second time when the response
878 * is parsed.
879 */
880static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
881{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100882 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100883 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100884 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200885
Christopher Fauletf2824e62018-10-01 12:12:37 +0200886 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100887 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200888 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100889 h1s->status == 101) {
890 /* Either we've established an explicit tunnel, or we're
891 * switching the protocol. In both cases, we're very unlikely to
892 * understand the next protocols. We have to switch to tunnel
893 * mode, so that we transfer the request and responses then let
894 * this protocol pass unmodified. When we later implement
895 * specific parsers for such protocols, we'll want to check the
896 * Upgrade header which contains information about that protocol
897 * for responses with status 101 (eg: see RFC2817 about TLS).
898 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200899 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200900 TRACE_STATE("set tunnel mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100901 }
902 else if (h1s->flags & H1S_F_WANT_KAL) {
903 /* By default the server is in KAL mode. CLOSE mode mean
904 * it is imposed by haproxy itself. So only change KAL
905 * mode here. */
906 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
907 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
908 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
909 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200910 TRACE_STATE("detect close mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100911 }
912 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200913 }
Christopher Faulet70033782018-12-05 13:50:11 +0100914 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100915 /* Output direction: first pass */
916 if (h1m->flags & H1_MF_CONN_CLO) {
917 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100918 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200919 TRACE_STATE("detect close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100920 }
921 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
922 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
923 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
924 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
925 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
926 /* no explicit keep-alive option httpclose/server-close => close */
927 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200928 TRACE_STATE("force close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100929 }
Christopher Faulet70033782018-12-05 13:50:11 +0100930 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200931
932 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200933 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200934 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200935 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);
936 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200937}
938
Christopher Fauletb992af02019-03-28 15:42:24 +0100939static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200940{
941 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200942
943 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
944 * token is found
945 */
946 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200947 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200948
949 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200950 if (!(h1m->flags & H1_MF_VER_11)) {
951 TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100952 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200953 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200954 }
955 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200956 if (h1m->flags & H1_MF_VER_11) {
957 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100958 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200959 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200960 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200961}
962
Christopher Fauletb992af02019-03-28 15:42:24 +0100963static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200964{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200965 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
966 * token is found
967 */
968 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200969 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200970
971 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100972 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +0200973 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
974 TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100975 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200976 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200977 }
978 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200979 if (h1m->flags & H1_MF_VER_11) {
980 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100981 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200982 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200983 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200984}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200985
Christopher Fauletb992af02019-03-28 15:42:24 +0100986static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200987{
Christopher Fauletb992af02019-03-28 15:42:24 +0100988 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200989 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100990 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200991 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200992}
993
Christopher Fauletb992af02019-03-28 15:42:24 +0100994static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
995{
996 if (!conn_is_back(h1s->h1c->conn))
997 h1_set_cli_conn_mode(h1s, h1m);
998 else
999 h1_set_srv_conn_mode(h1s, h1m);
1000
1001 if (!(h1m->flags & H1_MF_RESP))
1002 h1_update_req_conn_value(h1s, h1m, conn_val);
1003 else
1004 h1_update_res_conn_value(h1s, h1m, conn_val);
1005}
Christopher Faulete44769b2018-11-29 23:01:45 +01001006
Christopher Faulet98fbe952019-07-22 16:18:24 +02001007/* Try to adjust the case of the message header name using the global map
1008 * <hdrs_map>.
1009 */
1010static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1011{
1012 struct ebpt_node *node;
1013 struct h1_hdr_entry *entry;
1014
1015 /* No entry in the map, do nothing */
1016 if (eb_is_empty(&hdrs_map.map))
1017 return;
1018
1019 /* No conversion fo the request headers */
1020 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1021 return;
1022
1023 /* No conversion fo the response headers */
1024 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1025 return;
1026
1027 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1028 if (!node)
1029 return;
1030 entry = container_of(node, struct h1_hdr_entry, node);
1031 name->ptr = entry->name.ptr;
1032 name->len = entry->name.len;
1033}
1034
Christopher Faulete44769b2018-11-29 23:01:45 +01001035/* Append the description of what is present in error snapshot <es> into <out>.
1036 * The description must be small enough to always fit in a buffer. The output
1037 * buffer may be the trash so the trash must not be used inside this function.
1038 */
1039static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1040{
1041 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001042 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1043 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1044 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1045 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1046 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1047 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001048}
1049/*
1050 * Capture a bad request or response and archive it in the proxy's structure.
1051 * By default it tries to report the error position as h1m->err_pos. However if
1052 * this one is not set, it will then report h1m->next, which is the last known
1053 * parsing point. The function is able to deal with wrapping buffers. It always
1054 * displays buffers as a contiguous area starting at buf->p. The direction is
1055 * determined thanks to the h1m's flags.
1056 */
1057static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1058 struct h1m *h1m, struct buffer *buf)
1059{
1060 struct session *sess = h1c->conn->owner;
1061 struct proxy *proxy = h1c->px;
1062 struct proxy *other_end = sess->fe;
1063 union error_snapshot_ctx ctx;
1064
Willy Tarreau598d7fc2018-12-18 18:10:38 +01001065 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +01001066 other_end = si_strm(h1s->cs->data)->be;
1067
1068 /* http-specific part now */
1069 ctx.h1.state = h1m->state;
1070 ctx.h1.c_flags = h1c->flags;
1071 ctx.h1.s_flags = h1s->flags;
1072 ctx.h1.m_flags = h1m->flags;
1073 ctx.h1.m_clen = h1m->curr_len;
1074 ctx.h1.m_blen = h1m->body_len;
1075
1076 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1077 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001078 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1079 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001080}
1081
Christopher Fauletadb22202018-12-12 10:32:09 +01001082/* Emit the chunksize followed by a CRLF in front of data of the buffer
1083 * <buf>. It goes backwards and starts with the byte before the buffer's
1084 * head. The caller is responsible for ensuring there is enough room left before
1085 * the buffer's head for the string.
1086 */
1087static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1088{
1089 char *beg, *end;
1090
1091 beg = end = b_head(buf);
1092 *--beg = '\n';
1093 *--beg = '\r';
1094 do {
1095 *--beg = hextab[chksz & 0xF];
1096 } while (chksz >>= 4);
1097 buf->head -= (end - beg);
1098 b_add(buf, end - beg);
1099}
1100
1101/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1102 * ensuring there is enough room left in the buffer for the string. */
1103static void h1_emit_chunk_crlf(struct buffer *buf)
1104{
1105 *(b_peek(buf, b_data(buf))) = '\r';
1106 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1107 b_add(buf, 2);
1108}
1109
Christopher Faulet129817b2018-09-20 16:14:40 +02001110/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001111 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001112 * CONNECT requests. On the client side, if the response is not finished, the
1113 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001114 */
1115static void h1_set_req_tunnel_mode(struct h1s *h1s)
1116{
1117 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1118 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001119 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1120
Christopher Faulet76014fd2019-12-10 11:47:22 +01001121 if (!conn_is_back(h1s->h1c->conn)) {
1122 h1s->flags &= ~H1S_F_PARSING_DONE;
1123 if (h1s->res.state < H1_MSG_DONE) {
1124 h1s->h1c->flags |= H1C_F_IN_BUSY;
1125 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1126 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001127 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001128 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1129 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1130 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1131 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1132 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001133}
1134
1135/*
1136 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletf3158e92019-11-15 11:14:23 +01001137 * successfull replies to CONNECT requests or on protocol switching. In this
1138 * last case, this function takes care to switch the request to tunnel mode if
1139 * possible. On the server side, if the request is not finished, the mux is mark
1140 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001141 */
1142static void h1_set_res_tunnel_mode(struct h1s *h1s)
1143{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001144
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001145 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1146 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001147 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1148
Christopher Faulet76014fd2019-12-10 11:47:22 +01001149 if (conn_is_back(h1s->h1c->conn)) {
1150 h1s->flags &= ~H1S_F_PARSING_DONE;
1151 /* On protocol switching, switch the request to tunnel mode if it is in
1152 * DONE state. Otherwise we will wait the end of the request to switch
1153 * it in tunnel mode.
1154 */
1155 if (h1s->req.state < H1_MSG_DONE) {
1156 h1s->h1c->flags |= H1C_F_IN_BUSY;
1157 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1158 }
1159 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1160 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1161 h1s->req.state = H1_MSG_TUNNEL;
1162 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1163 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001164 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001165 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1166 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1167 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1168 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001169 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001170}
1171
1172/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001173 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001174 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001175 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001176 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001177static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001178 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001179{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001180 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001181 int ret = 0;
1182
Christopher Faulet6b81df72019-10-01 22:08:43 +02001183 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1184
Christopher Fauleteec96b52019-09-25 09:10:46 +02001185 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001186 /* Try to match H2 preface before parsing the request headers. */
1187 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001188 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001189 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001190 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001191 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001192 else {
1193 if (h1s->meth == HTTP_METH_CONNECT)
1194 h1m->flags |= H1_MF_METH_CONNECT;
1195 if (h1s->meth == HTTP_METH_HEAD)
1196 h1m->flags |= H1_MF_METH_HEAD;
1197 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001198
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001199 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1200 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001201 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001202 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001203 if (!(h1m->flags & H1_MF_RESP)) {
1204 h1s->flags |= H1S_F_REQ_ERROR;
1205 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1206 }
1207 else {
1208 h1s->flags |= H1S_F_RES_ERROR;
1209 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1210 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001211 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001212 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001213 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1214 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001215 goto end;
1216 }
1217
Christopher Faulet486498c2019-10-11 14:22:00 +02001218 if (h1m->err_pos >= 0) {
1219 /* Maybe we found an error during the parsing while we were
1220 * configured not to block on that, so we have to capture it
1221 * now.
1222 */
1223 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1224 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1225 }
1226
Christopher Faulet129817b2018-09-20 16:14:40 +02001227 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001228 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001229 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001230 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001231 }
1232 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001233 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001234 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001235 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001236 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001237 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001238 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001239
Christopher Faulet129817b2018-09-20 16:14:40 +02001240 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001241 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001242 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001243
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001244 h2c_upgrade:
1245 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001246 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001247 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001248 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1249 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001250}
1251
1252/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001253 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001254 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1255 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001256 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001257static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001258 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001259 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001260{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001261 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001262
Christopher Faulet6b81df72019-10-01 22:08:43 +02001263 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001264 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001265 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001266 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Christopher Faulet02a02532019-11-15 09:36:28 +01001267 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001268 if (!(h1m->flags & H1_MF_RESP)) {
1269 h1s->flags |= H1S_F_REQ_ERROR;
1270 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1271 }
1272 else {
1273 h1s->flags |= H1S_F_RES_ERROR;
1274 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1275 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001276 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001277 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001278 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001279 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001280 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001281 }
1282
Christopher Faulet02a02532019-11-15 09:36:28 +01001283 *ofs += ret;
1284
1285 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001286 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001287 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001288}
1289
1290/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001291 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1292 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1293 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1294 * responsible to update the parser state <h1m>.
1295 */
1296static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1297 struct buffer *buf, size_t *ofs, size_t max)
1298{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001299 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001300
Christopher Faulet6b81df72019-10-01 22:08:43 +02001301 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001302 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001303 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001304 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Christopher Faulet02a02532019-11-15 09:36:28 +01001305 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001306 if (!(h1m->flags & H1_MF_RESP)) {
1307 h1s->flags |= H1S_F_REQ_ERROR;
1308 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1309 }
1310 else {
1311 h1s->flags |= H1S_F_RES_ERROR;
1312 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1313 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001314 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001315 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001316 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1317 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001318 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001319 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001320
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001321 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001322
1323 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001324 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001325 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001326}
1327
1328/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001329 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
1330 * proceed. This functions is responsible to update the parser state <h1m>. It
1331 * also add the flag CS_FL_EOI on the CS.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001332 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001333static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1334 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001335{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001336 int ret;
1337
1338 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){max});
1339 ret = h1_parse_msg_eom(h1m, htx, max);
1340 if (!ret) {
1341 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1342 if (htx->flags & HTX_FL_PARSING_ERROR) {
1343 if (!(h1m->flags & H1_MF_RESP)) {
1344 h1s->flags |= H1S_F_REQ_ERROR;
1345 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1346 }
1347 else {
1348 h1s->flags |= H1S_F_RES_ERROR;
1349 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1350 }
1351 h1s->cs->flags |= CS_FL_EOI;
1352 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1353 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1354 }
1355 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001356 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001357
Christopher Faulet76014fd2019-12-10 11:47:22 +01001358 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001359 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001360 end:
1361 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s,, (size_t[]){ret});
1362 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001363}
1364
1365/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001366 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001367 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1368 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001369 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001370static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001371{
Christopher Faulet539e0292018-11-19 10:40:09 +01001372 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001373 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001374 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001375 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001376 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001377 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001378
Christopher Faulet539e0292018-11-19 10:40:09 +01001379 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001380 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001381
Christopher Fauletf2824e62018-10-01 12:12:37 +02001382 if (!conn_is_back(h1c->conn)) {
1383 h1m = &h1s->req;
1384 errflag = H1S_F_REQ_ERROR;
1385 }
1386 else {
1387 h1m = &h1s->res;
1388 errflag = H1S_F_RES_ERROR;
1389 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001390
Christopher Faulet74027762019-02-26 14:45:05 +01001391 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001392 if (h1s->flags & errflag)
1393 goto end;
1394
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001395 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001396 size_t used = htx_used_space(htx);
1397
Christopher Faulet129817b2018-09-20 16:14:40 +02001398 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001399 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001400 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001401 if (!ret)
1402 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001403
1404 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1405 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1406
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001407 if ((h1m->flags & H1_MF_RESP) &&
1408 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1409 h1m_init_res(&h1s->res);
1410 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001411 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001412 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001413 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001414 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001415 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001416 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001417 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001418 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001419
1420 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1421 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001422 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001423 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001424 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1425 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1426 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001427 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001428
Christopher Faulet76014fd2019-12-10 11:47:22 +01001429 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1430 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001431 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001432 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001433 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1434 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1435 break;
1436
1437 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1438 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1439 }
1440
Christopher Fauletf3158e92019-11-15 11:14:23 +01001441 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1442 h1_set_req_tunnel_mode(h1s);
1443 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001444 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001445 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1446 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001447 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001448 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001449 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001450 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001451 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001452 if (!ret)
1453 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001454
1455 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1456 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001457 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001458 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001459 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001460 break;
1461 }
1462
Christopher Faulet30db3d72019-05-17 15:35:33 +02001463 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001464 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001465
Christopher Faulet6b81df72019-10-01 22:08:43 +02001466 if (h1s->flags & errflag) {
1467 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001468 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001469 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001470
Christopher Faulet539e0292018-11-19 10:40:09 +01001471 b_del(&h1c->ibuf, total);
1472
1473 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001474 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001475 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001476 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001477 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001478 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001479 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001480 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001481
Christopher Fauletcf56b992018-12-11 16:12:31 +01001482 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1483
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001484 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001485 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001486 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001487 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001488 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001489 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001490 if (h1m->state == H1_MSG_TUNNEL)
1491 h1s->cs->flags |= CS_FL_EOI;
1492 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001493 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001494 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001495
Christopher Faulet6b81df72019-10-01 22:08:43 +02001496 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001497 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001498
1499 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001500 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001501 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001502 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001504}
1505
Christopher Faulet129817b2018-09-20 16:14:40 +02001506/*
1507 * Process outgoing data. It parses data and transfer them from the channel buffer into
1508 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1509 * 0 if it couldn't proceed.
1510 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001511static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1512{
Christopher Faulet129817b2018-09-20 16:14:40 +02001513 struct h1s *h1s = h1c->h1s;
1514 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001515 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001516 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001517 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001518 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001519 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001520
Christopher Faulet47365272018-10-31 17:40:50 +01001521 if (!count)
1522 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001523
Christopher Faulet94b2c762019-05-24 15:28:57 +02001524 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001525 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1526
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001527 if (htx_is_empty(chn_htx))
1528 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001529
Christopher Faulet51dbc942018-09-13 09:05:15 +02001530 if (!h1_get_buf(h1c, &h1c->obuf)) {
1531 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001532 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001533 goto end;
1534 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001535
Christopher Fauletf2824e62018-10-01 12:12:37 +02001536 if (!conn_is_back(h1c->conn)) {
1537 h1m = &h1s->res;
1538 errflag = H1S_F_RES_ERROR;
1539 }
1540 else {
1541 h1m = &h1s->req;
1542 errflag = H1S_F_REQ_ERROR;
1543 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001544
Christopher Faulet0e54d542019-07-04 21:22:34 +02001545 if (h1s->flags & errflag)
1546 goto end;
1547
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001548 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001549 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001550
Willy Tarreau3815b222018-12-11 19:50:43 +01001551 /* Perform some optimizations to reduce the number of buffer copies.
1552 * First, if the mux's buffer is empty and the htx area contains
1553 * exactly one data block of the same size as the requested count,
1554 * then it's possible to simply swap the caller's buffer with the
1555 * mux's output buffer and adjust offsets and length to match the
1556 * entire DATA HTX block in the middle. In this case we perform a
1557 * true zero-copy operation from end-to-end. This is the situation
1558 * that happens all the time with large files. Second, if this is not
1559 * possible, but the mux's output buffer is empty, we still have an
1560 * opportunity to avoid the copy to the intermediary buffer, by making
1561 * the intermediary buffer's area point to the output buffer's area.
1562 * In this case we want to skip the HTX header to make sure that copies
1563 * remain aligned and that this operation remains possible all the
1564 * time. This goes for headers, data blocks and any data extracted from
1565 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001566 */
1567 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001568 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001569 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001570 htx_get_blk_value(chn_htx, blk).len == count) {
1571 void *old_area = h1c->obuf.area;
1572
Christopher Faulet6b81df72019-10-01 22:08:43 +02001573 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001574 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001575 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001576 h1c->obuf.data = count;
1577
1578 buf->area = old_area;
1579 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001580
Christopher Faulet6b81df72019-10-01 22:08:43 +02001581 chn_htx = (struct htx *)buf->area;
1582 htx_reset(chn_htx);
1583
Christopher Fauletadb22202018-12-12 10:32:09 +01001584 /* The message is chunked. We need to emit the chunk
1585 * size. We have at least the size of the struct htx to
1586 * write the chunk envelope. It should be enough.
1587 */
1588 if (h1m->flags & H1_MF_CHNK) {
1589 h1_emit_chunk_size(&h1c->obuf, count);
1590 h1_emit_chunk_crlf(&h1c->obuf);
1591 }
1592
Willy Tarreau3815b222018-12-11 19:50:43 +01001593 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001594 if (h1m->state == H1_MSG_DATA)
1595 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001596 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001597 else
1598 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001599 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001600 goto out;
1601 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001602 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001603 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001604 else
1605 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001606
Christopher Fauletc2518a52019-06-25 21:41:02 +02001607 tmp.data = 0;
1608 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001609 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001610 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001611 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001612 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001613 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001614 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001615
Christopher Fauletb2e84162018-12-06 11:39:49 +01001616 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001617 if (type != HTX_BLK_DATA && vlen > count)
1618 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001619
Christopher Faulet94b2c762019-05-24 15:28:57 +02001620 if (type == HTX_BLK_UNUSED)
1621 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001622
Christopher Faulet94b2c762019-05-24 15:28:57 +02001623 switch (h1m->state) {
1624 case H1_MSG_RQBEFORE:
1625 if (type != HTX_BLK_REQ_SL)
1626 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001627 TRACE_USER("sending request headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001628 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001629 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001630 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001631 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001632 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001633 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001634 if (sl->flags & HTX_SL_F_BODYLESS)
1635 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001636 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001637 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001638
Christopher Faulet94b2c762019-05-24 15:28:57 +02001639 case H1_MSG_RPBEFORE:
1640 if (type != HTX_BLK_RES_SL)
1641 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001642 TRACE_USER("sending response headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001643 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001644 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001646 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001647 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001648 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001650 if (sl->info.res.status < 200 &&
1651 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001652 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001653 h1m->state = H1_MSG_HDR_FIRST;
1654 break;
1655
Christopher Faulet94b2c762019-05-24 15:28:57 +02001656 case H1_MSG_HDR_FIRST:
1657 case H1_MSG_HDR_NAME:
1658 case H1_MSG_HDR_L2_LWS:
1659 if (type == HTX_BLK_EOH)
1660 goto last_lf;
1661 if (type != HTX_BLK_HDR)
1662 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001663
Christopher Faulet9768c262018-10-22 09:34:31 +02001664 h1m->state = H1_MSG_HDR_NAME;
1665 n = htx_get_blk_name(chn_htx, blk);
1666 v = htx_get_blk_value(chn_htx, blk);
1667
Christopher Faulet86d144c2019-08-14 16:32:25 +02001668 /* Skip all pseudo-headers */
1669 if (*(n.ptr) == ':')
1670 goto skip_hdr;
1671
Christopher Faulet9768c262018-10-22 09:34:31 +02001672 if (isteqi(n, ist("transfer-encoding")))
1673 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001674 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001675 /* Only skip C-L header with invalid value. */
1676 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001677 goto skip_hdr;
1678 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001680 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001681 if (!v.len)
1682 goto skip_hdr;
1683 }
1684
Christopher Faulet67d58092019-10-02 10:51:38 +02001685 /* Skip header if same name is used to add the server name */
1686 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1687 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1688 goto skip_hdr;
1689
Christopher Faulet98fbe952019-07-22 16:18:24 +02001690 /* Try to adjust the case of the header name */
1691 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1692 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001693 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001694 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001695 skip_hdr:
1696 h1m->state = H1_MSG_HDR_L2_LWS;
1697 break;
1698
Christopher Faulet94b2c762019-05-24 15:28:57 +02001699 case H1_MSG_LAST_LF:
1700 if (type != HTX_BLK_EOH)
1701 goto error;
1702 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001703 h1m->state = H1_MSG_LAST_LF;
1704 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001705 /* If the reply comes from haproxy while the request is
1706 * not finished, we force the connection close. */
1707 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1708 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1709 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1710 }
1711
1712 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001713 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001714 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001715 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001716 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001717 /* Try to adjust the case of the header name */
1718 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1719 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001720 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001721 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001722 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001723 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001724 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001725
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001726 if ((h1s->meth != HTTP_METH_CONNECT &&
1727 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001728 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1729 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1730 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1731 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1732 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001733 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001734 n = ist("transfer-encoding");
1735 v = ist("chunked");
1736 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1737 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001738 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001739 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001740 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001741 h1m->flags |= H1_MF_CHNK;
1742 }
1743
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001744 /* Now add the server name to a header (if requested) */
1745 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1746 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1747 struct server *srv = objt_server(h1c->conn->target);
1748
1749 if (srv) {
1750 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1751 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001752
1753 /* Try to adjust the case of the header name */
1754 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1755 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001756 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001757 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001758 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001759 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001760 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1761 }
1762
Christopher Fauletc2518a52019-06-25 21:41:02 +02001763 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001764 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001765
Christopher Faulet6b81df72019-10-01 22:08:43 +02001766 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1767 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1768
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001769 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1770 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1771 h1_set_req_tunnel_mode(h1s);
1772 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001773 else if ((h1m->flags & H1_MF_RESP) &&
1774 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001775 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001776 * to the client. Switch the response to tunnel mode.
1777 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001778 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001779 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001780 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001781 else if ((h1m->flags & H1_MF_RESP) &&
1782 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1783 h1m_init_res(&h1s->res);
1784 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001785 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001786 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001787 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001788 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001789 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001790 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1791 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001792 else
1793 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001794 break;
1795
Christopher Faulet94b2c762019-05-24 15:28:57 +02001796 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001797 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001798 if (type == HTX_BLK_EOM) {
1799 /* Chunked message without explicit trailers */
1800 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001801 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001802 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001803 }
1804 goto done;
1805 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001806 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001807 /* If the message is not chunked, never
1808 * add the last chunk. */
1809 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001810 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001811 TRACE_PROTO("sending message trailers", H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s, chn_htx);
Christopher Faulet94b2c762019-05-24 15:28:57 +02001812 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001813 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001814 else if (type != HTX_BLK_DATA)
1815 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001816
1817 TRACE_PROTO("sending message data", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){sz});
Christopher Fauletd9233f02019-10-14 14:01:24 +02001818
1819
1820 if (vlen > count) {
1821 /* Get the maximum amount of data we can xferred */
1822 vlen = count;
1823 }
1824
1825 chklen = 0;
1826 if (h1m->flags & H1_MF_CHNK) {
1827 chklen = b_room(&tmp);
1828 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1829 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1830 (chklen < 1048576) ? 5 : 8);
1831 chklen += 4; /* 2 x CRLF */
1832 }
1833
1834 if (vlen + chklen > b_room(&tmp)) {
1835 /* too large for the buffer */
1836 if (chklen >= b_room(&tmp))
1837 goto full;
1838 vlen = b_room(&tmp) - chklen;
1839 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001840 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001841 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001842 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001843 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001844
1845 if (h1m->state == H1_MSG_DATA)
1846 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001847 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001848 else
1849 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001850 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001851 break;
1852
Christopher Faulet94b2c762019-05-24 15:28:57 +02001853 case H1_MSG_TRAILERS:
1854 if (type == HTX_BLK_EOM)
1855 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001856 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001857 goto error;
1858 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001859 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001860 /* If the message is not chunked, ignore
1861 * trailers. It may happen with H2 messages. */
1862 if (!(h1m->flags & H1_MF_CHNK))
1863 break;
1864
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001865 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001866 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001867 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001868 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1869 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001870 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001871 else { // HTX_BLK_TLR
1872 n = htx_get_blk_name(chn_htx, blk);
1873 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001874
1875 /* Try to adjust the case of the header name */
1876 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1877 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001878 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001879 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001880 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001881 break;
1882
Christopher Faulet94b2c762019-05-24 15:28:57 +02001883 case H1_MSG_DONE:
1884 if (type != HTX_BLK_EOM)
1885 goto error;
1886 done:
1887 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001888 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1889 h1_set_req_tunnel_mode(h1s);
1890 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1891 }
1892 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001893 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1894 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001895 TRACE_STATE("h1c no more busy", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001896 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001897
1898 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1899 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001900 break;
1901
Christopher Faulet9768c262018-10-22 09:34:31 +02001902 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001903 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001904 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001905 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001906 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001907 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001908 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001909 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1910 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001911 break;
1912 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001913
1914 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001915 total += vlen;
1916 count -= vlen;
1917 if (sz == vlen)
1918 blk = htx_remove_blk(chn_htx, blk);
1919 else {
1920 htx_cut_data_blk(chn_htx, blk, vlen);
1921 break;
1922 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001923 }
1924
Christopher Faulet9768c262018-10-22 09:34:31 +02001925 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001926 /* when the output buffer is empty, tmp shares the same area so that we
1927 * only have to update pointers and lengths.
1928 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001929 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1930 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001931 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001932 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001933
Willy Tarreau3815b222018-12-11 19:50:43 +01001934 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001935 out:
1936 if (!buf_room_for_htx_data(&h1c->obuf)) {
1937 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001938 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001939 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001940 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001941 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001942 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001943
1944 full:
1945 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1946 h1c->flags |= H1C_F_OUT_FULL;
1947 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001948}
1949
Christopher Faulet51dbc942018-09-13 09:05:15 +02001950/*********************************************************/
1951/* functions below are I/O callbacks from the connection */
1952/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001953static void h1_wake_stream_for_recv(struct h1s *h1s)
1954{
1955 if (h1s && h1s->recv_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001956 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001957 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001958 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001959 h1s->recv_wait = NULL;
1960 }
1961}
1962static void h1_wake_stream_for_send(struct h1s *h1s)
1963{
1964 if (h1s && h1s->send_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001965 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001966 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001967 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001968 h1s->send_wait = NULL;
1969 }
1970}
1971
Christopher Faulet51dbc942018-09-13 09:05:15 +02001972/*
1973 * Attempt to read data, and subscribe if none available
1974 */
1975static int h1_recv(struct h1c *h1c)
1976{
1977 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001978 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001979 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001980 int rcvd = 0;
1981
Christopher Faulet6b81df72019-10-01 22:08:43 +02001982 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
1983
1984 if (h1c->wait_event.events & SUB_RETRY_RECV) {
1985 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01001986 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001987 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001988
Olivier Houchard75159a92018-12-03 18:46:09 +01001989 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001990 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01001991 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001992 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001993 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001994
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001995 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1996 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001997 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02001998 goto end;
1999 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002000
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002001 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002002 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002003 h1_wake_stream_for_recv(h1s);
2004 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002005 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002006 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002007 }
2008
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002009 /*
2010 * If we only have a small amount of data, realign it,
2011 * it's probably cheaper than doing 2 recv() calls.
2012 */
2013 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2014 b_slow_realign(&h1c->ibuf, trash.area, 0);
2015
Willy Tarreau45f2b892018-12-05 07:59:27 +01002016 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002017 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002018 if (h1c->flags & H1C_F_IN_FULL) {
2019 h1c->flags &= ~H1C_F_IN_FULL;
2020 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2021 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002022
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002023 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002024 if (!b_data(&h1c->ibuf)) {
2025 /* try to pre-align the buffer like the rxbufs will be
2026 * to optimize memory copies.
2027 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002028 h1c->ibuf.head = sizeof(struct htx);
2029 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002030 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002031 }
Christopher Faulet47365272018-10-31 17:40:50 +01002032 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002033 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn,,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002034 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002035 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002036 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002037 if (h1s->csinfo.t_idle == -1)
2038 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2039 }
Christopher Faulet47365272018-10-31 17:40:50 +01002040 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002041
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002042 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002043 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002044 goto end;
2045 }
2046
Christopher Faulet6b81df72019-10-01 22:08:43 +02002047 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002048 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002049
Christopher Faulet9768c262018-10-22 09:34:31 +02002050 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002051 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2052 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002053
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002054 if (conn_xprt_read0_pending(conn) && h1s) {
2055 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002056 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002057 rcvd = 1;
2058 }
2059
Christopher Faulet51dbc942018-09-13 09:05:15 +02002060 if (!b_data(&h1c->ibuf))
2061 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002062 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002063 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002064 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2065 }
2066
2067 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002068 return rcvd;
2069}
2070
2071
2072/*
2073 * Try to send data if possible
2074 */
2075static int h1_send(struct h1c *h1c)
2076{
2077 struct connection *conn = h1c->conn;
2078 unsigned int flags = 0;
2079 size_t ret;
2080 int sent = 0;
2081
Christopher Faulet6b81df72019-10-01 22:08:43 +02002082 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2083
2084 if (conn->flags & CO_FL_ERROR) {
2085 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002086 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002087 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002088
2089 if (!b_data(&h1c->obuf))
2090 goto end;
2091
2092 if (h1c->flags & H1C_F_OUT_FULL)
2093 flags |= CO_SFL_MSG_MORE;
2094
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002095 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002096 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002097 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn,,, (size_t[]){ret});
2098 if (h1c->flags & H1C_F_OUT_FULL) {
2099 h1c->flags &= ~H1C_F_OUT_FULL;
2100 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2101 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002102 b_del(&h1c->obuf, ret);
2103 sent = 1;
2104 }
2105
Christopher Faulet145aa472018-12-06 10:56:20 +01002106 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002107 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002108 /* error or output closed, nothing to send, clear the buffer to release it */
2109 b_reset(&h1c->obuf);
2110 }
2111
Christopher Faulet51dbc942018-09-13 09:05:15 +02002112 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002113 if (!(h1c->flags & H1C_F_OUT_FULL))
2114 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002115
Christopher Faulet51dbc942018-09-13 09:05:15 +02002116 /* We're done, no more to send */
2117 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002118 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002119 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002120 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2121 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002122 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002123 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002124 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002125 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2126 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002127 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002128 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129
Christopher Faulet6b81df72019-10-01 22:08:43 +02002130 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002131 return sent;
2132}
2133
Christopher Faulet51dbc942018-09-13 09:05:15 +02002134
2135/* callback called on any event by the connection handler.
2136 * It applies changes and returns zero, or < 0 if it wants immediate
2137 * destruction of the connection.
2138 */
2139static int h1_process(struct h1c * h1c)
2140{
2141 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002142 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002143
Christopher Faulet6b81df72019-10-01 22:08:43 +02002144 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2145
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002146 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002147 return -1;
2148
Christopher Fauletfeb11742018-11-29 15:12:34 +01002149 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002150 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002151 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002152 goto release;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002153 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002154 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002155 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002156 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002157 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002158 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002159 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002160 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002161 }
2162
Christopher Fauletfeb11742018-11-29 15:12:34 +01002163 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2164 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2165
Christopher Faulet6b81df72019-10-01 22:08:43 +02002166 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002167 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002168 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2169 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002170
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002171 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002172 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002173 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002174 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002175 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002176 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002177 h1s->cs->data_cb->wake(h1s->cs);
2178 }
Christopher Faulet47365272018-10-31 17:40:50 +01002179 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002180 if (h1c->task) {
2181 h1c->task->expire = TICK_ETERNITY;
2182 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002183 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002184 ? h1c->shut_timeout
2185 : h1c->timeout));
2186 task_queue(h1c->task);
2187 }
2188 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002189 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002190 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002191
2192 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002193 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002194 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002195 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002196}
2197
2198static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2199{
2200 struct h1c *h1c = ctx;
2201 int ret = 0;
2202
Christopher Faulet6b81df72019-10-01 22:08:43 +02002203 TRACE_POINT(H1_EV_H1C_WAKE, h1c->conn);
2204
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002205 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002206 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002207 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002209 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002210 h1_process(h1c);
2211 return NULL;
2212}
2213
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002214static void h1_reset(struct connection *conn)
2215{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002216
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002217}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002218
2219static int h1_wake(struct connection *conn)
2220{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002221 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002222 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002223
Christopher Faulet6b81df72019-10-01 22:08:43 +02002224 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2225
Christopher Faulet539e0292018-11-19 10:40:09 +01002226 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002227 ret = h1_process(h1c);
2228 if (ret == 0) {
2229 struct h1s *h1s = h1c->h1s;
2230
Christopher Faulet6b81df72019-10-01 22:08:43 +02002231 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2232 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002233 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002234 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002235 }
2236 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002237}
2238
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002239/* Connection timeout management. The principle is that if there's no receipt
2240 * nor sending for a certain amount of time, the connection is closed.
2241 */
2242static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2243{
2244 struct h1c *h1c = context;
2245 int expired = tick_is_expired(t->expire, now_ms);
2246
Christopher Faulet6b81df72019-10-01 22:08:43 +02002247 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2248
2249 if (!expired && h1c) {
2250 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002251 return t;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002252 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002253
Olivier Houchard3f795f72019-04-17 22:51:06 +02002254 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002255
2256 if (!h1c) {
2257 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002258 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002259 return NULL;
2260 }
2261
2262 h1c->task = NULL;
2263 /* If a stream is still attached to the mux, just set an error and wait
2264 * for the stream's timeout. Otherwise, release the mux. This is only ok
2265 * because same timeouts are used.
2266 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002267 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002268 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002269 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2270 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002271 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002272 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002273
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002274 return NULL;
2275}
2276
Christopher Faulet51dbc942018-09-13 09:05:15 +02002277/*******************************************/
2278/* functions below are used by the streams */
2279/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002280
Christopher Faulet51dbc942018-09-13 09:05:15 +02002281/*
2282 * Attach a new stream to a connection
2283 * (Used for outgoing connections)
2284 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002285static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002286{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002287 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002288 struct conn_stream *cs = NULL;
2289 struct h1s *h1s;
2290
Christopher Faulet6b81df72019-10-01 22:08:43 +02002291 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2292 if (h1c->flags & H1C_F_CS_ERROR) {
2293 TRACE_DEVEL("leaving on h1c error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002294 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002295 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002296
2297 cs = cs_new(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002298 if (!cs) {
2299 TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002300 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002301 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002302
Olivier Houchardf502aca2018-12-14 19:42:40 +01002303 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002304 if (h1s == NULL) {
2305 TRACE_DEVEL("leaving on h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002306 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002307 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002308
Christopher Faulet6b81df72019-10-01 22:08:43 +02002309 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002310 return cs;
2311 end:
2312 cs_free(cs);
2313 return NULL;
2314}
2315
2316/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2317 * this mux, it's easy as we can only store a single conn_stream.
2318 */
2319static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2320{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002321 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002322 struct h1s *h1s = h1c->h1s;
2323
2324 if (h1s)
2325 return h1s->cs;
2326
2327 return NULL;
2328}
2329
Christopher Faulet73c12072019-04-08 11:23:22 +02002330static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002331{
Christopher Faulet73c12072019-04-08 11:23:22 +02002332 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002333
Christopher Faulet6b81df72019-10-01 22:08:43 +02002334 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002335 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002336 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002337}
2338
2339/*
2340 * Detach the stream from the connection and possibly release the connection.
2341 */
2342static void h1_detach(struct conn_stream *cs)
2343{
2344 struct h1s *h1s = cs->ctx;
2345 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002346 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002347 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002348
Christopher Faulet6b81df72019-10-01 22:08:43 +02002349 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2350
Christopher Faulet51dbc942018-09-13 09:05:15 +02002351 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002352 if (!h1s) {
2353 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002355 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002356
Olivier Houchardf502aca2018-12-14 19:42:40 +01002357 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002358 h1c = h1s->h1c;
2359 h1s->cs = NULL;
2360
Olivier Houchard8a786902018-12-15 16:05:40 +01002361 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2362 h1s_destroy(h1s);
2363
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002364 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002365 /* If there are any excess server data in the input buffer,
2366 * release it and close the connection ASAP (some data may
2367 * remain in the output buffer). This happens if a server sends
2368 * invalid responses. So in such case, we don't want to reuse
2369 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002370 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002371 if (b_data(&h1c->ibuf)) {
2372 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002373 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002374 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002375 goto release;
2376 }
Christopher Faulet03627242019-07-19 11:34:08 +02002377
Christopher Faulet9400a392018-11-23 23:10:39 +01002378 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002379 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002380 h1c->conn->flags |= CO_FL_PRIVATE;
2381
Olivier Houchard44d59142018-12-13 18:46:22 +01002382 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002383 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002384 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2385 h1c->conn->owner = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002386 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) {
Olivier Houchard351411f2018-12-27 17:20:54 +01002387 /* The server doesn't want it, let's kill the connection right away */
2388 h1c->conn->mux->destroy(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002389 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2390 goto end;
2391 }
2392 tasklet_wakeup(h1c->wait_event.tasklet);
2393 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2394 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002395 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002396 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002397 if (h1c->conn->owner == sess) {
2398 int ret = session_check_idle_conn(sess, h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002399 if (ret == -1) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002400 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002401 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2402 goto end;
2403 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002404 else if (ret == 1) {
2405 /* The connection was added to the server list,
2406 * wake the task so we can subscribe to events
2407 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002408 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002409 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2410 goto end;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002411 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002412 TRACE_DEVEL("connection in idle session list", H1_EV_STRM_END, h1c->conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002413 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002414 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002415 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002416 struct server *srv = objt_server(h1c->conn->target);
2417
2418 if (srv) {
2419 if (h1c->conn->flags & CO_FL_PRIVATE)
2420 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002421 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002422 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2423 else
2424 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002425 TRACE_DEVEL("connection in idle server list", H1_EV_STRM_END, h1c->conn);
Christopher Faulet9400a392018-11-23 23:10:39 +01002426 }
2427 }
2428 }
2429
Christopher Fauletf1204b82019-07-19 14:51:06 +02002430 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002431 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet37243bc2019-07-11 15:40:25 +02002432 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2433 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2434 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002435 !h1c->conn->owner) {
2436 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002437 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002438 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002439 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002440 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002441 if (h1c->task) {
2442 h1c->task->expire = TICK_ETERNITY;
2443 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002444 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002445 ? h1c->shut_timeout
2446 : h1c->timeout));
2447 task_queue(h1c->task);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002448 TRACE_DEVEL("refreshing connection's timeout", H1_EV_STRM_END, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002449 }
2450 }
2451 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002452 end:
2453 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002454}
2455
2456
2457static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2458{
2459 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002460 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002461
2462 if (!h1s)
2463 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002464 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002465
Christopher Faulet6b81df72019-10-01 22:08:43 +02002466 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2467
2468 if (cs->flags & CS_FL_KILL_CONN) {
2469 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2470 goto do_shutr;
2471 }
2472 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2473 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002474 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002475 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002476
Christopher Faulet6b81df72019-10-01 22:08:43 +02002477 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2478 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2479 goto end;
2480 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481
Christopher Faulet7f366362019-04-08 10:51:20 +02002482 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002483 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2484 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002485 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002486 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002487 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002488 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002489 end:
2490 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002491}
2492
2493static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2494{
2495 struct h1s *h1s = cs->ctx;
2496 struct h1c *h1c;
2497
2498 if (!h1s)
2499 return;
2500 h1c = h1s->h1c;
2501
Christopher Faulet6b81df72019-10-01 22:08:43 +02002502 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2503
2504 if (cs->flags & CS_FL_KILL_CONN) {
2505 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002506 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002507 }
2508 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2509 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2510 goto do_shutw;
2511 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002512
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002513 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002514 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2515 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2516 goto end;
2517 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002518
Christopher Faulet7f366362019-04-08 10:51:20 +02002519 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002520 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2521 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002522 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002523 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002524 end:
2525 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002526}
2527
Christopher Faulet666a0c42019-01-08 11:12:04 +01002528static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002529{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002530 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002531
Christopher Faulet6b81df72019-10-01 22:08:43 +02002532 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002533 conn_xprt_shutw(conn);
2534 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002535 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002536 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002537}
2538
2539/* Called from the upper layer, to unsubscribe to events */
2540static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2541{
2542 struct wait_event *sw;
2543 struct h1s *h1s = cs->ctx;
2544
2545 if (!h1s)
2546 return 0;
2547
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002548 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002549 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002550 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002551 BUG_ON(h1s->recv_wait != sw);
2552 sw->events &= ~SUB_RETRY_RECV;
2553 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002554 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002555 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002556 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002557 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002558 BUG_ON(h1s->send_wait != sw);
2559 sw->events &= ~SUB_RETRY_SEND;
2560 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002561 }
2562 return 0;
2563}
2564
2565/* Called from the upper layer, to subscribe to events, such as being able to send */
2566static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2567{
2568 struct wait_event *sw;
2569 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002570 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002571
2572 if (!h1s)
2573 return -1;
2574
Christopher Faulet6b81df72019-10-01 22:08:43 +02002575 if (event_type & SUB_RETRY_RECV) {
2576 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
2577 sw = param;
2578 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2579 sw->events |= SUB_RETRY_RECV;
2580 h1s->recv_wait = sw;
2581 event_type &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002582 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002583 if (event_type & SUB_RETRY_SEND) {
2584 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
2585 sw = param;
2586 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2587 sw->events |= SUB_RETRY_SEND;
2588 h1s->send_wait = sw;
2589 event_type &= ~SUB_RETRY_SEND;
2590 /*
2591 * If the conn_stream attempt to subscribe, and the
2592 * mux isn't subscribed to the connection, then it
2593 * probably means the connection wasn't established
2594 * yet, so we have to subscribe.
2595 */
2596 h1c = h1s->h1c;
2597 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2598 h1c->conn->xprt->subscribe(h1c->conn,
2599 h1c->conn->xprt_ctx,
2600 SUB_RETRY_SEND,
2601 &h1c->wait_event);
2602 }
2603 if (event_type != 0)
2604 return -1;
2605 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002606}
2607
2608/* Called from the upper layer, to receive data */
2609static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2610{
2611 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002612 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002613 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002614 size_t ret = 0;
2615
Christopher Faulet6b81df72019-10-01 22:08:43 +02002616 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002617 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002618 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002619 else
2620 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002621
Christopher Faulete18777b2019-04-16 16:46:36 +02002622 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002623 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002624 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002625 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2626 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002627 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002628 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002629 if (h1s->flags & H1S_F_SPLICED_DATA) {
2630 h1s->flags &= ~H1S_F_SPLICED_DATA;
2631 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2632 }
2633 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002634 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002635 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002636 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002637 return ret;
2638}
2639
2640
2641/* Called from the upper layer, to send data */
2642static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2643{
2644 struct h1s *h1s = cs->ctx;
2645 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002646 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002647
2648 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002649 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002650 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002651
Christopher Faulet6b81df72019-10-01 22:08:43 +02002652 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){count});
2653
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002654 /* If we're not connected yet, or we're waiting for a handshake, stop
2655 * now, as we don't want to remove everything from the channel buffer
2656 * before we're sure we can send it.
2657 */
2658 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002659 (h1c->conn->flags & CO_FL_HANDSHAKE)) {
2660 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002661 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002662 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002663
Christopher Fauletc31872f2019-06-04 22:09:36 +02002664 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002665 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002666
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002667 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2668 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002669 else
2670 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002671 if (!ret)
2672 break;
2673 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002674 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002675 if (!h1_send(h1c))
2676 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002677 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002678
Christopher Faulet6b81df72019-10-01 22:08:43 +02002679 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002680 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002681}
2682
Willy Tarreaue5733232019-05-22 19:24:06 +02002683#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002684/* Send and get, using splicing */
2685static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2686{
2687 struct h1s *h1s = cs->ctx;
2688 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2689 int ret = 0;
2690
Christopher Faulet6b81df72019-10-01 22:08:43 +02002691 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){count});
2692
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002693 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002694 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002695 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2696 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2697 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002698 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002699 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002700 goto end;
2701 }
2702
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002703 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002704 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002705 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002706 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002707 }
2708
2709 h1s->flags &= ~H1S_F_BUF_FLUSH;
2710 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002711 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002712 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2713 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002714 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002715 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002716 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002717 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002718 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002719 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2720 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002721 }
2722
Christopher Faulet1be55f92018-10-02 15:59:23 +02002723 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002724 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002725 h1s->flags |= H1S_F_REOS;
Christopher Fauletf3158e92019-11-15 11:14:23 +01002726 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002727 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002728 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002729
2730 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002731 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002732}
2733
2734static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2735{
2736 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002737 int ret = 0;
2738
Christopher Faulet6b81df72019-10-01 22:08:43 +02002739 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){pipe->data});
2740
Christopher Faulet1be55f92018-10-02 15:59:23 +02002741 if (b_data(&h1s->h1c->obuf))
2742 goto end;
2743
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002744 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002745 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002746 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002747 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2748 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002749 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002750 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002751 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002752
2753 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002754 return ret;
2755}
2756#endif
2757
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002758static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2759{
2760 int ret = 0;
2761 switch (mux_ctl) {
2762 case MUX_STATUS:
2763 if (conn->flags & CO_FL_CONNECTED)
2764 ret |= MUX_STATUS_READY;
2765 return ret;
2766 default:
2767 return -1;
2768 }
2769}
2770
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002771/* for debugging with CLI's "show fd" command */
2772static void h1_show_fd(struct buffer *msg, struct connection *conn)
2773{
2774 struct h1c *h1c = conn->ctx;
2775 struct h1s *h1s = h1c->h1s;
2776
Christopher Fauletf376a312019-01-04 15:16:06 +01002777 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2778 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002779 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2780 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2781 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2782 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2783
2784 if (h1s) {
2785 char *method;
2786
2787 if (h1s->meth < HTTP_METH_OTHER)
2788 method = http_known_methods[h1s->meth].ptr;
2789 else
2790 method = "UNKNOWN";
2791 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2792 " .meth=%s status=%d",
2793 h1s, h1s->flags,
2794 h1m_state_str(h1s->req.state),
2795 h1m_state_str(h1s->res.state), method, h1s->status);
2796 if (h1s->cs)
2797 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2798 h1s->cs->flags, h1s->cs->data);
2799 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002800}
2801
2802
2803/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2804static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2805{
2806 struct h1_hdr_entry *entry;
2807
2808 /* Be sure there is a non-empty <to> */
2809 if (!strlen(to)) {
2810 memprintf(err, "expect <to>");
2811 return -1;
2812 }
2813
2814 /* Be sure only the case differs between <from> and <to> */
2815 if (strcasecmp(from, to)) {
2816 memprintf(err, "<from> and <to> must not differ execpt the case");
2817 return -1;
2818 }
2819
2820 /* Be sure <from> does not already existsin the tree */
2821 if (ebis_lookup(&hdrs_map.map, from)) {
2822 memprintf(err, "duplicate entry '%s'", from);
2823 return -1;
2824 }
2825
2826 /* Create the entry and insert it in the tree */
2827 entry = malloc(sizeof(*entry));
2828 if (!entry) {
2829 memprintf(err, "out of memory");
2830 return -1;
2831 }
2832
2833 entry->node.key = strdup(from);
2834 entry->name.ptr = strdup(to);
2835 entry->name.len = strlen(to);
2836 if (!entry->node.key || !entry->name.ptr) {
2837 free(entry->node.key);
2838 free(entry->name.ptr);
2839 free(entry);
2840 memprintf(err, "out of memory");
2841 return -1;
2842 }
2843 ebis_insert(&hdrs_map.map, &entry->node);
2844 return 0;
2845}
2846
2847static void h1_hdeaders_case_adjust_deinit()
2848{
2849 struct ebpt_node *node, *next;
2850 struct h1_hdr_entry *entry;
2851
2852 node = ebpt_first(&hdrs_map.map);
2853 while (node) {
2854 next = ebpt_next(node);
2855 ebpt_delete(node);
2856 entry = container_of(node, struct h1_hdr_entry, node);
2857 free(entry->node.key);
2858 free(entry->name.ptr);
2859 free(entry);
2860 node = next;
2861 }
2862 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002863}
2864
Christopher Faulet98fbe952019-07-22 16:18:24 +02002865static int cfg_h1_headers_case_adjust_postparser()
2866{
2867 FILE *file = NULL;
2868 char *c, *key_beg, *key_end, *value_beg, *value_end;
2869 char *err;
2870 int rc, line = 0, err_code = 0;
2871
2872 if (!hdrs_map.name)
2873 goto end;
2874
2875 file = fopen(hdrs_map.name, "r");
2876 if (!file) {
2877 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2878 hdrs_map.name);
2879 err_code |= ERR_ALERT | ERR_FATAL;
2880 goto end;
2881 }
2882
2883 /* now parse all lines. The file may contain only two header name per
2884 * line, separated by spaces. All heading and trailing spaces will be
2885 * ignored. Lines starting with a # are ignored.
2886 */
2887 while (fgets(trash.area, trash.size, file) != NULL) {
2888 line++;
2889 c = trash.area;
2890
2891 /* strip leading spaces and tabs */
2892 while (*c == ' ' || *c == '\t')
2893 c++;
2894
2895 /* ignore emptu lines, or lines beginning with a dash */
2896 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2897 continue;
2898
2899 /* look for the end of the key */
2900 key_beg = c;
2901 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2902 c++;
2903 key_end = c;
2904
2905 /* strip middle spaces and tabs */
2906 while (*c == ' ' || *c == '\t')
2907 c++;
2908
2909 /* look for the end of the value, it is the end of the line */
2910 value_beg = c;
2911 while (*c && *c != '\n' && *c != '\r')
2912 c++;
2913 value_end = c;
2914
2915 /* trim possibly trailing spaces and tabs */
2916 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2917 value_end--;
2918
2919 /* set final \0 and check entries */
2920 *key_end = '\0';
2921 *value_end = '\0';
2922
2923 err = NULL;
2924 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2925 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002926 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2927 hdrs_map.name, err, line);
2928 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002929 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002930 goto end;
2931 }
2932 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002933 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2934 hdrs_map.name, err, line);
2935 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002936 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002937 }
2938 }
2939
2940 end:
2941 if (file)
2942 fclose(file);
2943 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2944 return err_code;
2945}
2946
2947
2948/* config parser for global "h1-outgoing-header-case-adjust" */
2949static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2950 struct proxy *defpx, const char *file, int line,
2951 char **err)
2952{
2953 if (too_many_args(2, args, err, NULL))
2954 return -1;
2955 if (!*(args[1]) || !*(args[2])) {
2956 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2957 return -1;
2958 }
2959 return add_hdr_case_adjust(args[1], args[2], err);
2960}
2961
2962/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2963static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2964 struct proxy *defpx, const char *file, int line,
2965 char **err)
2966{
2967 if (too_many_args(1, args, err, NULL))
2968 return -1;
2969 if (!*(args[1])) {
2970 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2971 return -1;
2972 }
2973 free(hdrs_map.name);
2974 hdrs_map.name = strdup(args[1]);
2975 return 0;
2976}
2977
2978
2979/* config keyword parsers */
2980static struct cfg_kw_list cfg_kws = {{ }, {
2981 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2982 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2983 { 0, NULL, NULL },
2984 }
2985};
2986
2987INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2988REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2989
2990
Christopher Faulet51dbc942018-09-13 09:05:15 +02002991/****************************************/
2992/* MUX initialization and instanciation */
2993/****************************************/
2994
2995/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002996static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002997 .init = h1_init,
2998 .wake = h1_wake,
2999 .attach = h1_attach,
3000 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01003001 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003002 .detach = h1_detach,
3003 .destroy = h1_destroy,
3004 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003005 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003006 .rcv_buf = h1_rcv_buf,
3007 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003008#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003009 .rcv_pipe = h1_rcv_pipe,
3010 .snd_pipe = h1_snd_pipe,
3011#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003012 .subscribe = h1_subscribe,
3013 .unsubscribe = h1_unsubscribe,
3014 .shutr = h1_shutr,
3015 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003016 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003017 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003018 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003019 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003020 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003021};
3022
3023
3024/* this mux registers default HTX proto */
3025static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003026{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003027
Willy Tarreau0108d902018-11-25 19:14:37 +01003028INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3029
Christopher Faulet51dbc942018-09-13 09:05:15 +02003030/*
3031 * Local variables:
3032 * c-indent-level: 8
3033 * c-basic-offset: 8
3034 * End:
3035 */