blob: 1f296fddc292e60c2999095d3e979b52c9a66619 [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 Fauletcb55f482018-12-10 11:56:47 +010047#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010048/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
50#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
51#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010052#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +010053#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
54 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020055
Christopher Fauletf2824e62018-10-01 12:12:37 +020056#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 +020057#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020058
Christopher Faulet51dbc942018-09-13 09:05:15 +020059/*
60 * H1 Stream flags (32 bits)
61 */
Christopher Faulet129817b2018-09-20 16:14:40 +020062#define H1S_F_NONE 0x00000000
63#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020064#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
65#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020066#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020067#define H1S_F_WANT_KAL 0x00000010
68#define H1S_F_WANT_TUN 0x00000020
69#define H1S_F_WANT_CLO 0x00000040
70#define H1S_F_WANT_MSK 0x00000070
71#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010072#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010073#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010074#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +020075#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020076/* 0x00002000 .. 0x00001000 unused */
77#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/*
351 * Indicates whether or not the we may call the h1_recv() function to
352 * attempt to receive data into the buffer and/or parse pending data. The
353 * condition is a bit complex due to some API limits for now. The rules are the
354 * following :
355 * - if an error or a shutdown was detected on the connection and the buffer
356 * is empty, we must not attempt to receive
357 * - if the input buffer failed to be allocated, we must not try to receive
358 * and we know there is nothing pending
359 * - if no flag indicates a blocking condition, we may attempt to receive,
360 * regardless of whether the input buffer is full or not, so that only de
361 * receiving part decides whether or not to block. This is needed because
362 * the connection API indeed prevents us from re-enabling receipt that is
363 * already enabled in a polled state, so we must always immediately stop as
364 * soon as the mux can't proceed so as never to hit an end of read with data
365 * pending in the buffers.
366 * - otherwise must may not attempt to receive
367 */
368static inline int h1_recv_allowed(const struct h1c *h1c)
369{
Christopher Faulet6b81df72019-10-01 22:08:43 +0200370 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN))) {
371 TRACE_DEVEL("recv not allowed because of (error|shudown) on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200373 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374
Christopher Faulet6b81df72019-10-01 22:08:43 +0200375 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn)) {
376 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 +0100377 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200378 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100379
Christopher Fauletcb55f482018-12-10 11:56:47 +0100380 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381 return 1;
382
Christopher Faulet6b81df72019-10-01 22:08:43 +0200383 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 +0200384 return 0;
385}
386
387/*
388 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
389 * flags are used to figure what buffer was requested. It returns 1 if the
390 * allocation succeeds, in which case the connection is woken up, or 0 if it's
391 * impossible to wake up and we prefer to be woken up later.
392 */
393static int h1_buf_available(void *target)
394{
395 struct h1c *h1c = target;
396
397 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200398 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 +0200399 h1c->flags &= ~H1C_F_IN_ALLOC;
400 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200401 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200402 return 1;
403 }
404
405 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200406 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 +0200407 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200408 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200409 if (h1c->h1s)
410 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200411 return 1;
412 }
413
Christopher Faulet51dbc942018-09-13 09:05:15 +0200414 return 0;
415}
416
417/*
418 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
419 */
420static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
421{
422 struct buffer *buf = NULL;
423
424 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
425 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
426 h1c->buf_wait.target = h1c;
427 h1c->buf_wait.wakeup_cb = h1_buf_available;
428 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
429 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
430 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
431 __conn_xprt_stop_recv(h1c->conn);
432 }
433 return buf;
434}
435
436/*
437 * Release a buffer, if any, and try to wake up entities waiting in the buffer
438 * wait queue.
439 */
440static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
441{
442 if (bptr->size) {
443 b_free(bptr);
444 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
445 }
446}
447
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100448/* returns the number of streams in use on a connection to figure if it's idle
449 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
450 * not. This flag is only set when no H1S is attached and when the previous
451 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100452 */
453static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200454{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100455 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100457 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200458}
459
Willy Tarreau00f18a32019-01-26 12:19:01 +0100460/* returns the number of streams still available on a connection */
461static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100462{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100463 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100464}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200465
Willy Tarreau00f18a32019-01-26 12:19:01 +0100466
Christopher Faulet51dbc942018-09-13 09:05:15 +0200467/*****************************************************************/
468/* functions below are dedicated to the mux setup and management */
469/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200470
471/* returns non-zero if there are input data pending for stream h1s. */
472static inline size_t h1s_data_pending(const struct h1s *h1s)
473{
474 const struct h1m *h1m;
475
476 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
477 if (h1m->state == H1_MSG_DONE)
478 return 0; // data not for this stream (e.g. pipelining)
479
480 return b_data(&h1s->h1c->ibuf);
481}
482
Christopher Faulet47365272018-10-31 17:40:50 +0100483static struct conn_stream *h1s_new_cs(struct h1s *h1s)
484{
485 struct conn_stream *cs;
486
Christopher Faulet6b81df72019-10-01 22:08:43 +0200487 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100488 cs = cs_new(h1s->h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200489 if (!cs) {
490 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 +0100491 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200492 }
Christopher Faulet47365272018-10-31 17:40:50 +0100493 h1s->cs = cs;
494 cs->ctx = h1s;
495
496 if (h1s->flags & H1S_F_NOT_FIRST)
497 cs->flags |= CS_FL_NOT_FIRST;
498
Christopher Faulet6b81df72019-10-01 22:08:43 +0200499 if (stream_create_from_cs(cs) < 0) {
500 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 +0100501 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200502 }
503
504 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100505 return cs;
506
507 err:
508 cs_free(cs);
509 h1s->cs = NULL;
510 return NULL;
511}
512
Olivier Houchardf502aca2018-12-14 19:42:40 +0100513static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200514{
515 struct h1s *h1s;
516
Christopher Faulet6b81df72019-10-01 22:08:43 +0200517 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
518
Christopher Faulet51dbc942018-09-13 09:05:15 +0200519 h1s = pool_alloc(pool_head_h1s);
520 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100521 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200522
523 h1s->h1c = h1c;
524 h1c->h1s = h1s;
525
Olivier Houchardf502aca2018-12-14 19:42:40 +0100526 h1s->sess = sess;
527
Christopher Faulet51dbc942018-09-13 09:05:15 +0200528 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100529 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200530
531 h1s->recv_wait = NULL;
532 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200533
534 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100535 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200536
Christopher Faulet129817b2018-09-20 16:14:40 +0200537 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100538 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200539
540 h1s->status = 0;
541 h1s->meth = HTTP_METH_OTHER;
542
Christopher Faulet47365272018-10-31 17:40:50 +0100543 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
544 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100545 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100546
Christopher Faulet129817b2018-09-20 16:14:40 +0200547 if (!conn_is_back(h1c->conn)) {
548 if (h1c->px->options2 & PR_O2_REQBUG_OK)
549 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200550
551 /* For frontend connections we should always have a session */
552 if (!sess)
553 sess = h1c->conn->owner;
554
Dave Pirotte234740f2019-07-10 13:57:38 +0000555 /* Timers for subsequent sessions on the same HTTP 1.x connection
556 * measure from `now`, not from the connection accept time */
557 if (h1s->flags & H1S_F_NOT_FIRST) {
558 h1s->csinfo.create_date = date;
559 h1s->csinfo.tv_create = now;
560 h1s->csinfo.t_handshake = 0;
561 h1s->csinfo.t_idle = -1;
562 }
563 else {
564 h1s->csinfo.create_date = sess->accept_date;
565 h1s->csinfo.tv_create = sess->tv_accept;
566 h1s->csinfo.t_handshake = sess->t_handshake;
567 h1s->csinfo.t_idle = -1;
568 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200569 }
570 else {
571 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
572 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200573
Christopher Fauletfeb11742018-11-29 15:12:34 +0100574 h1s->csinfo.create_date = date;
575 h1s->csinfo.tv_create = now;
576 h1s->csinfo.t_handshake = 0;
577 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200578 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100579
Christopher Faulete9b70722019-04-08 10:46:02 +0200580 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
581 * create a new one.
582 */
583 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200584 cs->ctx = h1s;
585 h1s->cs = cs;
586 }
Christopher Faulet47365272018-10-31 17:40:50 +0100587 else {
588 cs = h1s_new_cs(h1s);
589 if (!cs)
590 goto fail;
591 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200592 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200593 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100594
595 fail:
596 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200597 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 +0100598 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200599}
600
601static void h1s_destroy(struct h1s *h1s)
602{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200603 if (h1s) {
604 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200605
Christopher Faulet6b81df72019-10-01 22:08:43 +0200606 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200607 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200608
Christopher Fauletf2824e62018-10-01 12:12:37 +0200609 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100610 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200611 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100612 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200613
Christopher Fauletcb55f482018-12-10 11:56:47 +0100614 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200615 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100616 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200617 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
618 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100619
620 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
621 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
622 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
623 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
624 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
625 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
626 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200627 pool_free(pool_head_h1s, h1s);
628 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200629}
630
Christopher Fauletfeb11742018-11-29 15:12:34 +0100631static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
632{
633 struct h1s *h1s = cs->ctx;
634
635 if (h1s && !conn_is_back(cs->conn))
636 return &h1s->csinfo;
637 return NULL;
638}
639
Christopher Faulet51dbc942018-09-13 09:05:15 +0200640/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200641 * Initialize the mux once it's attached. It is expected that conn->ctx points
642 * to the existing conn_stream (for outgoing connections or for incoming onces
643 * during a mux upgrade) or NULL (for incoming ones during the connexion
644 * establishment). <input> is always used as Input buffer and may contain
645 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
646 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200647 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200648static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
649 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200650{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200651 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100652 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200653 void *conn_ctx = conn->ctx;
654
655 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200656
657 h1c = pool_alloc(pool_head_h1c);
658 if (!h1c)
659 goto fail_h1c;
660 h1c->conn = conn;
661 h1c->px = proxy;
662
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100663 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200664 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200665 h1c->obuf = BUF_NULL;
666 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200667 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200668
Christopher Faulet51dbc942018-09-13 09:05:15 +0200669 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200670 h1c->wait_event.tasklet = tasklet_new();
671 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200672 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200673 h1c->wait_event.tasklet->process = h1_io_cb;
674 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100675 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200676
Christopher Faulete9b70722019-04-08 10:46:02 +0200677 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100678 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
679 if (tick_isset(proxy->timeout.serverfin))
680 h1c->shut_timeout = proxy->timeout.serverfin;
681 } else {
682 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
683 if (tick_isset(proxy->timeout.clientfin))
684 h1c->shut_timeout = proxy->timeout.clientfin;
685 }
686 if (tick_isset(h1c->timeout)) {
687 t = task_new(tid_bit);
688 if (!t)
689 goto fail;
690
691 h1c->task = t;
692 t->process = h1_timeout_task;
693 t->context = h1c;
694 t->expire = tick_add(now_ms, h1c->timeout);
695 }
696
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100697 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200698
Christopher Faulet6b81df72019-10-01 22:08:43 +0200699 /* Always Create a new H1S */
700 if (!h1s_create(h1c, conn_ctx, sess))
701 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100702
703 if (t)
704 task_queue(t);
705
Christopher Faulet51dbc942018-09-13 09:05:15 +0200706 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200707 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200708
709 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200710 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200711 return 0;
712
713 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200714 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200715 if (h1c->wait_event.tasklet)
716 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200717 pool_free(pool_head_h1c, h1c);
718 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200719 conn->ctx = conn_ctx; // restore saved context
720 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200721 return -1;
722}
723
Christopher Faulet73c12072019-04-08 11:23:22 +0200724/* release function. This one should be called to free all resources allocated
725 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200726 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200727static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200728{
Christopher Faulet61840e72019-04-15 09:33:32 +0200729 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200730
Christopher Faulet6b81df72019-10-01 22:08:43 +0200731 TRACE_POINT(H1_EV_H1C_END);
732
Christopher Faulet51dbc942018-09-13 09:05:15 +0200733 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200734 /* The connection must be aattached to this mux to be released */
735 if (h1c->conn && h1c->conn->ctx == h1c)
736 conn = h1c->conn;
737
Christopher Faulet6b81df72019-10-01 22:08:43 +0200738 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
739
Christopher Faulet61840e72019-04-15 09:33:32 +0200740 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200741 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200742 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200743 /* Make sure we're no longer subscribed to anything */
744 if (h1c->wait_event.events)
745 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
746 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200747 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200748 /* connection successfully upgraded to H2, this
749 * mux was already released */
750 return;
751 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200752 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200753 sess_log(conn->owner); /* Log if the upgrade failed */
754 }
755
Olivier Houchard45c44372019-06-11 14:06:23 +0200756
Christopher Faulet51dbc942018-09-13 09:05:15 +0200757 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
758 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
759 LIST_DEL(&h1c->buf_wait.list);
760 LIST_INIT(&h1c->buf_wait.list);
761 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
762 }
763
764 h1_release_buf(h1c, &h1c->ibuf);
765 h1_release_buf(h1c, &h1c->obuf);
766
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100767 if (h1c->task) {
768 h1c->task->context = NULL;
769 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
770 h1c->task = NULL;
771 }
772
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200773 if (h1c->wait_event.tasklet)
774 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200775
Christopher Fauletf2824e62018-10-01 12:12:37 +0200776 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200777 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100778 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200779 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200780 pool_free(pool_head_h1c, h1c);
781 }
782
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200783 if (conn) {
784 conn->mux = NULL;
785 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200786 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200787
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200788 conn_stop_tracking(conn);
789 conn_full_close(conn);
790 if (conn->destroy_cb)
791 conn->destroy_cb(conn);
792 conn_free(conn);
793 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200794}
795
796/******************************************************/
797/* functions below are for the H1 protocol processing */
798/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200799/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
800 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200801 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100802static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200803{
Christopher Faulet570d1612018-11-26 11:13:57 +0100804 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200805
Christopher Faulet570d1612018-11-26 11:13:57 +0100806 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200807 (*(p + 5) > '1' ||
808 (*(p + 5) == '1' && *(p + 7) >= '1')))
809 h1m->flags |= H1_MF_VER_11;
810}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200811
Christopher Faulet9768c262018-10-22 09:34:31 +0200812/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
813 * greater or equal to 1.1
814 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100815static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200816{
Christopher Faulet570d1612018-11-26 11:13:57 +0100817 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200818
Christopher Faulet570d1612018-11-26 11:13:57 +0100819 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200820 (*(p + 5) > '1' ||
821 (*(p + 5) == '1' && *(p + 7) >= '1')))
822 h1m->flags |= H1_MF_VER_11;
823}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200824
Christopher Fauletf2824e62018-10-01 12:12:37 +0200825/* Deduce the connection mode of the client connection, depending on the
826 * configuration and the H1 message flags. This function is called twice, the
827 * first time when the request is parsed and the second time when the response
828 * is parsed.
829 */
830static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
831{
832 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200833
834 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100835 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200836 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100837 h1s->status == 101) {
838 /* Either we've established an explicit tunnel, or we're
839 * switching the protocol. In both cases, we're very unlikely to
840 * understand the next protocols. We have to switch to tunnel
841 * mode, so that we transfer the request and responses then let
842 * this protocol pass unmodified. When we later implement
843 * specific parsers for such protocols, we'll want to check the
844 * Upgrade header which contains information about that protocol
845 * for responses with status 101 (eg: see RFC2817 about TLS).
846 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200847 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200848 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 +0100849 }
850 else if (h1s->flags & H1S_F_WANT_KAL) {
851 /* By default the client is in KAL mode. CLOSE mode mean
852 * it is imposed by the client itself. So only change
853 * KAL mode here. */
854 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
855 /* no length known or explicit close => close */
856 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200857 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 +0100858 }
859 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
860 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
861 /* no explict keep-alive and option httpclose => close */
862 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200863 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 +0100864 }
865 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200866 }
867 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100868 /* Input direction: first pass */
869 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
870 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200871 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200872 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 +0100873 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200874 }
875
876 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200877 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200879 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);
880 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200881}
882
883/* Deduce the connection mode of the client connection, depending on the
884 * configuration and the H1 message flags. This function is called twice, the
885 * first time when the request is parsed and the second time when the response
886 * is parsed.
887 */
888static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
889{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100890 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100891 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100892 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200893
Christopher Fauletf2824e62018-10-01 12:12:37 +0200894 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100895 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200896 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100897 h1s->status == 101) {
898 /* Either we've established an explicit tunnel, or we're
899 * switching the protocol. In both cases, we're very unlikely to
900 * understand the next protocols. We have to switch to tunnel
901 * mode, so that we transfer the request and responses then let
902 * this protocol pass unmodified. When we later implement
903 * specific parsers for such protocols, we'll want to check the
904 * Upgrade header which contains information about that protocol
905 * for responses with status 101 (eg: see RFC2817 about TLS).
906 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200907 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200908 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 +0100909 }
910 else if (h1s->flags & H1S_F_WANT_KAL) {
911 /* By default the server is in KAL mode. CLOSE mode mean
912 * it is imposed by haproxy itself. So only change KAL
913 * mode here. */
914 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
915 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
916 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
917 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200918 TRACE_STATE("detect close mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100919 }
920 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200921 }
Christopher Faulet70033782018-12-05 13:50:11 +0100922 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100923 /* Output direction: first pass */
924 if (h1m->flags & H1_MF_CONN_CLO) {
925 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100926 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200927 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 +0100928 }
929 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
930 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
931 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
932 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
933 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
934 /* no explicit keep-alive option httpclose/server-close => close */
935 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200936 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 +0100937 }
Christopher Faulet70033782018-12-05 13:50:11 +0100938 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200939
940 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200941 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200942 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200943 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);
944 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200945}
946
Christopher Fauletb992af02019-03-28 15:42:24 +0100947static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200948{
949 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200950
951 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
952 * token is found
953 */
954 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200955 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200956
957 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200958 if (!(h1m->flags & H1_MF_VER_11)) {
959 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 +0100960 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200961 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200962 }
963 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200964 if (h1m->flags & H1_MF_VER_11) {
965 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100966 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200967 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200968 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200969}
970
Christopher Fauletb992af02019-03-28 15:42:24 +0100971static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200972{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200973 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
974 * token is found
975 */
976 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200977 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200978
979 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100980 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +0200981 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
982 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 +0100983 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200984 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200985 }
986 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200987 if (h1m->flags & H1_MF_VER_11) {
988 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100989 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200990 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200991 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200992}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200993
Christopher Fauletb992af02019-03-28 15:42:24 +0100994static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200995{
Christopher Fauletb992af02019-03-28 15:42:24 +0100996 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200997 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100998 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200999 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001000}
1001
Christopher Fauletb992af02019-03-28 15:42:24 +01001002static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1003{
1004 if (!conn_is_back(h1s->h1c->conn))
1005 h1_set_cli_conn_mode(h1s, h1m);
1006 else
1007 h1_set_srv_conn_mode(h1s, h1m);
1008
1009 if (!(h1m->flags & H1_MF_RESP))
1010 h1_update_req_conn_value(h1s, h1m, conn_val);
1011 else
1012 h1_update_res_conn_value(h1s, h1m, conn_val);
1013}
Christopher Faulete44769b2018-11-29 23:01:45 +01001014
Christopher Faulet98fbe952019-07-22 16:18:24 +02001015/* Try to adjust the case of the message header name using the global map
1016 * <hdrs_map>.
1017 */
1018static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1019{
1020 struct ebpt_node *node;
1021 struct h1_hdr_entry *entry;
1022
1023 /* No entry in the map, do nothing */
1024 if (eb_is_empty(&hdrs_map.map))
1025 return;
1026
1027 /* No conversion fo the request headers */
1028 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1029 return;
1030
1031 /* No conversion fo the response headers */
1032 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1033 return;
1034
1035 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1036 if (!node)
1037 return;
1038 entry = container_of(node, struct h1_hdr_entry, node);
1039 name->ptr = entry->name.ptr;
1040 name->len = entry->name.len;
1041}
1042
Christopher Faulete44769b2018-11-29 23:01:45 +01001043/* Append the description of what is present in error snapshot <es> into <out>.
1044 * The description must be small enough to always fit in a buffer. The output
1045 * buffer may be the trash so the trash must not be used inside this function.
1046 */
1047static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1048{
1049 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001050 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1051 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1052 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1053 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1054 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1055 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001056}
1057/*
1058 * Capture a bad request or response and archive it in the proxy's structure.
1059 * By default it tries to report the error position as h1m->err_pos. However if
1060 * this one is not set, it will then report h1m->next, which is the last known
1061 * parsing point. The function is able to deal with wrapping buffers. It always
1062 * displays buffers as a contiguous area starting at buf->p. The direction is
1063 * determined thanks to the h1m's flags.
1064 */
1065static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1066 struct h1m *h1m, struct buffer *buf)
1067{
1068 struct session *sess = h1c->conn->owner;
1069 struct proxy *proxy = h1c->px;
1070 struct proxy *other_end = sess->fe;
1071 union error_snapshot_ctx ctx;
1072
Willy Tarreau598d7fc2018-12-18 18:10:38 +01001073 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +01001074 other_end = si_strm(h1s->cs->data)->be;
1075
1076 /* http-specific part now */
1077 ctx.h1.state = h1m->state;
1078 ctx.h1.c_flags = h1c->flags;
1079 ctx.h1.s_flags = h1s->flags;
1080 ctx.h1.m_flags = h1m->flags;
1081 ctx.h1.m_clen = h1m->curr_len;
1082 ctx.h1.m_blen = h1m->body_len;
1083
1084 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1085 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001086 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1087 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001088}
1089
Christopher Fauletadb22202018-12-12 10:32:09 +01001090/* Emit the chunksize followed by a CRLF in front of data of the buffer
1091 * <buf>. It goes backwards and starts with the byte before the buffer's
1092 * head. The caller is responsible for ensuring there is enough room left before
1093 * the buffer's head for the string.
1094 */
1095static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1096{
1097 char *beg, *end;
1098
1099 beg = end = b_head(buf);
1100 *--beg = '\n';
1101 *--beg = '\r';
1102 do {
1103 *--beg = hextab[chksz & 0xF];
1104 } while (chksz >>= 4);
1105 buf->head -= (end - beg);
1106 b_add(buf, end - beg);
1107}
1108
1109/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1110 * ensuring there is enough room left in the buffer for the string. */
1111static void h1_emit_chunk_crlf(struct buffer *buf)
1112{
1113 *(b_peek(buf, b_data(buf))) = '\r';
1114 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1115 b_add(buf, 2);
1116}
1117
Christopher Faulet129817b2018-09-20 16:14:40 +02001118/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001119 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001120 * CONNECT requests. On the client side, if the response is not finished, the
1121 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001122 */
1123static void h1_set_req_tunnel_mode(struct h1s *h1s)
1124{
1125 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1126 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001127 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1128
1129 if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001130 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001131 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1132 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001133 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1134 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1135 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1136 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1137 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001138}
1139
1140/*
1141 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletf3158e92019-11-15 11:14:23 +01001142 * successfull replies to CONNECT requests or on protocol switching. In this
1143 * last case, this function takes care to switch the request to tunnel mode if
1144 * possible. On the server side, if the request is not finished, the mux is mark
1145 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001146 */
1147static void h1_set_res_tunnel_mode(struct h1s *h1s)
1148{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001149 /* On protocol switching, switch the request to tunnel mode if it is in
1150 * DONE state. Otherwise we will wait the end of the request to switch
1151 * it in tunnel mode.
1152 */
1153 if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1154 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1155 h1s->req.state = H1_MSG_TUNNEL;
1156 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1157 }
1158
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001159 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1160 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001161 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1162
Christopher Faulet6b81df72019-10-01 22:08:43 +02001163 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001164 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001165 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1166 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001167 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1168 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1169 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1170 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001171 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001172}
1173
1174/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001175 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001176 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001177 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001178 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001179static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001180 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001181{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001182 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001183 int ret = 0;
1184
Christopher Faulet6b81df72019-10-01 22:08:43 +02001185 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1186
Christopher Fauleteec96b52019-09-25 09:10:46 +02001187 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001188 /* Try to match H2 preface before parsing the request headers. */
1189 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001190 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001191 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001192 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001193 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001194 else {
1195 if (h1s->meth == HTTP_METH_CONNECT)
1196 h1m->flags |= H1_MF_METH_CONNECT;
1197 if (h1s->meth == HTTP_METH_HEAD)
1198 h1m->flags |= H1_MF_METH_HEAD;
1199 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001200
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001201 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1202 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001203 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 +02001204 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001205 if (!(h1m->flags & H1_MF_RESP)) {
1206 h1s->flags |= H1S_F_REQ_ERROR;
1207 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1208 }
1209 else {
1210 h1s->flags |= H1S_F_RES_ERROR;
1211 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1212 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001213 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001214 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 +02001215 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1216 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001217 goto end;
1218 }
1219
Christopher Faulet486498c2019-10-11 14:22:00 +02001220 if (h1m->err_pos >= 0) {
1221 /* Maybe we found an error during the parsing while we were
1222 * configured not to block on that, so we have to capture it
1223 * now.
1224 */
1225 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1226 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1227 }
1228
Christopher Faulet129817b2018-09-20 16:14:40 +02001229 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001230 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001231 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001232 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001233 }
1234 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001235 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001236 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001237 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001238 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001239 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001241
Christopher Faulet129817b2018-09-20 16:14:40 +02001242 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001243 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001244 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001245
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001246 h2c_upgrade:
1247 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001248 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001249 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001250 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1251 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001252}
1253
1254/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001255 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001256 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1257 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001258 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001259static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001260 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001261 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001262{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001263 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001264
Christopher Faulet6b81df72019-10-01 22:08:43 +02001265 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001266 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001267 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001268 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 +01001269 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001270 if (!(h1m->flags & H1_MF_RESP)) {
1271 h1s->flags |= H1S_F_REQ_ERROR;
1272 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1273 }
1274 else {
1275 h1s->flags |= H1S_F_RES_ERROR;
1276 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1277 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001278 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001279 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 +02001280 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001281 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001282 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001283 }
1284
Christopher Faulet02a02532019-11-15 09:36:28 +01001285 *ofs += ret;
1286
1287 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001288 if (h1m->state == H1_MSG_DONE) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001289 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001290 TRACE_STATE("end of message", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_RX_EOI, h1s->h1c->conn);
1291 }
1292
Christopher Faulet6b81df72019-10-01 22:08:43 +02001293 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001294 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001295}
1296
1297/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001298 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1299 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1300 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1301 * responsible to update the parser state <h1m>.
1302 */
1303static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1304 struct buffer *buf, size_t *ofs, size_t max)
1305{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001306 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001307
Christopher Faulet6b81df72019-10-01 22:08:43 +02001308 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001309 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001310 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001311 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 +01001312 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001313 if (!(h1m->flags & H1_MF_RESP)) {
1314 h1s->flags |= H1S_F_REQ_ERROR;
1315 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1316 }
1317 else {
1318 h1s->flags |= H1S_F_RES_ERROR;
1319 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1320 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001321 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001322 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 +02001323 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1324 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001325 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001326 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001327
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001328 *ofs += ret;
1329 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet02a02532019-11-15 09:36:28 +01001330
1331 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001332 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001333 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001334}
1335
1336/*
1337 * Add the EOM in the HTX message and switch the message to the DONE state. It
1338 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
1339 * functions is responsible to update the parser state <h1m>. It also add the
1340 * flag CS_FL_EOI on the CS.
1341 */
1342static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1343{
Christopher Faulet6b81df72019-10-01 22:08:43 +02001344 TRACE_ENTER(H1_EV_RX_DATA, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001345 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1346 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001347 TRACE_STATE("leaving on append_eom", H1_EV_RX_DATA, h1s->h1c->conn);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001348 return 0;
1349 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001350
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001351 h1s->flags &= ~H1S_F_APPEND_EOM;
1352 h1m->state = H1_MSG_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001353 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001354 TRACE_STATE("end of message", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1355 TRACE_LEAVE(H1_EV_RX_DATA, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001356 return (sizeof(struct htx_blk) + 1);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001357}
1358
1359/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001360 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001361 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1362 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001363 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001364static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001365{
Christopher Faulet539e0292018-11-19 10:40:09 +01001366 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001367 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001368 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001369 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001370 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001371 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001372
Christopher Faulet539e0292018-11-19 10:40:09 +01001373 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001374 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001375
Christopher Fauletf2824e62018-10-01 12:12:37 +02001376 if (!conn_is_back(h1c->conn)) {
1377 h1m = &h1s->req;
1378 errflag = H1S_F_REQ_ERROR;
1379 }
1380 else {
1381 h1m = &h1s->res;
1382 errflag = H1S_F_RES_ERROR;
1383 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001384
Christopher Faulet74027762019-02-26 14:45:05 +01001385 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001386 if (h1s->flags & errflag)
1387 goto end;
1388
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001389 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001390 size_t used = htx_used_space(htx);
1391
Christopher Faulet129817b2018-09-20 16:14:40 +02001392 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001393 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001394 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001395 if (!ret)
1396 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001397
1398 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1399 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1400
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001401 if ((h1m->flags & H1_MF_RESP) &&
1402 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1403 h1m_init_res(&h1s->res);
1404 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001405 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001406 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001407 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001408 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001409 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001410 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001411 if (!ret)
1412 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001413
1414 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1415 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
1416
1417 if (h1m->state == H1_MSG_DONE)
1418 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1419 H1_EV_RX_DATA, h1c->conn, h1s, htx);
Christopher Faulet129817b2018-09-20 16:14:40 +02001420 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001421 else if (h1m->state == H1_MSG_TRAILERS) {
1422 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001423 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001424 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1425 if (!ret)
1426 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001427
1428 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1429 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001430 }
Christopher Fauletf1ef7f62019-09-03 21:55:14 +02001431 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001432 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001433
1434 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1435 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001436 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001437 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletf3158e92019-11-15 11:14:23 +01001438 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1439 h1_set_req_tunnel_mode(h1s);
1440 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001441 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001442 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1443 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001444 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001445 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001446 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001447 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001448 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001449 if (!ret)
1450 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001451
1452 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1453 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001454 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001456 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001457 break;
1458 }
1459
Christopher Faulet30db3d72019-05-17 15:35:33 +02001460 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001461 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001462
Christopher Faulet6b81df72019-10-01 22:08:43 +02001463 if (h1s->flags & errflag) {
1464 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001465 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001466 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001467
Christopher Faulet539e0292018-11-19 10:40:09 +01001468 b_del(&h1c->ibuf, total);
1469
1470 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001471 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001472 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001473 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001474 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001475 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 +02001476 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001477 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001478
Christopher Fauletcf56b992018-12-11 16:12:31 +01001479 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1480
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001481 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001482 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001483 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001484 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001485
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001486 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1487 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001488 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001489 if (h1m->state == H1_MSG_TUNNEL)
1490 h1s->cs->flags |= CS_FL_EOI;
1491 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001492 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001493 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001494
Christopher Faulet6b81df72019-10-01 22:08:43 +02001495 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001496 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001497
1498 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001499 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001500 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001501 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001502 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001503}
1504
Christopher Faulet129817b2018-09-20 16:14:40 +02001505/*
1506 * Process outgoing data. It parses data and transfer them from the channel buffer into
1507 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1508 * 0 if it couldn't proceed.
1509 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001510static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1511{
Christopher Faulet129817b2018-09-20 16:14:40 +02001512 struct h1s *h1s = h1c->h1s;
1513 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001514 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001515 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001516 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001517 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001518 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001519
Christopher Faulet47365272018-10-31 17:40:50 +01001520 if (!count)
1521 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001522
Christopher Faulet94b2c762019-05-24 15:28:57 +02001523 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001524 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1525
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001526 if (htx_is_empty(chn_htx))
1527 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001528
Christopher Faulet51dbc942018-09-13 09:05:15 +02001529 if (!h1_get_buf(h1c, &h1c->obuf)) {
1530 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001531 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 +02001532 goto end;
1533 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001534
Christopher Fauletf2824e62018-10-01 12:12:37 +02001535 if (!conn_is_back(h1c->conn)) {
1536 h1m = &h1s->res;
1537 errflag = H1S_F_RES_ERROR;
1538 }
1539 else {
1540 h1m = &h1s->req;
1541 errflag = H1S_F_REQ_ERROR;
1542 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001543
Christopher Faulet0e54d542019-07-04 21:22:34 +02001544 if (h1s->flags & errflag)
1545 goto end;
1546
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001547 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001548 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001549
Willy Tarreau3815b222018-12-11 19:50:43 +01001550 /* Perform some optimizations to reduce the number of buffer copies.
1551 * First, if the mux's buffer is empty and the htx area contains
1552 * exactly one data block of the same size as the requested count,
1553 * then it's possible to simply swap the caller's buffer with the
1554 * mux's output buffer and adjust offsets and length to match the
1555 * entire DATA HTX block in the middle. In this case we perform a
1556 * true zero-copy operation from end-to-end. This is the situation
1557 * that happens all the time with large files. Second, if this is not
1558 * possible, but the mux's output buffer is empty, we still have an
1559 * opportunity to avoid the copy to the intermediary buffer, by making
1560 * the intermediary buffer's area point to the output buffer's area.
1561 * In this case we want to skip the HTX header to make sure that copies
1562 * remain aligned and that this operation remains possible all the
1563 * time. This goes for headers, data blocks and any data extracted from
1564 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001565 */
1566 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001567 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001568 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001569 htx_get_blk_value(chn_htx, blk).len == count) {
1570 void *old_area = h1c->obuf.area;
1571
Christopher Faulet6b81df72019-10-01 22:08:43 +02001572 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 +01001573 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001574 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001575 h1c->obuf.data = count;
1576
1577 buf->area = old_area;
1578 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001579
Christopher Faulet6b81df72019-10-01 22:08:43 +02001580 chn_htx = (struct htx *)buf->area;
1581 htx_reset(chn_htx);
1582
Christopher Fauletadb22202018-12-12 10:32:09 +01001583 /* The message is chunked. We need to emit the chunk
1584 * size. We have at least the size of the struct htx to
1585 * write the chunk envelope. It should be enough.
1586 */
1587 if (h1m->flags & H1_MF_CHNK) {
1588 h1_emit_chunk_size(&h1c->obuf, count);
1589 h1_emit_chunk_crlf(&h1c->obuf);
1590 }
1591
Willy Tarreau3815b222018-12-11 19:50:43 +01001592 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001593 if (h1m->state == H1_MSG_DATA)
1594 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001595 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001596 else
1597 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001598 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001599 goto out;
1600 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001601 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001602 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001603 else
1604 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001605
Christopher Fauletc2518a52019-06-25 21:41:02 +02001606 tmp.data = 0;
1607 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001608 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001609 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001610 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001611 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001612 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001613 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001614
Christopher Fauletb2e84162018-12-06 11:39:49 +01001615 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001616 if (type != HTX_BLK_DATA && vlen > count)
1617 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001618
Christopher Faulet94b2c762019-05-24 15:28:57 +02001619 if (type == HTX_BLK_UNUSED)
1620 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001621
Christopher Faulet94b2c762019-05-24 15:28:57 +02001622 switch (h1m->state) {
1623 case H1_MSG_RQBEFORE:
1624 if (type != HTX_BLK_REQ_SL)
1625 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001626 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 +02001627 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001628 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001629 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001630 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001631 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001632 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001633 if (sl->flags & HTX_SL_F_BODYLESS)
1634 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001635 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001636 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001637
Christopher Faulet94b2c762019-05-24 15:28:57 +02001638 case H1_MSG_RPBEFORE:
1639 if (type != HTX_BLK_RES_SL)
1640 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001641 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 +02001642 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001643 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001645 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001646 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001647 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001648 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001649 if (sl->info.res.status < 200 &&
1650 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001651 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001652 h1m->state = H1_MSG_HDR_FIRST;
1653 break;
1654
Christopher Faulet94b2c762019-05-24 15:28:57 +02001655 case H1_MSG_HDR_FIRST:
1656 case H1_MSG_HDR_NAME:
1657 case H1_MSG_HDR_L2_LWS:
1658 if (type == HTX_BLK_EOH)
1659 goto last_lf;
1660 if (type != HTX_BLK_HDR)
1661 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001662
Christopher Faulet9768c262018-10-22 09:34:31 +02001663 h1m->state = H1_MSG_HDR_NAME;
1664 n = htx_get_blk_name(chn_htx, blk);
1665 v = htx_get_blk_value(chn_htx, blk);
1666
Christopher Faulet86d144c2019-08-14 16:32:25 +02001667 /* Skip all pseudo-headers */
1668 if (*(n.ptr) == ':')
1669 goto skip_hdr;
1670
Christopher Faulet9768c262018-10-22 09:34:31 +02001671 if (isteqi(n, ist("transfer-encoding")))
1672 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001673 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001674 /* Only skip C-L header with invalid value. */
1675 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001676 goto skip_hdr;
1677 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001678 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001679 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001680 if (!v.len)
1681 goto skip_hdr;
1682 }
1683
Christopher Faulet67d58092019-10-02 10:51:38 +02001684 /* Skip header if same name is used to add the server name */
1685 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1686 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1687 goto skip_hdr;
1688
Christopher Faulet98fbe952019-07-22 16:18:24 +02001689 /* Try to adjust the case of the header name */
1690 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1691 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001692 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001693 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001694 skip_hdr:
1695 h1m->state = H1_MSG_HDR_L2_LWS;
1696 break;
1697
Christopher Faulet94b2c762019-05-24 15:28:57 +02001698 case H1_MSG_LAST_LF:
1699 if (type != HTX_BLK_EOH)
1700 goto error;
1701 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001702 h1m->state = H1_MSG_LAST_LF;
1703 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001704 /* If the reply comes from haproxy while the request is
1705 * not finished, we force the connection close. */
1706 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1707 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1708 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1709 }
1710
1711 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001712 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001713 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001714 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001715 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001716 /* Try to adjust the case of the header name */
1717 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1718 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001719 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001720 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001721 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001722 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001723 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001724
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001725 if ((h1s->meth != HTTP_METH_CONNECT &&
1726 (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 +01001727 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1728 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1729 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1730 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1731 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001732 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001733 n = ist("transfer-encoding");
1734 v = ist("chunked");
1735 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1736 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001737 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001738 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001739 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001740 h1m->flags |= H1_MF_CHNK;
1741 }
1742
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001743 /* Now add the server name to a header (if requested) */
1744 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1745 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1746 struct server *srv = objt_server(h1c->conn->target);
1747
1748 if (srv) {
1749 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1750 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001751
1752 /* Try to adjust the case of the header name */
1753 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1754 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001755 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001756 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001757 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001758 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001759 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1760 }
1761
Christopher Fauletc2518a52019-06-25 21:41:02 +02001762 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001763 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001764
Christopher Faulet6b81df72019-10-01 22:08:43 +02001765 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1766 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1767
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001768 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1769 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1770 h1_set_req_tunnel_mode(h1s);
1771 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001772 else if ((h1m->flags & H1_MF_RESP) &&
1773 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001774 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001775 * to the client. Switch the response to tunnel mode.
1776 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001777 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001778 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 +01001779 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001780 else if ((h1m->flags & H1_MF_RESP) &&
1781 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1782 h1m_init_res(&h1s->res);
1783 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001784 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001785 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001786 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001787 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001788 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001789 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1790 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001791 else
1792 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001793 break;
1794
Christopher Faulet94b2c762019-05-24 15:28:57 +02001795 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001796 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001797 if (type == HTX_BLK_EOM) {
1798 /* Chunked message without explicit trailers */
1799 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001800 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001801 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001802 }
1803 goto done;
1804 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001805 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001806 /* If the message is not chunked, never
1807 * add the last chunk. */
1808 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001809 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001810 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 +02001811 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001812 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001813 else if (type != HTX_BLK_DATA)
1814 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001815
1816 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 +02001817
1818
1819 if (vlen > count) {
1820 /* Get the maximum amount of data we can xferred */
1821 vlen = count;
1822 }
1823
1824 chklen = 0;
1825 if (h1m->flags & H1_MF_CHNK) {
1826 chklen = b_room(&tmp);
1827 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1828 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1829 (chklen < 1048576) ? 5 : 8);
1830 chklen += 4; /* 2 x CRLF */
1831 }
1832
1833 if (vlen + chklen > b_room(&tmp)) {
1834 /* too large for the buffer */
1835 if (chklen >= b_room(&tmp))
1836 goto full;
1837 vlen = b_room(&tmp) - chklen;
1838 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001839 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001840 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001841 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001842 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001843
1844 if (h1m->state == H1_MSG_DATA)
1845 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001846 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001847 else
1848 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001849 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001850 break;
1851
Christopher Faulet94b2c762019-05-24 15:28:57 +02001852 case H1_MSG_TRAILERS:
1853 if (type == HTX_BLK_EOM)
1854 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001855 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001856 goto error;
1857 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001858 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001859 /* If the message is not chunked, ignore
1860 * trailers. It may happen with H2 messages. */
1861 if (!(h1m->flags & H1_MF_CHNK))
1862 break;
1863
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001864 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001865 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001866 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001867 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1868 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001869 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001870 else { // HTX_BLK_TLR
1871 n = htx_get_blk_name(chn_htx, blk);
1872 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001873
1874 /* Try to adjust the case of the header name */
1875 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1876 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001877 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001878 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001879 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001880 break;
1881
Christopher Faulet94b2c762019-05-24 15:28:57 +02001882 case H1_MSG_DONE:
1883 if (type != HTX_BLK_EOM)
1884 goto error;
1885 done:
1886 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001887 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1888 h1_set_req_tunnel_mode(h1s);
1889 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1890 }
1891 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001892 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1893 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001894 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 +02001895 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001896
1897 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1898 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001899 break;
1900
Christopher Faulet9768c262018-10-22 09:34:31 +02001901 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001902 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001903 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001904 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001905 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001906 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001907 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001908 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1909 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001910 break;
1911 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001912
1913 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001914 total += vlen;
1915 count -= vlen;
1916 if (sz == vlen)
1917 blk = htx_remove_blk(chn_htx, blk);
1918 else {
1919 htx_cut_data_blk(chn_htx, blk, vlen);
1920 break;
1921 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001922 }
1923
Christopher Faulet9768c262018-10-22 09:34:31 +02001924 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001925 /* when the output buffer is empty, tmp shares the same area so that we
1926 * only have to update pointers and lengths.
1927 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001928 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1929 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001930 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001931 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001932
Willy Tarreau3815b222018-12-11 19:50:43 +01001933 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001934 out:
1935 if (!buf_room_for_htx_data(&h1c->obuf)) {
1936 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001937 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001938 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001939 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001940 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001941 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001942
1943 full:
1944 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1945 h1c->flags |= H1C_F_OUT_FULL;
1946 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001947}
1948
Christopher Faulet51dbc942018-09-13 09:05:15 +02001949/*********************************************************/
1950/* functions below are I/O callbacks from the connection */
1951/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001952static void h1_wake_stream_for_recv(struct h1s *h1s)
1953{
1954 if (h1s && h1s->recv_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001955 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001956 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001957 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001958 h1s->recv_wait = NULL;
1959 }
1960}
1961static void h1_wake_stream_for_send(struct h1s *h1s)
1962{
1963 if (h1s && h1s->send_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001964 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001965 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001966 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001967 h1s->send_wait = NULL;
1968 }
1969}
1970
Christopher Faulet51dbc942018-09-13 09:05:15 +02001971/*
1972 * Attempt to read data, and subscribe if none available
1973 */
1974static int h1_recv(struct h1c *h1c)
1975{
1976 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001977 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001978 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001979 int rcvd = 0;
1980
Christopher Faulet6b81df72019-10-01 22:08:43 +02001981 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
1982
1983 if (h1c->wait_event.events & SUB_RETRY_RECV) {
1984 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01001985 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001986 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001987
Olivier Houchard75159a92018-12-03 18:46:09 +01001988 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001989 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01001990 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001991 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001992 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001993
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001994 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1995 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001996 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02001997 goto end;
1998 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001999
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002000 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002001 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002002 h1_wake_stream_for_recv(h1s);
2003 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002004 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002005 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002006 }
2007
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002008 /*
2009 * If we only have a small amount of data, realign it,
2010 * it's probably cheaper than doing 2 recv() calls.
2011 */
2012 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2013 b_slow_realign(&h1c->ibuf, trash.area, 0);
2014
Willy Tarreau45f2b892018-12-05 07:59:27 +01002015 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002016 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002017 if (h1c->flags & H1C_F_IN_FULL) {
2018 h1c->flags &= ~H1C_F_IN_FULL;
2019 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2020 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002021
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002022 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002023 if (!b_data(&h1c->ibuf)) {
2024 /* try to pre-align the buffer like the rxbufs will be
2025 * to optimize memory copies.
2026 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002027 h1c->ibuf.head = sizeof(struct htx);
2028 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002029 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002030 }
Christopher Faulet47365272018-10-31 17:40:50 +01002031 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002032 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn,,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002033 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002034 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002035 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002036 if (h1s->csinfo.t_idle == -1)
2037 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2038 }
Christopher Faulet47365272018-10-31 17:40:50 +01002039 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002040
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002041 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002042 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002043 goto end;
2044 }
2045
Christopher Faulet6b81df72019-10-01 22:08:43 +02002046 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002047 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002048
Christopher Faulet9768c262018-10-22 09:34:31 +02002049 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002050 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2051 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002052
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002053 if (conn_xprt_read0_pending(conn) && h1s) {
2054 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002055 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002056 rcvd = 1;
2057 }
2058
Christopher Faulet51dbc942018-09-13 09:05:15 +02002059 if (!b_data(&h1c->ibuf))
2060 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002061 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002062 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002063 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2064 }
2065
2066 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002067 return rcvd;
2068}
2069
2070
2071/*
2072 * Try to send data if possible
2073 */
2074static int h1_send(struct h1c *h1c)
2075{
2076 struct connection *conn = h1c->conn;
2077 unsigned int flags = 0;
2078 size_t ret;
2079 int sent = 0;
2080
Christopher Faulet6b81df72019-10-01 22:08:43 +02002081 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2082
2083 if (conn->flags & CO_FL_ERROR) {
2084 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002085 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002086 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002087
2088 if (!b_data(&h1c->obuf))
2089 goto end;
2090
2091 if (h1c->flags & H1C_F_OUT_FULL)
2092 flags |= CO_SFL_MSG_MORE;
2093
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002094 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002095 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002096 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn,,, (size_t[]){ret});
2097 if (h1c->flags & H1C_F_OUT_FULL) {
2098 h1c->flags &= ~H1C_F_OUT_FULL;
2099 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2100 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002101 b_del(&h1c->obuf, ret);
2102 sent = 1;
2103 }
2104
Christopher Faulet145aa472018-12-06 10:56:20 +01002105 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002106 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002107 /* error or output closed, nothing to send, clear the buffer to release it */
2108 b_reset(&h1c->obuf);
2109 }
2110
Christopher Faulet51dbc942018-09-13 09:05:15 +02002111 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002112 if (!(h1c->flags & H1C_F_OUT_FULL))
2113 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002114
Christopher Faulet51dbc942018-09-13 09:05:15 +02002115 /* We're done, no more to send */
2116 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002117 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002118 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002119 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2120 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002121 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002122 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002123 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002124 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2125 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002126 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002127 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002128
Christopher Faulet6b81df72019-10-01 22:08:43 +02002129 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002130 return sent;
2131}
2132
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133
2134/* callback called on any event by the connection handler.
2135 * It applies changes and returns zero, or < 0 if it wants immediate
2136 * destruction of the connection.
2137 */
2138static int h1_process(struct h1c * h1c)
2139{
2140 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002141 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002142
Christopher Faulet6b81df72019-10-01 22:08:43 +02002143 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2144
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002145 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002146 return -1;
2147
Christopher Fauletfeb11742018-11-29 15:12:34 +01002148 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002149 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002150 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002151 goto release;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002152 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002153 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002154 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002155 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002156 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002157 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002158 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002159 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002160 }
2161
Christopher Fauletfeb11742018-11-29 15:12:34 +01002162 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2163 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2164
Christopher Faulet6b81df72019-10-01 22:08:43 +02002165 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002166 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002167 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2168 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002169
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002170 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002171 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002172 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002173 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002174 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002175 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002176 h1s->cs->data_cb->wake(h1s->cs);
2177 }
Christopher Faulet47365272018-10-31 17:40:50 +01002178 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002179 if (h1c->task) {
2180 h1c->task->expire = TICK_ETERNITY;
2181 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002182 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 +01002183 ? h1c->shut_timeout
2184 : h1c->timeout));
2185 task_queue(h1c->task);
2186 }
2187 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002188 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002189 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002190
2191 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002192 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002193 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002194 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002195}
2196
2197static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2198{
2199 struct h1c *h1c = ctx;
2200 int ret = 0;
2201
Christopher Faulet6b81df72019-10-01 22:08:43 +02002202 TRACE_POINT(H1_EV_H1C_WAKE, h1c->conn);
2203
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002204 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002205 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002206 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002207 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002208 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002209 h1_process(h1c);
2210 return NULL;
2211}
2212
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002213static void h1_reset(struct connection *conn)
2214{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002215
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002216}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002217
2218static int h1_wake(struct connection *conn)
2219{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002220 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002221 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002222
Christopher Faulet6b81df72019-10-01 22:08:43 +02002223 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2224
Christopher Faulet539e0292018-11-19 10:40:09 +01002225 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002226 ret = h1_process(h1c);
2227 if (ret == 0) {
2228 struct h1s *h1s = h1c->h1s;
2229
Christopher Faulet6b81df72019-10-01 22:08:43 +02002230 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2231 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002232 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002233 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002234 }
2235 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002236}
2237
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002238/* Connection timeout management. The principle is that if there's no receipt
2239 * nor sending for a certain amount of time, the connection is closed.
2240 */
2241static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2242{
2243 struct h1c *h1c = context;
2244 int expired = tick_is_expired(t->expire, now_ms);
2245
Christopher Faulet6b81df72019-10-01 22:08:43 +02002246 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2247
2248 if (!expired && h1c) {
2249 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002250 return t;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002251 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002252
Olivier Houchard3f795f72019-04-17 22:51:06 +02002253 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002254
2255 if (!h1c) {
2256 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002257 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002258 return NULL;
2259 }
2260
2261 h1c->task = NULL;
2262 /* If a stream is still attached to the mux, just set an error and wait
2263 * for the stream's timeout. Otherwise, release the mux. This is only ok
2264 * because same timeouts are used.
2265 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002266 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002267 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002268 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2269 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002270 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002271 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002272
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002273 return NULL;
2274}
2275
Christopher Faulet51dbc942018-09-13 09:05:15 +02002276/*******************************************/
2277/* functions below are used by the streams */
2278/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002279
Christopher Faulet51dbc942018-09-13 09:05:15 +02002280/*
2281 * Attach a new stream to a connection
2282 * (Used for outgoing connections)
2283 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002284static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002285{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002286 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002287 struct conn_stream *cs = NULL;
2288 struct h1s *h1s;
2289
Christopher Faulet6b81df72019-10-01 22:08:43 +02002290 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2291 if (h1c->flags & H1C_F_CS_ERROR) {
2292 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 +02002293 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002294 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002295
2296 cs = cs_new(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002297 if (!cs) {
2298 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 +02002299 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002300 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002301
Olivier Houchardf502aca2018-12-14 19:42:40 +01002302 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002303 if (h1s == NULL) {
2304 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 +02002305 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002306 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002307
Christopher Faulet6b81df72019-10-01 22:08:43 +02002308 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002309 return cs;
2310 end:
2311 cs_free(cs);
2312 return NULL;
2313}
2314
2315/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2316 * this mux, it's easy as we can only store a single conn_stream.
2317 */
2318static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2319{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002320 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002321 struct h1s *h1s = h1c->h1s;
2322
2323 if (h1s)
2324 return h1s->cs;
2325
2326 return NULL;
2327}
2328
Christopher Faulet73c12072019-04-08 11:23:22 +02002329static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002330{
Christopher Faulet73c12072019-04-08 11:23:22 +02002331 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002332
Christopher Faulet6b81df72019-10-01 22:08:43 +02002333 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002334 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002335 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002336}
2337
2338/*
2339 * Detach the stream from the connection and possibly release the connection.
2340 */
2341static void h1_detach(struct conn_stream *cs)
2342{
2343 struct h1s *h1s = cs->ctx;
2344 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002345 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002346 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002347
Christopher Faulet6b81df72019-10-01 22:08:43 +02002348 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2349
Christopher Faulet51dbc942018-09-13 09:05:15 +02002350 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002351 if (!h1s) {
2352 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002353 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002354 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002355
Olivier Houchardf502aca2018-12-14 19:42:40 +01002356 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002357 h1c = h1s->h1c;
2358 h1s->cs = NULL;
2359
Olivier Houchard8a786902018-12-15 16:05:40 +01002360 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2361 h1s_destroy(h1s);
2362
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002363 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002364 /* If there are any excess server data in the input buffer,
2365 * release it and close the connection ASAP (some data may
2366 * remain in the output buffer). This happens if a server sends
2367 * invalid responses. So in such case, we don't want to reuse
2368 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002369 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002370 if (b_data(&h1c->ibuf)) {
2371 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002372 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002373 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002374 goto release;
2375 }
Christopher Faulet03627242019-07-19 11:34:08 +02002376
Christopher Faulet9400a392018-11-23 23:10:39 +01002377 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002378 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002379 h1c->conn->flags |= CO_FL_PRIVATE;
2380
Olivier Houchard44d59142018-12-13 18:46:22 +01002381 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002382 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002383 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2384 h1c->conn->owner = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002385 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) {
Olivier Houchard351411f2018-12-27 17:20:54 +01002386 /* The server doesn't want it, let's kill the connection right away */
2387 h1c->conn->mux->destroy(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002388 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2389 goto end;
2390 }
2391 tasklet_wakeup(h1c->wait_event.tasklet);
2392 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2393 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002394 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002395 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002396 if (h1c->conn->owner == sess) {
2397 int ret = session_check_idle_conn(sess, h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002398 if (ret == -1) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002399 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002400 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2401 goto end;
2402 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002403 else if (ret == 1) {
2404 /* The connection was added to the server list,
2405 * wake the task so we can subscribe to events
2406 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002407 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002408 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2409 goto end;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002410 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002411 TRACE_DEVEL("connection in idle session list", H1_EV_STRM_END, h1c->conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002412 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002413 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002414 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002415 struct server *srv = objt_server(h1c->conn->target);
2416
2417 if (srv) {
2418 if (h1c->conn->flags & CO_FL_PRIVATE)
2419 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002420 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002421 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2422 else
2423 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002424 TRACE_DEVEL("connection in idle server list", H1_EV_STRM_END, h1c->conn);
Christopher Faulet9400a392018-11-23 23:10:39 +01002425 }
2426 }
2427 }
2428
Christopher Fauletf1204b82019-07-19 14:51:06 +02002429 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002430 /* 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 +02002431 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2432 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2433 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002434 !h1c->conn->owner) {
2435 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002436 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002437 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002438 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002439 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002440 if (h1c->task) {
2441 h1c->task->expire = TICK_ETERNITY;
2442 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002443 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 +01002444 ? h1c->shut_timeout
2445 : h1c->timeout));
2446 task_queue(h1c->task);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002447 TRACE_DEVEL("refreshing connection's timeout", H1_EV_STRM_END, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002448 }
2449 }
2450 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002451 end:
2452 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453}
2454
2455
2456static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2457{
2458 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002459 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002460
2461 if (!h1s)
2462 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002463 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002464
Christopher Faulet6b81df72019-10-01 22:08:43 +02002465 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2466
2467 if (cs->flags & CS_FL_KILL_CONN) {
2468 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2469 goto do_shutr;
2470 }
2471 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2472 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002473 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002474 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002475
Christopher Faulet6b81df72019-10-01 22:08:43 +02002476 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2477 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2478 goto end;
2479 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480
Christopher Faulet7f366362019-04-08 10:51:20 +02002481 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002482 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2483 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002484 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002485 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002486 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002487 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002488 if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet7f366362019-04-08 10:51:20 +02002489 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002490 end:
2491 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002492}
2493
2494static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2495{
2496 struct h1s *h1s = cs->ctx;
2497 struct h1c *h1c;
2498
2499 if (!h1s)
2500 return;
2501 h1c = h1s->h1c;
2502
Christopher Faulet6b81df72019-10-01 22:08:43 +02002503 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2504
2505 if (cs->flags & CS_FL_KILL_CONN) {
2506 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002507 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002508 }
2509 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2510 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2511 goto do_shutw;
2512 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002513
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002514 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002515 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2516 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2517 goto end;
2518 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002519
Christopher Faulet7f366362019-04-08 10:51:20 +02002520 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002521 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2522 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002523 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002524 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002525 end:
2526 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002527}
2528
Christopher Faulet666a0c42019-01-08 11:12:04 +01002529static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002530{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002531 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002532
Christopher Faulet6b81df72019-10-01 22:08:43 +02002533 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002534 conn_xprt_shutw(conn);
2535 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2536 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2537 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002538 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002539}
2540
2541/* Called from the upper layer, to unsubscribe to events */
2542static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2543{
2544 struct wait_event *sw;
2545 struct h1s *h1s = cs->ctx;
2546
2547 if (!h1s)
2548 return 0;
2549
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002550 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002551 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002552 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002553 BUG_ON(h1s->recv_wait != sw);
2554 sw->events &= ~SUB_RETRY_RECV;
2555 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002556 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002557 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002558 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002559 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002560 BUG_ON(h1s->send_wait != sw);
2561 sw->events &= ~SUB_RETRY_SEND;
2562 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002563 }
2564 return 0;
2565}
2566
2567/* Called from the upper layer, to subscribe to events, such as being able to send */
2568static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2569{
2570 struct wait_event *sw;
2571 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002572 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002573
2574 if (!h1s)
2575 return -1;
2576
Christopher Faulet6b81df72019-10-01 22:08:43 +02002577 if (event_type & SUB_RETRY_RECV) {
2578 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
2579 sw = param;
2580 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2581 sw->events |= SUB_RETRY_RECV;
2582 h1s->recv_wait = sw;
2583 event_type &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002584 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002585 if (event_type & SUB_RETRY_SEND) {
2586 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
2587 sw = param;
2588 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2589 sw->events |= SUB_RETRY_SEND;
2590 h1s->send_wait = sw;
2591 event_type &= ~SUB_RETRY_SEND;
2592 /*
2593 * If the conn_stream attempt to subscribe, and the
2594 * mux isn't subscribed to the connection, then it
2595 * probably means the connection wasn't established
2596 * yet, so we have to subscribe.
2597 */
2598 h1c = h1s->h1c;
2599 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2600 h1c->conn->xprt->subscribe(h1c->conn,
2601 h1c->conn->xprt_ctx,
2602 SUB_RETRY_SEND,
2603 &h1c->wait_event);
2604 }
2605 if (event_type != 0)
2606 return -1;
2607 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002608}
2609
2610/* Called from the upper layer, to receive data */
2611static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2612{
2613 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002614 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002615 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002616 size_t ret = 0;
2617
Christopher Faulet6b81df72019-10-01 22:08:43 +02002618 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002619 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002620 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002621 else
2622 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002623
Christopher Faulete18777b2019-04-16 16:46:36 +02002624 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002625 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002626 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002627 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2628 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002629 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002630 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002631 if (h1s->flags & H1S_F_SPLICED_DATA) {
2632 h1s->flags &= ~H1S_F_SPLICED_DATA;
2633 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2634 }
2635 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002636 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002637 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002638 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002639 return ret;
2640}
2641
2642
2643/* Called from the upper layer, to send data */
2644static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2645{
2646 struct h1s *h1s = cs->ctx;
2647 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002648 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002649
2650 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002651 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002652 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002653
Christopher Faulet6b81df72019-10-01 22:08:43 +02002654 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){count});
2655
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002656 /* If we're not connected yet, or we're waiting for a handshake, stop
2657 * now, as we don't want to remove everything from the channel buffer
2658 * before we're sure we can send it.
2659 */
2660 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002661 (h1c->conn->flags & CO_FL_HANDSHAKE)) {
2662 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002663 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002664 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002665
Christopher Fauletc31872f2019-06-04 22:09:36 +02002666 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002667 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002668
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002669 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2670 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002671 else
2672 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002673 if (!ret)
2674 break;
2675 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002676 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002677 if (!h1_send(h1c))
2678 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002679 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002680
Christopher Faulet6b81df72019-10-01 22:08:43 +02002681 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002682 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002683}
2684
Willy Tarreaue5733232019-05-22 19:24:06 +02002685#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002686/* Send and get, using splicing */
2687static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2688{
2689 struct h1s *h1s = cs->ctx;
2690 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2691 int ret = 0;
2692
Christopher Faulet6b81df72019-10-01 22:08:43 +02002693 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){count});
2694
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002695 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002696 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002697 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2698 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2699 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002700 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002701 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002702 goto end;
2703 }
2704
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002705 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002706 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002707 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002708 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002709 }
2710
2711 h1s->flags &= ~H1S_F_BUF_FLUSH;
2712 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002713 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002714 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2715 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002716 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002717 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002718 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002719 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002720 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002721 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2722 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002723 }
2724
Christopher Faulet1be55f92018-10-02 15:59:23 +02002725 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002726 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002727 h1s->flags |= H1S_F_REOS;
Christopher Fauletf3158e92019-11-15 11:14:23 +01002728 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002729 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002730 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002731
2732 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002733 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002734}
2735
2736static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2737{
2738 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002739 int ret = 0;
2740
Christopher Faulet6b81df72019-10-01 22:08:43 +02002741 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){pipe->data});
2742
Christopher Faulet1be55f92018-10-02 15:59:23 +02002743 if (b_data(&h1s->h1c->obuf))
2744 goto end;
2745
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002746 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002747 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002748 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002749 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2750 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002751 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002752 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002753 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002754
2755 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002756 return ret;
2757}
2758#endif
2759
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002760static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2761{
2762 int ret = 0;
2763 switch (mux_ctl) {
2764 case MUX_STATUS:
2765 if (conn->flags & CO_FL_CONNECTED)
2766 ret |= MUX_STATUS_READY;
2767 return ret;
2768 default:
2769 return -1;
2770 }
2771}
2772
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002773/* for debugging with CLI's "show fd" command */
2774static void h1_show_fd(struct buffer *msg, struct connection *conn)
2775{
2776 struct h1c *h1c = conn->ctx;
2777 struct h1s *h1s = h1c->h1s;
2778
Christopher Fauletf376a312019-01-04 15:16:06 +01002779 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2780 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002781 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2782 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2783 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2784 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2785
2786 if (h1s) {
2787 char *method;
2788
2789 if (h1s->meth < HTTP_METH_OTHER)
2790 method = http_known_methods[h1s->meth].ptr;
2791 else
2792 method = "UNKNOWN";
2793 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2794 " .meth=%s status=%d",
2795 h1s, h1s->flags,
2796 h1m_state_str(h1s->req.state),
2797 h1m_state_str(h1s->res.state), method, h1s->status);
2798 if (h1s->cs)
2799 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2800 h1s->cs->flags, h1s->cs->data);
2801 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002802}
2803
2804
2805/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2806static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2807{
2808 struct h1_hdr_entry *entry;
2809
2810 /* Be sure there is a non-empty <to> */
2811 if (!strlen(to)) {
2812 memprintf(err, "expect <to>");
2813 return -1;
2814 }
2815
2816 /* Be sure only the case differs between <from> and <to> */
2817 if (strcasecmp(from, to)) {
2818 memprintf(err, "<from> and <to> must not differ execpt the case");
2819 return -1;
2820 }
2821
2822 /* Be sure <from> does not already existsin the tree */
2823 if (ebis_lookup(&hdrs_map.map, from)) {
2824 memprintf(err, "duplicate entry '%s'", from);
2825 return -1;
2826 }
2827
2828 /* Create the entry and insert it in the tree */
2829 entry = malloc(sizeof(*entry));
2830 if (!entry) {
2831 memprintf(err, "out of memory");
2832 return -1;
2833 }
2834
2835 entry->node.key = strdup(from);
2836 entry->name.ptr = strdup(to);
2837 entry->name.len = strlen(to);
2838 if (!entry->node.key || !entry->name.ptr) {
2839 free(entry->node.key);
2840 free(entry->name.ptr);
2841 free(entry);
2842 memprintf(err, "out of memory");
2843 return -1;
2844 }
2845 ebis_insert(&hdrs_map.map, &entry->node);
2846 return 0;
2847}
2848
2849static void h1_hdeaders_case_adjust_deinit()
2850{
2851 struct ebpt_node *node, *next;
2852 struct h1_hdr_entry *entry;
2853
2854 node = ebpt_first(&hdrs_map.map);
2855 while (node) {
2856 next = ebpt_next(node);
2857 ebpt_delete(node);
2858 entry = container_of(node, struct h1_hdr_entry, node);
2859 free(entry->node.key);
2860 free(entry->name.ptr);
2861 free(entry);
2862 node = next;
2863 }
2864 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002865}
2866
Christopher Faulet98fbe952019-07-22 16:18:24 +02002867static int cfg_h1_headers_case_adjust_postparser()
2868{
2869 FILE *file = NULL;
2870 char *c, *key_beg, *key_end, *value_beg, *value_end;
2871 char *err;
2872 int rc, line = 0, err_code = 0;
2873
2874 if (!hdrs_map.name)
2875 goto end;
2876
2877 file = fopen(hdrs_map.name, "r");
2878 if (!file) {
2879 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2880 hdrs_map.name);
2881 err_code |= ERR_ALERT | ERR_FATAL;
2882 goto end;
2883 }
2884
2885 /* now parse all lines. The file may contain only two header name per
2886 * line, separated by spaces. All heading and trailing spaces will be
2887 * ignored. Lines starting with a # are ignored.
2888 */
2889 while (fgets(trash.area, trash.size, file) != NULL) {
2890 line++;
2891 c = trash.area;
2892
2893 /* strip leading spaces and tabs */
2894 while (*c == ' ' || *c == '\t')
2895 c++;
2896
2897 /* ignore emptu lines, or lines beginning with a dash */
2898 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2899 continue;
2900
2901 /* look for the end of the key */
2902 key_beg = c;
2903 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2904 c++;
2905 key_end = c;
2906
2907 /* strip middle spaces and tabs */
2908 while (*c == ' ' || *c == '\t')
2909 c++;
2910
2911 /* look for the end of the value, it is the end of the line */
2912 value_beg = c;
2913 while (*c && *c != '\n' && *c != '\r')
2914 c++;
2915 value_end = c;
2916
2917 /* trim possibly trailing spaces and tabs */
2918 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2919 value_end--;
2920
2921 /* set final \0 and check entries */
2922 *key_end = '\0';
2923 *value_end = '\0';
2924
2925 err = NULL;
2926 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2927 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002928 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2929 hdrs_map.name, err, line);
2930 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002931 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002932 goto end;
2933 }
2934 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002935 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2936 hdrs_map.name, err, line);
2937 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002938 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002939 }
2940 }
2941
2942 end:
2943 if (file)
2944 fclose(file);
2945 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2946 return err_code;
2947}
2948
2949
2950/* config parser for global "h1-outgoing-header-case-adjust" */
2951static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2952 struct proxy *defpx, const char *file, int line,
2953 char **err)
2954{
2955 if (too_many_args(2, args, err, NULL))
2956 return -1;
2957 if (!*(args[1]) || !*(args[2])) {
2958 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2959 return -1;
2960 }
2961 return add_hdr_case_adjust(args[1], args[2], err);
2962}
2963
2964/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2965static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2966 struct proxy *defpx, const char *file, int line,
2967 char **err)
2968{
2969 if (too_many_args(1, args, err, NULL))
2970 return -1;
2971 if (!*(args[1])) {
2972 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2973 return -1;
2974 }
2975 free(hdrs_map.name);
2976 hdrs_map.name = strdup(args[1]);
2977 return 0;
2978}
2979
2980
2981/* config keyword parsers */
2982static struct cfg_kw_list cfg_kws = {{ }, {
2983 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2984 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2985 { 0, NULL, NULL },
2986 }
2987};
2988
2989INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2990REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2991
2992
Christopher Faulet51dbc942018-09-13 09:05:15 +02002993/****************************************/
2994/* MUX initialization and instanciation */
2995/****************************************/
2996
2997/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002998static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002999 .init = h1_init,
3000 .wake = h1_wake,
3001 .attach = h1_attach,
3002 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01003003 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003004 .detach = h1_detach,
3005 .destroy = h1_destroy,
3006 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003007 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003008 .rcv_buf = h1_rcv_buf,
3009 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003010#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003011 .rcv_pipe = h1_rcv_pipe,
3012 .snd_pipe = h1_snd_pipe,
3013#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003014 .subscribe = h1_subscribe,
3015 .unsubscribe = h1_unsubscribe,
3016 .shutr = h1_shutr,
3017 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003018 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003019 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003020 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003021 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003022 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003023};
3024
3025
3026/* this mux registers default HTX proto */
3027static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003028{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003029
Willy Tarreau0108d902018-11-25 19:14:37 +01003030INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3031
Christopher Faulet51dbc942018-09-13 09:05:15 +02003032/*
3033 * Local variables:
3034 * c-indent-level: 8
3035 * c-basic-offset: 8
3036 * End:
3037 */