blob: b44204845465bbea73caca44b55706ed194e7972 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Christopher Faulet0ef372a2019-04-08 10:57:20 +020015#include <common/h2.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Christopher Faulet98fbe952019-07-22 16:18:24 +020019#include <ebistree.h>
20
Christopher Faulet1be55f92018-10-02 15:59:23 +020021#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020022#include <types/proxy.h>
23#include <types/session.h>
24
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/connection.h>
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020026#include <proto/h1_htx.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020027#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020028#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010029#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030#include <proto/stream.h>
31#include <proto/stream_interface.h>
Christopher Faulet6b81df72019-10-01 22:08:43 +020032#include <proto/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020033
34/*
35 * H1 Connection flags (32 bits)
36 */
37#define H1C_F_NONE 0x00000000
38
39/* Flags indicating why writing output data are blocked */
40#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
41#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
42/* 0x00000004 - 0x00000008 unused */
43
44/* Flags indicating why reading input data are blocked. */
45#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
46#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Faulet7b109f22019-12-05 11:18:31 +010047#define H1C_F_IN_BUSY 0x00000040 /* mux is blocked on input waiting the other side */
Christopher Faulet539e0292018-11-19 10:40:09 +010048/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
Christopher Faulet7b109f22019-12-05 11:18:31 +010050/* Flags indicating the connection state */
Christopher Faulet51dbc942018-09-13 09:05:15 +020051#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
52#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet7b109f22019-12-05 11:18:31 +010053#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +010054#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
55 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020056
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020058#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020059
Christopher Faulet51dbc942018-09-13 09:05:15 +020060/*
61 * H1 Stream flags (32 bits)
62 */
Christopher Faulet129817b2018-09-20 16:14:40 +020063#define H1S_F_NONE 0x00000000
64#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020065#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
66#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020067#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020068#define H1S_F_WANT_KAL 0x00000010
69#define H1S_F_WANT_TUN 0x00000020
70#define H1S_F_WANT_CLO 0x00000040
71#define H1S_F_WANT_MSK 0x00000070
72#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010073#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010074#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010075#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +020076#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020077/* 0x00002000 .. 0x00001000 unused */
78#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 +020079#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020080
81/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020082struct h1c {
83 struct connection *conn;
84 struct proxy *px;
85 uint32_t flags; /* Connection flags: H1C_F_* */
86
87 struct buffer ibuf; /* Input buffer to store data before parsing */
88 struct buffer obuf; /* Output buffer to store data after reformatting */
89
90 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
91 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
92
93 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010094 struct task *task; /* timeout management task */
95 int timeout; /* idle timeout duration in ticks */
96 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020097};
98
99/* H1 stream descriptor */
100struct h1s {
101 struct h1c *h1c;
102 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100103 struct cs_info csinfo; /* CS info, only used for client connections */
104 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200105
Christopher Faulet51dbc942018-09-13 09:05:15 +0200106 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
107 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200108
Olivier Houchardf502aca2018-12-14 19:42:40 +0100109 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200110 struct h1m req;
111 struct h1m res;
112
113 enum http_meth_t meth; /* HTTP resquest method */
114 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200115};
116
Christopher Faulet98fbe952019-07-22 16:18:24 +0200117/* Map of headers used to convert outgoing headers */
118struct h1_hdrs_map {
119 char *name;
120 struct eb_root map;
121};
122
123/* An entry in a headers map */
124struct h1_hdr_entry {
125 struct ist name;
126 struct ebpt_node node;
127};
128
129/* Declare the headers map */
130static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
131
132
Christopher Faulet6b81df72019-10-01 22:08:43 +0200133/* trace source and events */
134static void h1_trace(enum trace_level level, uint64_t mask,
135 const struct trace_source *src,
136 const struct ist where, const struct ist func,
137 const void *a1, const void *a2, const void *a3, const void *a4);
138
139/* The event representation is split like this :
140 * h1c - internal H1 connection
141 * h1s - internal H1 stream
142 * strm - application layer
143 * rx - data receipt
144 * tx - data transmission
145 *
146 */
147static const struct trace_event h1_trace_events[] = {
148#define H1_EV_H1C_NEW (1ULL << 0)
149 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
150#define H1_EV_H1C_RECV (1ULL << 1)
151 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
152#define H1_EV_H1C_SEND (1ULL << 2)
153 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
154#define H1_EV_H1C_BLK (1ULL << 3)
155 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
156#define H1_EV_H1C_WAKE (1ULL << 4)
157 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
158#define H1_EV_H1C_END (1ULL << 5)
159 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
160#define H1_EV_H1C_ERR (1ULL << 6)
161 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
162
163#define H1_EV_RX_DATA (1ULL << 7)
164 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
165#define H1_EV_RX_EOI (1ULL << 8)
166 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
167#define H1_EV_RX_HDRS (1ULL << 9)
168 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
169#define H1_EV_RX_BODY (1ULL << 10)
170 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
171#define H1_EV_RX_TLRS (1ULL << 11)
172 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
173
174#define H1_EV_TX_DATA (1ULL << 12)
175 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
176#define H1_EV_TX_EOI (1ULL << 13)
177 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
178#define H1_EV_TX_HDRS (1ULL << 14)
179 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
180#define H1_EV_TX_BODY (1ULL << 15)
181 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
182#define H1_EV_TX_TLRS (1ULL << 16)
183 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
184
185#define H1_EV_H1S_NEW (1ULL << 17)
186 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
187#define H1_EV_H1S_BLK (1ULL << 18)
188 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
189#define H1_EV_H1S_END (1ULL << 19)
190 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
191#define H1_EV_H1S_ERR (1ULL << 20)
192 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
193
194#define H1_EV_STRM_NEW (1ULL << 21)
195 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
196#define H1_EV_STRM_RECV (1ULL << 22)
197 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
198#define H1_EV_STRM_SEND (1ULL << 23)
199 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
200#define H1_EV_STRM_WAKE (1ULL << 24)
201 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
202#define H1_EV_STRM_SHUT (1ULL << 25)
203 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
204#define H1_EV_STRM_END (1ULL << 26)
205 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
206#define H1_EV_STRM_ERR (1ULL << 27)
207 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
208
209 { }
210};
211
212static const struct name_desc h1_trace_lockon_args[4] = {
213 /* arg1 */ { /* already used by the connection */ },
214 /* arg2 */ { .name="h1s", .desc="H1 stream" },
215 /* arg3 */ { },
216 /* arg4 */ { }
217};
218
219static const struct name_desc h1_trace_decoding[] = {
220#define H1_VERB_CLEAN 1
221 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
222#define H1_VERB_MINIMAL 2
223 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
224#define H1_VERB_SIMPLE 3
225 { .name="simple", .desc="add request/response status line or htx info when available" },
226#define H1_VERB_ADVANCED 4
227 { .name="advanced", .desc="add header fields or frame decoding when available" },
228#define H1_VERB_COMPLETE 5
229 { .name="complete", .desc="add full data dump when available" },
230 { /* end */ }
231};
232
233static struct trace_source trace_h1 = {
234 .name = IST("h1"),
235 .desc = "HTTP/1 multiplexer",
236 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
237 .default_cb = h1_trace,
238 .known_events = h1_trace_events,
239 .lockon_args = h1_trace_lockon_args,
240 .decoding = h1_trace_decoding,
241 .report_events = ~0, // report everything by default
242};
243
244#define TRACE_SOURCE &trace_h1
245INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
246
Christopher Faulet51dbc942018-09-13 09:05:15 +0200247/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100248DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
249DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200250
Christopher Faulet51dbc942018-09-13 09:05:15 +0200251static int h1_recv(struct h1c *h1c);
252static int h1_send(struct h1c *h1c);
253static int h1_process(struct h1c *h1c);
254static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100255static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100256static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200257static void h1_wake_stream_for_recv(struct h1s *h1s);
258static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200259
Christopher Faulet6b81df72019-10-01 22:08:43 +0200260/* the H1 traces always expect that arg1, if non-null, is of type connection
261 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
262 * that arg3, if non-null, is a htx for rx/tx headers.
263 */
264static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
265 const struct ist where, const struct ist func,
266 const void *a1, const void *a2, const void *a3, const void *a4)
267{
268 const struct connection *conn = a1;
269 const struct h1c *h1c = conn ? conn->ctx : NULL;
270 const struct h1s *h1s = a2;
271 const struct htx *htx = a3;
272 const size_t *val = a4;
273
274 if (!h1c)
275 h1c = (h1s ? h1s->h1c : NULL);
276
277 if (!h1c || src->verbosity < H1_VERB_CLEAN)
278 return;
279
280 /* Display frontend/backend info by default */
281 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
282
283 /* Display request and response states if h1s is defined */
284 if (h1s)
285 chunk_appendf(&trace_buf, " [%s, %s]",
286 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
287
288 if (src->verbosity == H1_VERB_CLEAN)
289 return;
290
291 /* Display the value to the 4th argument (level > STATE) */
292 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100293 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200294
295 /* Display status-line if possible (verbosity > MINIMAL) */
296 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
297 const struct htx_blk *blk = htx_get_head_blk(htx);
298 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
299 enum htx_blk_type type = htx_get_blk_type(blk);
300
301 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
302 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
303 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
304 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
305 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
306 }
307
308 /* Display h1c info and, if defined, h1s info (pointer + flags) */
309 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
310 if (h1s)
311 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
312
313 if (src->verbosity == H1_VERB_MINIMAL)
314 return;
315
316 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
317 if (src->level > TRACE_LEVEL_USER) {
318 if (src->verbosity == H1_VERB_COMPLETE ||
319 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
320 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
321 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
322 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
323 if (src->verbosity == H1_VERB_COMPLETE ||
324 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
325 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
326 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
327 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
328 }
329
330 /* Display htx info if defined (level > USER) */
331 if (src->level > TRACE_LEVEL_USER && htx) {
332 int full = 0;
333
334 /* Full htx info (level > STATE && verbosity > SIMPLE) */
335 if (src->level > TRACE_LEVEL_STATE) {
336 if (src->verbosity == H1_VERB_COMPLETE)
337 full = 1;
338 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
339 full = 1;
340 }
341
342 chunk_memcat(&trace_buf, "\n\t", 2);
343 htx_dump(&trace_buf, htx, full);
344 }
345}
346
347
Christopher Faulet51dbc942018-09-13 09:05:15 +0200348/*****************************************************/
349/* functions below are for dynamic buffer management */
350/*****************************************************/
351/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100352 * Indicates whether or not we may receive data. The rules are the following :
353 * - if an error or a shutdown for reads was detected on the connection we
354 must not attempt to receive
355 * - if the input buffer failed to be allocated or is full , we must not try
356 * to receive
357 * - if he input processing is busy waiting for the output side, we may
358 * attemp to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200359 * - otherwise must may not attempt to receive
360 */
361static inline int h1_recv_allowed(const struct h1c *h1c)
362{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100363 if (h1c->flags & H1C_F_CS_ERROR) {
364 TRACE_DEVEL("recv not allowed because of error on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200365 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200366 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200367
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100368 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200369 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 +0100370 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200371 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100372
Christopher Fauletcb55f482018-12-10 11:56:47 +0100373 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374 return 1;
375
Christopher Faulet6b81df72019-10-01 22:08:43 +0200376 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 +0200377 return 0;
378}
379
380/*
381 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
382 * flags are used to figure what buffer was requested. It returns 1 if the
383 * allocation succeeds, in which case the connection is woken up, or 0 if it's
384 * impossible to wake up and we prefer to be woken up later.
385 */
386static int h1_buf_available(void *target)
387{
388 struct h1c *h1c = target;
389
390 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200391 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 +0200392 h1c->flags &= ~H1C_F_IN_ALLOC;
393 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200394 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200395 return 1;
396 }
397
398 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200399 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 +0200400 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200401 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200402 if (h1c->h1s)
403 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200404 return 1;
405 }
406
Christopher Faulet51dbc942018-09-13 09:05:15 +0200407 return 0;
408}
409
410/*
411 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
412 */
413static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
414{
415 struct buffer *buf = NULL;
416
417 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
418 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
419 h1c->buf_wait.target = h1c;
420 h1c->buf_wait.wakeup_cb = h1_buf_available;
421 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
422 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
423 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
424 __conn_xprt_stop_recv(h1c->conn);
425 }
426 return buf;
427}
428
429/*
430 * Release a buffer, if any, and try to wake up entities waiting in the buffer
431 * wait queue.
432 */
433static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
434{
435 if (bptr->size) {
436 b_free(bptr);
437 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
438 }
439}
440
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100441/* returns the number of streams in use on a connection to figure if it's idle
442 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
443 * not. This flag is only set when no H1S is attached and when the previous
444 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100445 */
446static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200447{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100448 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100450 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200451}
452
Willy Tarreau00f18a32019-01-26 12:19:01 +0100453/* returns the number of streams still available on a connection */
454static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100455{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100456 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100457}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200458
Willy Tarreau00f18a32019-01-26 12:19:01 +0100459
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460/*****************************************************************/
461/* functions below are dedicated to the mux setup and management */
462/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200463
464/* returns non-zero if there are input data pending for stream h1s. */
465static inline size_t h1s_data_pending(const struct h1s *h1s)
466{
467 const struct h1m *h1m;
468
469 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
470 if (h1m->state == H1_MSG_DONE)
471 return 0; // data not for this stream (e.g. pipelining)
472
473 return b_data(&h1s->h1c->ibuf);
474}
475
Christopher Faulet47365272018-10-31 17:40:50 +0100476static struct conn_stream *h1s_new_cs(struct h1s *h1s)
477{
478 struct conn_stream *cs;
479
Christopher Faulet6b81df72019-10-01 22:08:43 +0200480 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100481 cs = cs_new(h1s->h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200482 if (!cs) {
483 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 +0100484 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200485 }
Christopher Faulet47365272018-10-31 17:40:50 +0100486 h1s->cs = cs;
487 cs->ctx = h1s;
488
489 if (h1s->flags & H1S_F_NOT_FIRST)
490 cs->flags |= CS_FL_NOT_FIRST;
491
Christopher Faulet6b81df72019-10-01 22:08:43 +0200492 if (stream_create_from_cs(cs) < 0) {
493 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 +0100494 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200495 }
496
497 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100498 return cs;
499
500 err:
501 cs_free(cs);
502 h1s->cs = NULL;
503 return NULL;
504}
505
Olivier Houchardf502aca2018-12-14 19:42:40 +0100506static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200507{
508 struct h1s *h1s;
509
Christopher Faulet6b81df72019-10-01 22:08:43 +0200510 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
511
Christopher Faulet51dbc942018-09-13 09:05:15 +0200512 h1s = pool_alloc(pool_head_h1s);
513 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100514 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200515
516 h1s->h1c = h1c;
517 h1c->h1s = h1s;
518
Olivier Houchardf502aca2018-12-14 19:42:40 +0100519 h1s->sess = sess;
520
Christopher Faulet51dbc942018-09-13 09:05:15 +0200521 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100522 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200523
524 h1s->recv_wait = NULL;
525 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200526
527 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100528 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200529
Christopher Faulet129817b2018-09-20 16:14:40 +0200530 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100531 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200532
533 h1s->status = 0;
534 h1s->meth = HTTP_METH_OTHER;
535
Christopher Faulet47365272018-10-31 17:40:50 +0100536 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
537 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100538 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100539
Christopher Faulet129817b2018-09-20 16:14:40 +0200540 if (!conn_is_back(h1c->conn)) {
541 if (h1c->px->options2 & PR_O2_REQBUG_OK)
542 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200543
544 /* For frontend connections we should always have a session */
545 if (!sess)
546 sess = h1c->conn->owner;
547
Dave Pirotte234740f2019-07-10 13:57:38 +0000548 /* Timers for subsequent sessions on the same HTTP 1.x connection
549 * measure from `now`, not from the connection accept time */
550 if (h1s->flags & H1S_F_NOT_FIRST) {
551 h1s->csinfo.create_date = date;
552 h1s->csinfo.tv_create = now;
553 h1s->csinfo.t_handshake = 0;
554 h1s->csinfo.t_idle = -1;
555 }
556 else {
557 h1s->csinfo.create_date = sess->accept_date;
558 h1s->csinfo.tv_create = sess->tv_accept;
559 h1s->csinfo.t_handshake = sess->t_handshake;
560 h1s->csinfo.t_idle = -1;
561 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200562 }
563 else {
564 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
565 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200566
Christopher Fauletfeb11742018-11-29 15:12:34 +0100567 h1s->csinfo.create_date = date;
568 h1s->csinfo.tv_create = now;
569 h1s->csinfo.t_handshake = 0;
570 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200571 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100572
Christopher Faulete9b70722019-04-08 10:46:02 +0200573 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
574 * create a new one.
575 */
576 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200577 cs->ctx = h1s;
578 h1s->cs = cs;
579 }
Christopher Faulet47365272018-10-31 17:40:50 +0100580 else {
581 cs = h1s_new_cs(h1s);
582 if (!cs)
583 goto fail;
584 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200585 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200586 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100587
588 fail:
589 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200590 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100591 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200592}
593
594static void h1s_destroy(struct h1s *h1s)
595{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596 if (h1s) {
597 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200598
Christopher Faulet6b81df72019-10-01 22:08:43 +0200599 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200600 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200601
Christopher Fauletf2824e62018-10-01 12:12:37 +0200602 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100603 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200604 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100605 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200606
Christopher Fauletcb55f482018-12-10 11:56:47 +0100607 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200608 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100609 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200610 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
611 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100612
613 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
614 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
615 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
616 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
617 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
618 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
619 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200620 pool_free(pool_head_h1s, h1s);
621 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200622}
623
Christopher Fauletfeb11742018-11-29 15:12:34 +0100624static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
625{
626 struct h1s *h1s = cs->ctx;
627
628 if (h1s && !conn_is_back(cs->conn))
629 return &h1s->csinfo;
630 return NULL;
631}
632
Christopher Faulet51dbc942018-09-13 09:05:15 +0200633/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200634 * Initialize the mux once it's attached. It is expected that conn->ctx points
635 * to the existing conn_stream (for outgoing connections or for incoming onces
636 * during a mux upgrade) or NULL (for incoming ones during the connexion
637 * establishment). <input> is always used as Input buffer and may contain
638 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
639 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200640 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200641static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
642 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200643{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200644 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100645 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200646 void *conn_ctx = conn->ctx;
647
648 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200649
650 h1c = pool_alloc(pool_head_h1c);
651 if (!h1c)
652 goto fail_h1c;
653 h1c->conn = conn;
654 h1c->px = proxy;
655
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100656 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200657 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200658 h1c->obuf = BUF_NULL;
659 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200660 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200661
Christopher Faulet51dbc942018-09-13 09:05:15 +0200662 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200663 h1c->wait_event.tasklet = tasklet_new();
664 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200665 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200666 h1c->wait_event.tasklet->process = h1_io_cb;
667 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100668 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200669
Christopher Faulete9b70722019-04-08 10:46:02 +0200670 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100671 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
672 if (tick_isset(proxy->timeout.serverfin))
673 h1c->shut_timeout = proxy->timeout.serverfin;
674 } else {
675 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
676 if (tick_isset(proxy->timeout.clientfin))
677 h1c->shut_timeout = proxy->timeout.clientfin;
678 }
679 if (tick_isset(h1c->timeout)) {
680 t = task_new(tid_bit);
681 if (!t)
682 goto fail;
683
684 h1c->task = t;
685 t->process = h1_timeout_task;
686 t->context = h1c;
687 t->expire = tick_add(now_ms, h1c->timeout);
688 }
689
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100690 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200691
Christopher Faulet6b81df72019-10-01 22:08:43 +0200692 /* Always Create a new H1S */
693 if (!h1s_create(h1c, conn_ctx, sess))
694 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100695
696 if (t)
697 task_queue(t);
698
Christopher Faulet51dbc942018-09-13 09:05:15 +0200699 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200700 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200701
702 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200703 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200704 return 0;
705
706 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200707 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200708 if (h1c->wait_event.tasklet)
709 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200710 pool_free(pool_head_h1c, h1c);
711 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200712 conn->ctx = conn_ctx; // restore saved context
713 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200714 return -1;
715}
716
Christopher Faulet73c12072019-04-08 11:23:22 +0200717/* release function. This one should be called to free all resources allocated
718 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200719 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200720static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200721{
Christopher Faulet61840e72019-04-15 09:33:32 +0200722 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200723
Christopher Faulet6b81df72019-10-01 22:08:43 +0200724 TRACE_POINT(H1_EV_H1C_END);
725
Christopher Faulet51dbc942018-09-13 09:05:15 +0200726 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200727 /* The connection must be aattached to this mux to be released */
728 if (h1c->conn && h1c->conn->ctx == h1c)
729 conn = h1c->conn;
730
Christopher Faulet6b81df72019-10-01 22:08:43 +0200731 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
732
Christopher Faulet61840e72019-04-15 09:33:32 +0200733 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200734 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200735 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200736 /* Make sure we're no longer subscribed to anything */
737 if (h1c->wait_event.events)
738 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
739 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200740 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200741 /* connection successfully upgraded to H2, this
742 * mux was already released */
743 return;
744 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200745 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200746 sess_log(conn->owner); /* Log if the upgrade failed */
747 }
748
Olivier Houchard45c44372019-06-11 14:06:23 +0200749
Christopher Faulet51dbc942018-09-13 09:05:15 +0200750 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
751 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
752 LIST_DEL(&h1c->buf_wait.list);
753 LIST_INIT(&h1c->buf_wait.list);
754 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
755 }
756
757 h1_release_buf(h1c, &h1c->ibuf);
758 h1_release_buf(h1c, &h1c->obuf);
759
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100760 if (h1c->task) {
761 h1c->task->context = NULL;
762 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
763 h1c->task = NULL;
764 }
765
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200766 if (h1c->wait_event.tasklet)
767 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200768
Christopher Fauletf2824e62018-10-01 12:12:37 +0200769 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200770 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100771 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200772 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200773 pool_free(pool_head_h1c, h1c);
774 }
775
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200776 if (conn) {
777 conn->mux = NULL;
778 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200779 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200780
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200781 conn_stop_tracking(conn);
782 conn_full_close(conn);
783 if (conn->destroy_cb)
784 conn->destroy_cb(conn);
785 conn_free(conn);
786 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200787}
788
789/******************************************************/
790/* functions below are for the H1 protocol processing */
791/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200792/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
793 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200794 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100795static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200796{
Christopher Faulet570d1612018-11-26 11:13:57 +0100797 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200798
Christopher Faulet570d1612018-11-26 11:13:57 +0100799 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200800 (*(p + 5) > '1' ||
801 (*(p + 5) == '1' && *(p + 7) >= '1')))
802 h1m->flags |= H1_MF_VER_11;
803}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200804
Christopher Faulet9768c262018-10-22 09:34:31 +0200805/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
806 * greater or equal to 1.1
807 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100808static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200809{
Christopher Faulet570d1612018-11-26 11:13:57 +0100810 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200811
Christopher Faulet570d1612018-11-26 11:13:57 +0100812 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200813 (*(p + 5) > '1' ||
814 (*(p + 5) == '1' && *(p + 7) >= '1')))
815 h1m->flags |= H1_MF_VER_11;
816}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200817
Christopher Fauletf2824e62018-10-01 12:12:37 +0200818/* Deduce the connection mode of the client connection, depending on the
819 * configuration and the H1 message flags. This function is called twice, the
820 * first time when the request is parsed and the second time when the response
821 * is parsed.
822 */
823static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
824{
825 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200826
827 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100828 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200829 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100830 h1s->status == 101) {
831 /* Either we've established an explicit tunnel, or we're
832 * switching the protocol. In both cases, we're very unlikely to
833 * understand the next protocols. We have to switch to tunnel
834 * mode, so that we transfer the request and responses then let
835 * this protocol pass unmodified. When we later implement
836 * specific parsers for such protocols, we'll want to check the
837 * Upgrade header which contains information about that protocol
838 * for responses with status 101 (eg: see RFC2817 about TLS).
839 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200840 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200841 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 +0100842 }
843 else if (h1s->flags & H1S_F_WANT_KAL) {
844 /* By default the client is in KAL mode. CLOSE mode mean
845 * it is imposed by the client itself. So only change
846 * KAL mode here. */
847 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
848 /* no length known or explicit close => close */
849 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200850 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 +0100851 }
852 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
853 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
854 /* no explict keep-alive and option httpclose => close */
855 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200856 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 +0100857 }
858 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200859 }
860 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100861 /* Input direction: first pass */
862 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
863 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200864 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200865 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 +0100866 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200867 }
868
869 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200870 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
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("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
873 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200874}
875
876/* Deduce the connection mode of the client connection, depending on the
877 * configuration and the H1 message flags. This function is called twice, the
878 * first time when the request is parsed and the second time when the response
879 * is parsed.
880 */
881static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
882{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100883 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100884 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100885 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200886
Christopher Fauletf2824e62018-10-01 12:12:37 +0200887 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100888 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200889 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100890 h1s->status == 101) {
891 /* Either we've established an explicit tunnel, or we're
892 * switching the protocol. In both cases, we're very unlikely to
893 * understand the next protocols. We have to switch to tunnel
894 * mode, so that we transfer the request and responses then let
895 * this protocol pass unmodified. When we later implement
896 * specific parsers for such protocols, we'll want to check the
897 * Upgrade header which contains information about that protocol
898 * for responses with status 101 (eg: see RFC2817 about TLS).
899 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200900 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200901 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 +0100902 }
903 else if (h1s->flags & H1S_F_WANT_KAL) {
904 /* By default the server is in KAL mode. CLOSE mode mean
905 * it is imposed by haproxy itself. So only change KAL
906 * mode here. */
907 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
908 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
909 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
910 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200911 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 +0100912 }
913 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200914 }
Christopher Faulet70033782018-12-05 13:50:11 +0100915 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100916 /* Output direction: first pass */
917 if (h1m->flags & H1_MF_CONN_CLO) {
918 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100919 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200920 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 +0100921 }
922 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
923 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
924 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
925 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
926 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
927 /* no explicit keep-alive option httpclose/server-close => close */
928 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200929 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 +0100930 }
Christopher Faulet70033782018-12-05 13:50:11 +0100931 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200932
933 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200934 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200935 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200936 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);
937 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200938}
939
Christopher Fauletb992af02019-03-28 15:42:24 +0100940static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200941{
942 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200943
944 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
945 * token is found
946 */
947 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200948 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200949
950 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200951 if (!(h1m->flags & H1_MF_VER_11)) {
952 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 +0100953 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200954 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200955 }
956 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200957 if (h1m->flags & H1_MF_VER_11) {
958 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100959 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200960 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200961 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200962}
963
Christopher Fauletb992af02019-03-28 15:42:24 +0100964static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200965{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200966 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
967 * token is found
968 */
969 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200970 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200971
972 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100973 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +0200974 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
975 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 +0100976 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200977 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200978 }
979 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200980 if (h1m->flags & H1_MF_VER_11) {
981 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100982 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200983 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200984 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200985}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200986
Christopher Fauletb992af02019-03-28 15:42:24 +0100987static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200988{
Christopher Fauletb992af02019-03-28 15:42:24 +0100989 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200990 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100991 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200992 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200993}
994
Christopher Fauletb992af02019-03-28 15:42:24 +0100995static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
996{
997 if (!conn_is_back(h1s->h1c->conn))
998 h1_set_cli_conn_mode(h1s, h1m);
999 else
1000 h1_set_srv_conn_mode(h1s, h1m);
1001
1002 if (!(h1m->flags & H1_MF_RESP))
1003 h1_update_req_conn_value(h1s, h1m, conn_val);
1004 else
1005 h1_update_res_conn_value(h1s, h1m, conn_val);
1006}
Christopher Faulete44769b2018-11-29 23:01:45 +01001007
Christopher Faulet98fbe952019-07-22 16:18:24 +02001008/* Try to adjust the case of the message header name using the global map
1009 * <hdrs_map>.
1010 */
1011static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1012{
1013 struct ebpt_node *node;
1014 struct h1_hdr_entry *entry;
1015
1016 /* No entry in the map, do nothing */
1017 if (eb_is_empty(&hdrs_map.map))
1018 return;
1019
1020 /* No conversion fo the request headers */
1021 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1022 return;
1023
1024 /* No conversion fo the response headers */
1025 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1026 return;
1027
1028 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1029 if (!node)
1030 return;
1031 entry = container_of(node, struct h1_hdr_entry, node);
1032 name->ptr = entry->name.ptr;
1033 name->len = entry->name.len;
1034}
1035
Christopher Faulete44769b2018-11-29 23:01:45 +01001036/* Append the description of what is present in error snapshot <es> into <out>.
1037 * The description must be small enough to always fit in a buffer. The output
1038 * buffer may be the trash so the trash must not be used inside this function.
1039 */
1040static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1041{
1042 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001043 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1044 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1045 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1046 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1047 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1048 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001049}
1050/*
1051 * Capture a bad request or response and archive it in the proxy's structure.
1052 * By default it tries to report the error position as h1m->err_pos. However if
1053 * this one is not set, it will then report h1m->next, which is the last known
1054 * parsing point. The function is able to deal with wrapping buffers. It always
1055 * displays buffers as a contiguous area starting at buf->p. The direction is
1056 * determined thanks to the h1m's flags.
1057 */
1058static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1059 struct h1m *h1m, struct buffer *buf)
1060{
1061 struct session *sess = h1c->conn->owner;
1062 struct proxy *proxy = h1c->px;
1063 struct proxy *other_end = sess->fe;
1064 union error_snapshot_ctx ctx;
1065
Willy Tarreau598d7fc2018-12-18 18:10:38 +01001066 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +01001067 other_end = si_strm(h1s->cs->data)->be;
1068
1069 /* http-specific part now */
1070 ctx.h1.state = h1m->state;
1071 ctx.h1.c_flags = h1c->flags;
1072 ctx.h1.s_flags = h1s->flags;
1073 ctx.h1.m_flags = h1m->flags;
1074 ctx.h1.m_clen = h1m->curr_len;
1075 ctx.h1.m_blen = h1m->body_len;
1076
1077 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1078 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001079 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1080 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001081}
1082
Christopher Fauletadb22202018-12-12 10:32:09 +01001083/* Emit the chunksize followed by a CRLF in front of data of the buffer
1084 * <buf>. It goes backwards and starts with the byte before the buffer's
1085 * head. The caller is responsible for ensuring there is enough room left before
1086 * the buffer's head for the string.
1087 */
1088static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1089{
1090 char *beg, *end;
1091
1092 beg = end = b_head(buf);
1093 *--beg = '\n';
1094 *--beg = '\r';
1095 do {
1096 *--beg = hextab[chksz & 0xF];
1097 } while (chksz >>= 4);
1098 buf->head -= (end - beg);
1099 b_add(buf, end - beg);
1100}
1101
1102/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1103 * ensuring there is enough room left in the buffer for the string. */
1104static void h1_emit_chunk_crlf(struct buffer *buf)
1105{
1106 *(b_peek(buf, b_data(buf))) = '\r';
1107 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1108 b_add(buf, 2);
1109}
1110
Christopher Faulet129817b2018-09-20 16:14:40 +02001111/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001112 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001113 * CONNECT requests. On the client side, if the response is not finished, the
1114 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001115 */
1116static void h1_set_req_tunnel_mode(struct h1s *h1s)
1117{
1118 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1119 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001120 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1121
1122 if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001123 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001124 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1125 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001126 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1127 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1128 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1129 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1130 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001131}
1132
1133/*
1134 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletf3158e92019-11-15 11:14:23 +01001135 * successfull replies to CONNECT requests or on protocol switching. In this
1136 * last case, this function takes care to switch the request to tunnel mode if
1137 * possible. On the server side, if the request is not finished, the mux is mark
1138 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001139 */
1140static void h1_set_res_tunnel_mode(struct h1s *h1s)
1141{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001142 /* On protocol switching, switch the request to tunnel mode if it is in
1143 * DONE state. Otherwise we will wait the end of the request to switch
1144 * it in tunnel mode.
1145 */
1146 if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1147 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1148 h1s->req.state = H1_MSG_TUNNEL;
1149 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1150 }
1151
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001152 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1153 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001154 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1155
Christopher Faulet6b81df72019-10-01 22:08:43 +02001156 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001157 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001158 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1159 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001160 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1161 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1162 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1163 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 +01001164 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001165}
1166
1167/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001168 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001169 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001170 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001171 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001172static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001173 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001174{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001175 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001176 int ret = 0;
1177
Christopher Faulet6b81df72019-10-01 22:08:43 +02001178 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1179
Christopher Fauleteec96b52019-09-25 09:10:46 +02001180 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001181 /* Try to match H2 preface before parsing the request headers. */
1182 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001183 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001184 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001185 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001186 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001187 else {
1188 if (h1s->meth == HTTP_METH_CONNECT)
1189 h1m->flags |= H1_MF_METH_CONNECT;
1190 if (h1s->meth == HTTP_METH_HEAD)
1191 h1m->flags |= H1_MF_METH_HEAD;
1192 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001193
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001194 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1195 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001196 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 +02001197 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001198 if (!(h1m->flags & H1_MF_RESP)) {
1199 h1s->flags |= H1S_F_REQ_ERROR;
1200 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1201 }
1202 else {
1203 h1s->flags |= H1S_F_RES_ERROR;
1204 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1205 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001206 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001207 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 +02001208 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1209 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001210 goto end;
1211 }
1212
Christopher Faulet486498c2019-10-11 14:22:00 +02001213 if (h1m->err_pos >= 0) {
1214 /* Maybe we found an error during the parsing while we were
1215 * configured not to block on that, so we have to capture it
1216 * now.
1217 */
1218 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1219 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1220 }
1221
Christopher Faulet129817b2018-09-20 16:14:40 +02001222 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001223 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001224 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001225 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001226 }
1227 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001228 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001229 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001230 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001231 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001232 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001233 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001234
Christopher Faulet129817b2018-09-20 16:14:40 +02001235 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001236 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001237 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001238
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001239 h2c_upgrade:
1240 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001241 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001242 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001243 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1244 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001245}
1246
1247/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001248 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001249 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1250 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001251 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001252static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001253 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001254 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001255{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001256 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001257
Christopher Faulet6b81df72019-10-01 22:08:43 +02001258 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001259 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001260 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001261 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 +01001262 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001263 if (!(h1m->flags & H1_MF_RESP)) {
1264 h1s->flags |= H1S_F_REQ_ERROR;
1265 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1266 }
1267 else {
1268 h1s->flags |= H1S_F_RES_ERROR;
1269 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1270 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001271 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001272 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 +02001273 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001274 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001275 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001276 }
1277
Christopher Faulet02a02532019-11-15 09:36:28 +01001278 *ofs += ret;
1279
1280 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001281 if (h1m->state == H1_MSG_DONE) {
Christopher Faulet7aae8582019-12-06 15:59:05 +01001282 h1s->flags &= ~H1S_F_APPEND_EOM;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001283 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001284 TRACE_STATE("end of message", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_RX_EOI, h1s->h1c->conn);
1285 }
Christopher Faulet7aae8582019-12-06 15:59:05 +01001286 else if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_XFER_LEN) && h1m->curr_len == 0) {
1287 h1s->flags |= H1S_F_APPEND_EOM;
1288 TRACE_STATE("add append_eom", H1_EV_RX_DATA, h1s->h1c->conn);
1289 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001290
Christopher Faulet6b81df72019-10-01 22:08:43 +02001291 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001292 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001293}
1294
1295/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001296 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1297 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1298 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1299 * responsible to update the parser state <h1m>.
1300 */
1301static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1302 struct buffer *buf, size_t *ofs, size_t max)
1303{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001304 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001305
Christopher Faulet6b81df72019-10-01 22:08:43 +02001306 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001307 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001308 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001309 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 +01001310 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001311 if (!(h1m->flags & H1_MF_RESP)) {
1312 h1s->flags |= H1S_F_REQ_ERROR;
1313 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1314 }
1315 else {
1316 h1s->flags |= H1S_F_RES_ERROR;
1317 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1318 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001319 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001320 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 +02001321 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1322 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001323 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001324 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001325
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001326 *ofs += ret;
1327 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet02a02532019-11-15 09:36:28 +01001328
1329 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001330 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001331 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001332}
1333
1334/*
1335 * Add the EOM in the HTX message and switch the message to the DONE state. It
1336 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
1337 * functions is responsible to update the parser state <h1m>. It also add the
1338 * flag CS_FL_EOI on the CS.
1339 */
1340static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1341{
Christopher Faulet6b81df72019-10-01 22:08:43 +02001342 TRACE_ENTER(H1_EV_RX_DATA, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001343 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1344 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001345 TRACE_STATE("leaving on append_eom", H1_EV_RX_DATA, h1s->h1c->conn);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001346 return 0;
1347 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001348
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001349 h1s->flags &= ~H1S_F_APPEND_EOM;
1350 h1m->state = H1_MSG_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001351 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001352 TRACE_STATE("end of message", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1353 TRACE_LEAVE(H1_EV_RX_DATA, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001354 return (sizeof(struct htx_blk) + 1);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001355}
1356
1357/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001358 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001359 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1360 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001361 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001362static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001363{
Christopher Faulet539e0292018-11-19 10:40:09 +01001364 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001365 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001366 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001367 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001368 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001369 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001370
Christopher Faulet539e0292018-11-19 10:40:09 +01001371 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001372 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001373
Christopher Fauletf2824e62018-10-01 12:12:37 +02001374 if (!conn_is_back(h1c->conn)) {
1375 h1m = &h1s->req;
1376 errflag = H1S_F_REQ_ERROR;
1377 }
1378 else {
1379 h1m = &h1s->res;
1380 errflag = H1S_F_RES_ERROR;
1381 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001382
Christopher Faulet74027762019-02-26 14:45:05 +01001383 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001384 if (h1s->flags & errflag)
1385 goto end;
1386
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001387 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001388 size_t used = htx_used_space(htx);
1389
Christopher Faulet129817b2018-09-20 16:14:40 +02001390 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001391 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001392 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001393 if (!ret)
1394 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001395
1396 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1397 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1398
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001399 if ((h1m->flags & H1_MF_RESP) &&
1400 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1401 h1m_init_res(&h1s->res);
1402 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001403 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001404 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001405 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001406 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001407 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001408 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001409 if (!ret)
1410 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001411
1412 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1413 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
1414
1415 if (h1m->state == H1_MSG_DONE)
1416 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1417 H1_EV_RX_DATA, h1c->conn, h1s, htx);
Christopher Faulet129817b2018-09-20 16:14:40 +02001418 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001419 else if (h1m->state == H1_MSG_TRAILERS) {
1420 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001421 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001422 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1423 if (!ret)
1424 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001425
1426 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1427 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001428 }
Christopher Fauletf1ef7f62019-09-03 21:55:14 +02001429 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001430 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001431
1432 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1433 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001434 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001435 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletf3158e92019-11-15 11:14:23 +01001436 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1437 h1_set_req_tunnel_mode(h1s);
1438 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001439 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001440 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1441 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001442 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001443 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001444 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001445 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001446 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001447 if (!ret)
1448 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001449
1450 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1451 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001452 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001453 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001454 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 break;
1456 }
1457
Christopher Faulet30db3d72019-05-17 15:35:33 +02001458 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001459 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001460
Christopher Faulet6b81df72019-10-01 22:08:43 +02001461 if (h1s->flags & errflag) {
1462 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001463 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001464 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001465
Christopher Faulet539e0292018-11-19 10:40:09 +01001466 b_del(&h1c->ibuf, total);
1467
1468 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001469 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001470 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001471 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001472 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001473 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 +02001474 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001475 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001476
Christopher Fauletcf56b992018-12-11 16:12:31 +01001477 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1478
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001479 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001480 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet7aae8582019-12-06 15:59:05 +01001481
1482 if ((h1s_data_pending(h1s) && !htx_is_empty(htx)) || (h1s->flags & H1S_F_APPEND_EOM))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001483 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001484
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001485 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1486 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001487 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001488 if (h1m->state == H1_MSG_TUNNEL)
1489 h1s->cs->flags |= CS_FL_EOI;
1490 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001491 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001492 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001493
Christopher Faulet6b81df72019-10-01 22:08:43 +02001494 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001495 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001496
1497 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001498 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001499 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001500 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001501 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001502}
1503
Christopher Faulet129817b2018-09-20 16:14:40 +02001504/*
1505 * Process outgoing data. It parses data and transfer them from the channel buffer into
1506 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1507 * 0 if it couldn't proceed.
1508 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001509static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1510{
Christopher Faulet129817b2018-09-20 16:14:40 +02001511 struct h1s *h1s = h1c->h1s;
1512 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001513 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001514 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001515 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001516 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001517 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001518
Christopher Faulet47365272018-10-31 17:40:50 +01001519 if (!count)
1520 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001521
Christopher Faulet94b2c762019-05-24 15:28:57 +02001522 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001523 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1524
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001525 if (htx_is_empty(chn_htx))
1526 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001527
Christopher Faulet51dbc942018-09-13 09:05:15 +02001528 if (!h1_get_buf(h1c, &h1c->obuf)) {
1529 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001530 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 +02001531 goto end;
1532 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001533
Christopher Fauletf2824e62018-10-01 12:12:37 +02001534 if (!conn_is_back(h1c->conn)) {
1535 h1m = &h1s->res;
1536 errflag = H1S_F_RES_ERROR;
1537 }
1538 else {
1539 h1m = &h1s->req;
1540 errflag = H1S_F_REQ_ERROR;
1541 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001542
Christopher Faulet0e54d542019-07-04 21:22:34 +02001543 if (h1s->flags & errflag)
1544 goto end;
1545
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001546 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001547 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001548
Willy Tarreau3815b222018-12-11 19:50:43 +01001549 /* Perform some optimizations to reduce the number of buffer copies.
1550 * First, if the mux's buffer is empty and the htx area contains
1551 * exactly one data block of the same size as the requested count,
1552 * then it's possible to simply swap the caller's buffer with the
1553 * mux's output buffer and adjust offsets and length to match the
1554 * entire DATA HTX block in the middle. In this case we perform a
1555 * true zero-copy operation from end-to-end. This is the situation
1556 * that happens all the time with large files. Second, if this is not
1557 * possible, but the mux's output buffer is empty, we still have an
1558 * opportunity to avoid the copy to the intermediary buffer, by making
1559 * the intermediary buffer's area point to the output buffer's area.
1560 * In this case we want to skip the HTX header to make sure that copies
1561 * remain aligned and that this operation remains possible all the
1562 * time. This goes for headers, data blocks and any data extracted from
1563 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001564 */
1565 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001566 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001567 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001568 htx_get_blk_value(chn_htx, blk).len == count) {
1569 void *old_area = h1c->obuf.area;
1570
Christopher Faulet6b81df72019-10-01 22:08:43 +02001571 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 +01001572 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001573 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001574 h1c->obuf.data = count;
1575
1576 buf->area = old_area;
1577 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001578
Christopher Faulet6b81df72019-10-01 22:08:43 +02001579 chn_htx = (struct htx *)buf->area;
1580 htx_reset(chn_htx);
1581
Christopher Fauletadb22202018-12-12 10:32:09 +01001582 /* The message is chunked. We need to emit the chunk
1583 * size. We have at least the size of the struct htx to
1584 * write the chunk envelope. It should be enough.
1585 */
1586 if (h1m->flags & H1_MF_CHNK) {
1587 h1_emit_chunk_size(&h1c->obuf, count);
1588 h1_emit_chunk_crlf(&h1c->obuf);
1589 }
1590
Willy Tarreau3815b222018-12-11 19:50:43 +01001591 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001592 if (h1m->state == H1_MSG_DATA)
1593 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001594 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001595 else
1596 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001597 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001598 goto out;
1599 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001600 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001601 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001602 else
1603 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001604
Christopher Fauletc2518a52019-06-25 21:41:02 +02001605 tmp.data = 0;
1606 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001607 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001608 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001609 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001610 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001611 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001612 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001613
Christopher Fauletb2e84162018-12-06 11:39:49 +01001614 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001615 if (type != HTX_BLK_DATA && vlen > count)
1616 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001617
Christopher Faulet94b2c762019-05-24 15:28:57 +02001618 if (type == HTX_BLK_UNUSED)
1619 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001620
Christopher Faulet94b2c762019-05-24 15:28:57 +02001621 switch (h1m->state) {
1622 case H1_MSG_RQBEFORE:
1623 if (type != HTX_BLK_REQ_SL)
1624 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001625 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 +02001626 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001627 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001628 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001629 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001630 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001631 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001632 if (sl->flags & HTX_SL_F_BODYLESS)
1633 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001634 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001635 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001636
Christopher Faulet94b2c762019-05-24 15:28:57 +02001637 case H1_MSG_RPBEFORE:
1638 if (type != HTX_BLK_RES_SL)
1639 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001640 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 +02001641 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001642 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001643 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001644 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001645 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001646 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001647 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001648 if (sl->info.res.status < 200 &&
1649 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001650 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001651 h1m->state = H1_MSG_HDR_FIRST;
1652 break;
1653
Christopher Faulet94b2c762019-05-24 15:28:57 +02001654 case H1_MSG_HDR_FIRST:
1655 case H1_MSG_HDR_NAME:
1656 case H1_MSG_HDR_L2_LWS:
1657 if (type == HTX_BLK_EOH)
1658 goto last_lf;
1659 if (type != HTX_BLK_HDR)
1660 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001661
Christopher Faulet9768c262018-10-22 09:34:31 +02001662 h1m->state = H1_MSG_HDR_NAME;
1663 n = htx_get_blk_name(chn_htx, blk);
1664 v = htx_get_blk_value(chn_htx, blk);
1665
Christopher Faulet86d144c2019-08-14 16:32:25 +02001666 /* Skip all pseudo-headers */
1667 if (*(n.ptr) == ':')
1668 goto skip_hdr;
1669
Christopher Faulet9768c262018-10-22 09:34:31 +02001670 if (isteqi(n, ist("transfer-encoding")))
1671 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001672 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001673 /* Only skip C-L header with invalid value. */
1674 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001675 goto skip_hdr;
1676 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001677 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001678 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 if (!v.len)
1680 goto skip_hdr;
1681 }
1682
Christopher Faulet67d58092019-10-02 10:51:38 +02001683 /* Skip header if same name is used to add the server name */
1684 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1685 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1686 goto skip_hdr;
1687
Christopher Faulet98fbe952019-07-22 16:18:24 +02001688 /* Try to adjust the case of the header name */
1689 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1690 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001691 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001692 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001693 skip_hdr:
1694 h1m->state = H1_MSG_HDR_L2_LWS;
1695 break;
1696
Christopher Faulet94b2c762019-05-24 15:28:57 +02001697 case H1_MSG_LAST_LF:
1698 if (type != HTX_BLK_EOH)
1699 goto error;
1700 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001701 h1m->state = H1_MSG_LAST_LF;
1702 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001703 /* If the reply comes from haproxy while the request is
1704 * not finished, we force the connection close. */
1705 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1706 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1707 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1708 }
1709
1710 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001711 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001712 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001713 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001714 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001715 /* Try to adjust the case of the header name */
1716 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1717 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001718 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001719 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001720 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001721 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001722 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001723
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001724 if ((h1s->meth != HTTP_METH_CONNECT &&
1725 (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 +01001726 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1727 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1728 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1729 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1730 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001731 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001732 n = ist("transfer-encoding");
1733 v = ist("chunked");
1734 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1735 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001736 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001737 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001738 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001739 h1m->flags |= H1_MF_CHNK;
1740 }
1741
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001742 /* Now add the server name to a header (if requested) */
1743 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1744 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1745 struct server *srv = objt_server(h1c->conn->target);
1746
1747 if (srv) {
1748 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1749 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001750
1751 /* Try to adjust the case of the header name */
1752 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1753 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001754 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001755 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001756 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001757 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001758 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1759 }
1760
Christopher Fauletc2518a52019-06-25 21:41:02 +02001761 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001762 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001763
Christopher Faulet6b81df72019-10-01 22:08:43 +02001764 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1765 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1766
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001767 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1768 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1769 h1_set_req_tunnel_mode(h1s);
1770 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001771 else if ((h1m->flags & H1_MF_RESP) &&
1772 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001773 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001774 * to the client. Switch the response to tunnel mode.
1775 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001776 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001777 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 +01001778 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001779 else if ((h1m->flags & H1_MF_RESP) &&
1780 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1781 h1m_init_res(&h1s->res);
1782 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001783 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001784 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001785 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001786 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001787 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001788 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1789 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001790 else
1791 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001792 break;
1793
Christopher Faulet94b2c762019-05-24 15:28:57 +02001794 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001795 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001796 if (type == HTX_BLK_EOM) {
1797 /* Chunked message without explicit trailers */
1798 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001799 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001800 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001801 }
1802 goto done;
1803 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001804 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001805 /* If the message is not chunked, never
1806 * add the last chunk. */
1807 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001808 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001809 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 +02001810 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001811 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001812 else if (type != HTX_BLK_DATA)
1813 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001814
1815 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 +02001816
1817
1818 if (vlen > count) {
1819 /* Get the maximum amount of data we can xferred */
1820 vlen = count;
1821 }
1822
1823 chklen = 0;
1824 if (h1m->flags & H1_MF_CHNK) {
1825 chklen = b_room(&tmp);
1826 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1827 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1828 (chklen < 1048576) ? 5 : 8);
1829 chklen += 4; /* 2 x CRLF */
1830 }
1831
1832 if (vlen + chklen > b_room(&tmp)) {
1833 /* too large for the buffer */
1834 if (chklen >= b_room(&tmp))
1835 goto full;
1836 vlen = b_room(&tmp) - chklen;
1837 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001838 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001839 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001840 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001841 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001842
1843 if (h1m->state == H1_MSG_DATA)
1844 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001845 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001846 else
1847 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001848 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001849 break;
1850
Christopher Faulet94b2c762019-05-24 15:28:57 +02001851 case H1_MSG_TRAILERS:
1852 if (type == HTX_BLK_EOM)
1853 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001854 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001855 goto error;
1856 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001857 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001858 /* If the message is not chunked, ignore
1859 * trailers. It may happen with H2 messages. */
1860 if (!(h1m->flags & H1_MF_CHNK))
1861 break;
1862
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001863 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001864 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001865 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001866 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1867 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001868 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001869 else { // HTX_BLK_TLR
1870 n = htx_get_blk_name(chn_htx, blk);
1871 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001872
1873 /* Try to adjust the case of the header name */
1874 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1875 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001876 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001877 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001878 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001879 break;
1880
Christopher Faulet94b2c762019-05-24 15:28:57 +02001881 case H1_MSG_DONE:
1882 if (type != HTX_BLK_EOM)
1883 goto error;
1884 done:
1885 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001886 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1887 h1_set_req_tunnel_mode(h1s);
1888 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1889 }
1890 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001891 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1892 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001893 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 +02001894 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001895
1896 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1897 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001898 break;
1899
Christopher Faulet9768c262018-10-22 09:34:31 +02001900 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001901 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001902 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001903 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001904 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001905 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001906 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001907 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1908 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001909 break;
1910 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001911
1912 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001913 total += vlen;
1914 count -= vlen;
1915 if (sz == vlen)
1916 blk = htx_remove_blk(chn_htx, blk);
1917 else {
1918 htx_cut_data_blk(chn_htx, blk, vlen);
1919 break;
1920 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001921 }
1922
Christopher Faulet9768c262018-10-22 09:34:31 +02001923 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001924 /* when the output buffer is empty, tmp shares the same area so that we
1925 * only have to update pointers and lengths.
1926 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001927 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1928 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001929 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001930 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931
Willy Tarreau3815b222018-12-11 19:50:43 +01001932 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001933 out:
1934 if (!buf_room_for_htx_data(&h1c->obuf)) {
1935 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001936 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001937 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001938 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001939 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001940 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001941
1942 full:
1943 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1944 h1c->flags |= H1C_F_OUT_FULL;
1945 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001946}
1947
Christopher Faulet51dbc942018-09-13 09:05:15 +02001948/*********************************************************/
1949/* functions below are I/O callbacks from the connection */
1950/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001951static void h1_wake_stream_for_recv(struct h1s *h1s)
1952{
1953 if (h1s && h1s->recv_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001954 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001955 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001956 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001957 h1s->recv_wait = NULL;
1958 }
1959}
1960static void h1_wake_stream_for_send(struct h1s *h1s)
1961{
1962 if (h1s && h1s->send_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001963 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001964 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001965 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001966 h1s->send_wait = NULL;
1967 }
1968}
1969
Christopher Faulet51dbc942018-09-13 09:05:15 +02001970/*
1971 * Attempt to read data, and subscribe if none available
1972 */
1973static int h1_recv(struct h1c *h1c)
1974{
1975 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001976 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001977 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001978 int rcvd = 0;
1979
Christopher Faulet6b81df72019-10-01 22:08:43 +02001980 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
1981
1982 if (h1c->wait_event.events & SUB_RETRY_RECV) {
1983 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01001984 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001985 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986
Olivier Houchard75159a92018-12-03 18:46:09 +01001987 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001988 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01001989 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001990 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001991 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001992
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001993 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1994 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001995 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02001996 goto end;
1997 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001998
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001999 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002000 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002001 h1_wake_stream_for_recv(h1s);
2002 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002003 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002004 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002005 }
2006
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002007 /*
2008 * If we only have a small amount of data, realign it,
2009 * it's probably cheaper than doing 2 recv() calls.
2010 */
2011 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2012 b_slow_realign(&h1c->ibuf, trash.area, 0);
2013
Willy Tarreau45f2b892018-12-05 07:59:27 +01002014 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002015 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002016 if (h1c->flags & H1C_F_IN_FULL) {
2017 h1c->flags &= ~H1C_F_IN_FULL;
2018 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2019 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002020
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002021 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002022 if (!b_data(&h1c->ibuf)) {
2023 /* try to pre-align the buffer like the rxbufs will be
2024 * to optimize memory copies.
2025 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002026 h1c->ibuf.head = sizeof(struct htx);
2027 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002028 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002029 }
Christopher Faulet47365272018-10-31 17:40:50 +01002030 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002031 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn,,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002032 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002033 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002034 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002035 if (h1s->csinfo.t_idle == -1)
2036 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2037 }
Christopher Faulet47365272018-10-31 17:40:50 +01002038 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002039
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002040 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002041 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002042 goto end;
2043 }
2044
Christopher Faulet6b81df72019-10-01 22:08:43 +02002045 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002046 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002047
Christopher Faulet9768c262018-10-22 09:34:31 +02002048 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002049 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2050 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002051
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002052 if (conn_xprt_read0_pending(conn) && h1s) {
2053 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002054 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002055 rcvd = 1;
2056 }
2057
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058 if (!b_data(&h1c->ibuf))
2059 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002060 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002061 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002062 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2063 }
2064
2065 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066 return rcvd;
2067}
2068
2069
2070/*
2071 * Try to send data if possible
2072 */
2073static int h1_send(struct h1c *h1c)
2074{
2075 struct connection *conn = h1c->conn;
2076 unsigned int flags = 0;
2077 size_t ret;
2078 int sent = 0;
2079
Christopher Faulet6b81df72019-10-01 22:08:43 +02002080 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2081
2082 if (conn->flags & CO_FL_ERROR) {
2083 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002084 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002085 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002086
2087 if (!b_data(&h1c->obuf))
2088 goto end;
2089
2090 if (h1c->flags & H1C_F_OUT_FULL)
2091 flags |= CO_SFL_MSG_MORE;
2092
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002093 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002094 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002095 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn,,, (size_t[]){ret});
2096 if (h1c->flags & H1C_F_OUT_FULL) {
2097 h1c->flags &= ~H1C_F_OUT_FULL;
2098 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2099 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002100 b_del(&h1c->obuf, ret);
2101 sent = 1;
2102 }
2103
Christopher Faulet145aa472018-12-06 10:56:20 +01002104 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002105 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002106 /* error or output closed, nothing to send, clear the buffer to release it */
2107 b_reset(&h1c->obuf);
2108 }
2109
Christopher Faulet51dbc942018-09-13 09:05:15 +02002110 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002111 if (!(h1c->flags & H1C_F_OUT_FULL))
2112 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002113
Christopher Faulet51dbc942018-09-13 09:05:15 +02002114 /* We're done, no more to send */
2115 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002116 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002117 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002118 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2119 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002120 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002121 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002122 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002123 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2124 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002125 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002126 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002127
Christopher Faulet6b81df72019-10-01 22:08:43 +02002128 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129 return sent;
2130}
2131
Christopher Faulet51dbc942018-09-13 09:05:15 +02002132
2133/* callback called on any event by the connection handler.
2134 * It applies changes and returns zero, or < 0 if it wants immediate
2135 * destruction of the connection.
2136 */
2137static int h1_process(struct h1c * h1c)
2138{
2139 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002140 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002141
Christopher Faulet6b81df72019-10-01 22:08:43 +02002142 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2143
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002144 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002145 return -1;
2146
Christopher Fauletfeb11742018-11-29 15:12:34 +01002147 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002148 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002149 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002150 goto release;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002151 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002152 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002153 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002154 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002155 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002156 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002157 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002158 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159 }
2160
Christopher Fauletfeb11742018-11-29 15:12:34 +01002161 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2162 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2163
Christopher Faulet6b81df72019-10-01 22:08:43 +02002164 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002165 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002166 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2167 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002168
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002169 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002170 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002171 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002172 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002173 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002174 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002175 h1s->cs->data_cb->wake(h1s->cs);
2176 }
Christopher Faulet47365272018-10-31 17:40:50 +01002177 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002178 if (h1c->task) {
2179 h1c->task->expire = TICK_ETERNITY;
2180 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002181 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 +01002182 ? h1c->shut_timeout
2183 : h1c->timeout));
2184 task_queue(h1c->task);
2185 }
2186 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002187 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002188 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002189
2190 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002191 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002192 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002193 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002194}
2195
2196static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2197{
2198 struct h1c *h1c = ctx;
2199 int ret = 0;
2200
Christopher Faulet6b81df72019-10-01 22:08:43 +02002201 TRACE_POINT(H1_EV_H1C_WAKE, h1c->conn);
2202
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002203 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002204 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002205 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002206 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002207 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208 h1_process(h1c);
2209 return NULL;
2210}
2211
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002212static void h1_reset(struct connection *conn)
2213{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002214
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002215}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002216
2217static int h1_wake(struct connection *conn)
2218{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002219 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002220 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002221
Christopher Faulet6b81df72019-10-01 22:08:43 +02002222 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2223
Christopher Faulet539e0292018-11-19 10:40:09 +01002224 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002225 ret = h1_process(h1c);
2226 if (ret == 0) {
2227 struct h1s *h1s = h1c->h1s;
2228
Christopher Faulet6b81df72019-10-01 22:08:43 +02002229 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2230 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002231 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002232 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002233 }
2234 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002235}
2236
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002237/* Connection timeout management. The principle is that if there's no receipt
2238 * nor sending for a certain amount of time, the connection is closed.
2239 */
2240static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2241{
2242 struct h1c *h1c = context;
2243 int expired = tick_is_expired(t->expire, now_ms);
2244
Christopher Faulet6b81df72019-10-01 22:08:43 +02002245 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2246
2247 if (!expired && h1c) {
2248 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002249 return t;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002250 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002251
Olivier Houchard3f795f72019-04-17 22:51:06 +02002252 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002253
2254 if (!h1c) {
2255 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002256 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002257 return NULL;
2258 }
2259
2260 h1c->task = NULL;
2261 /* If a stream is still attached to the mux, just set an error and wait
2262 * for the stream's timeout. Otherwise, release the mux. This is only ok
2263 * because same timeouts are used.
2264 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002265 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002266 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002267 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2268 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002269 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002270 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002271
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002272 return NULL;
2273}
2274
Christopher Faulet51dbc942018-09-13 09:05:15 +02002275/*******************************************/
2276/* functions below are used by the streams */
2277/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002278
Christopher Faulet51dbc942018-09-13 09:05:15 +02002279/*
2280 * Attach a new stream to a connection
2281 * (Used for outgoing connections)
2282 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002283static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002284{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002285 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002286 struct conn_stream *cs = NULL;
2287 struct h1s *h1s;
2288
Christopher Faulet6b81df72019-10-01 22:08:43 +02002289 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2290 if (h1c->flags & H1C_F_CS_ERROR) {
2291 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 +02002292 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002293 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002294
2295 cs = cs_new(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002296 if (!cs) {
2297 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 +02002298 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002299 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002300
Olivier Houchardf502aca2018-12-14 19:42:40 +01002301 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002302 if (h1s == NULL) {
2303 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 +02002304 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002305 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002306
Christopher Faulet6b81df72019-10-01 22:08:43 +02002307 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002308 return cs;
2309 end:
2310 cs_free(cs);
2311 return NULL;
2312}
2313
2314/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2315 * this mux, it's easy as we can only store a single conn_stream.
2316 */
2317static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2318{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002319 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002320 struct h1s *h1s = h1c->h1s;
2321
2322 if (h1s)
2323 return h1s->cs;
2324
2325 return NULL;
2326}
2327
Christopher Faulet73c12072019-04-08 11:23:22 +02002328static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002329{
Christopher Faulet73c12072019-04-08 11:23:22 +02002330 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002331
Christopher Faulet6b81df72019-10-01 22:08:43 +02002332 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002333 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002334 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002335}
2336
2337/*
2338 * Detach the stream from the connection and possibly release the connection.
2339 */
2340static void h1_detach(struct conn_stream *cs)
2341{
2342 struct h1s *h1s = cs->ctx;
2343 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002344 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002345 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002346
Christopher Faulet6b81df72019-10-01 22:08:43 +02002347 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2348
Christopher Faulet51dbc942018-09-13 09:05:15 +02002349 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002350 if (!h1s) {
2351 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002352 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002353 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354
Olivier Houchardf502aca2018-12-14 19:42:40 +01002355 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002356 h1c = h1s->h1c;
2357 h1s->cs = NULL;
2358
Olivier Houchard8a786902018-12-15 16:05:40 +01002359 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2360 h1s_destroy(h1s);
2361
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002362 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002363 /* If there are any excess server data in the input buffer,
2364 * release it and close the connection ASAP (some data may
2365 * remain in the output buffer). This happens if a server sends
2366 * invalid responses. So in such case, we don't want to reuse
2367 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002368 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002369 if (b_data(&h1c->ibuf)) {
2370 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002371 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002372 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002373 goto release;
2374 }
Christopher Faulet03627242019-07-19 11:34:08 +02002375
Christopher Faulet9400a392018-11-23 23:10:39 +01002376 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002377 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002378 h1c->conn->flags |= CO_FL_PRIVATE;
2379
Olivier Houchard44d59142018-12-13 18:46:22 +01002380 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002381 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002382 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2383 h1c->conn->owner = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002384 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) {
Olivier Houchard351411f2018-12-27 17:20:54 +01002385 /* The server doesn't want it, let's kill the connection right away */
2386 h1c->conn->mux->destroy(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002387 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2388 goto end;
2389 }
2390 tasklet_wakeup(h1c->wait_event.tasklet);
2391 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2392 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002393 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002394 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002395 if (h1c->conn->owner == sess) {
2396 int ret = session_check_idle_conn(sess, h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002397 if (ret == -1) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002398 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002399 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2400 goto end;
2401 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002402 else if (ret == 1) {
2403 /* The connection was added to the server list,
2404 * wake the task so we can subscribe to events
2405 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002406 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002407 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2408 goto end;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002409 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002410 TRACE_DEVEL("connection in idle session list", H1_EV_STRM_END, h1c->conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002411 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002412 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002413 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002414 struct server *srv = objt_server(h1c->conn->target);
2415
2416 if (srv) {
2417 if (h1c->conn->flags & CO_FL_PRIVATE)
2418 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002419 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002420 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2421 else
2422 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002423 TRACE_DEVEL("connection in idle server list", H1_EV_STRM_END, h1c->conn);
Christopher Faulet9400a392018-11-23 23:10:39 +01002424 }
2425 }
2426 }
2427
Christopher Fauletf1204b82019-07-19 14:51:06 +02002428 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002429 /* 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 +02002430 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2431 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2432 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002433 !h1c->conn->owner) {
2434 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002435 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002436 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002437 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002438 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002439 if (h1c->task) {
2440 h1c->task->expire = TICK_ETERNITY;
2441 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002442 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 +01002443 ? h1c->shut_timeout
2444 : h1c->timeout));
2445 task_queue(h1c->task);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002446 TRACE_DEVEL("refreshing connection's timeout", H1_EV_STRM_END, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002447 }
2448 }
2449 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002450 end:
2451 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002452}
2453
2454
2455static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2456{
2457 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002458 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002459
2460 if (!h1s)
2461 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002462 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002463
Christopher Faulet6b81df72019-10-01 22:08:43 +02002464 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2465
2466 if (cs->flags & CS_FL_KILL_CONN) {
2467 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2468 goto do_shutr;
2469 }
2470 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2471 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002472 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002473 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002474
Christopher Faulet6b81df72019-10-01 22:08:43 +02002475 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2476 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2477 goto end;
2478 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002479
Christopher Faulet7f366362019-04-08 10:51:20 +02002480 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002481 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2482 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002483 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002484 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002485 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002486 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002487 end:
2488 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002489}
2490
2491static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2492{
2493 struct h1s *h1s = cs->ctx;
2494 struct h1c *h1c;
2495
2496 if (!h1s)
2497 return;
2498 h1c = h1s->h1c;
2499
Christopher Faulet6b81df72019-10-01 22:08:43 +02002500 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2501
2502 if (cs->flags & CS_FL_KILL_CONN) {
2503 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002504 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002505 }
2506 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2507 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2508 goto do_shutw;
2509 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002510
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002511 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002512 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2513 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2514 goto end;
2515 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002516
Christopher Faulet7f366362019-04-08 10:51:20 +02002517 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002518 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2519 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002520 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002521 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002522 end:
2523 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002524}
2525
Christopher Faulet666a0c42019-01-08 11:12:04 +01002526static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002527{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002528 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002529
Christopher Faulet6b81df72019-10-01 22:08:43 +02002530 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002531 conn_xprt_shutw(conn);
2532 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002533 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002534 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002535}
2536
2537/* Called from the upper layer, to unsubscribe to events */
2538static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2539{
2540 struct wait_event *sw;
2541 struct h1s *h1s = cs->ctx;
2542
2543 if (!h1s)
2544 return 0;
2545
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002546 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002547 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002548 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002549 BUG_ON(h1s->recv_wait != sw);
2550 sw->events &= ~SUB_RETRY_RECV;
2551 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002552 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002553 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002554 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002555 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002556 BUG_ON(h1s->send_wait != sw);
2557 sw->events &= ~SUB_RETRY_SEND;
2558 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002559 }
2560 return 0;
2561}
2562
2563/* Called from the upper layer, to subscribe to events, such as being able to send */
2564static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2565{
2566 struct wait_event *sw;
2567 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002568 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002569
2570 if (!h1s)
2571 return -1;
2572
Christopher Faulet6b81df72019-10-01 22:08:43 +02002573 if (event_type & SUB_RETRY_RECV) {
2574 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
2575 sw = param;
2576 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2577 sw->events |= SUB_RETRY_RECV;
2578 h1s->recv_wait = sw;
2579 event_type &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002580 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002581 if (event_type & SUB_RETRY_SEND) {
2582 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
2583 sw = param;
2584 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2585 sw->events |= SUB_RETRY_SEND;
2586 h1s->send_wait = sw;
2587 event_type &= ~SUB_RETRY_SEND;
2588 /*
2589 * If the conn_stream attempt to subscribe, and the
2590 * mux isn't subscribed to the connection, then it
2591 * probably means the connection wasn't established
2592 * yet, so we have to subscribe.
2593 */
2594 h1c = h1s->h1c;
2595 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2596 h1c->conn->xprt->subscribe(h1c->conn,
2597 h1c->conn->xprt_ctx,
2598 SUB_RETRY_SEND,
2599 &h1c->wait_event);
2600 }
2601 if (event_type != 0)
2602 return -1;
2603 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002604}
2605
2606/* Called from the upper layer, to receive data */
2607static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2608{
2609 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002610 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002611 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002612 size_t ret = 0;
2613
Christopher Faulet6b81df72019-10-01 22:08:43 +02002614 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002615 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002616 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002617 else
2618 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002619
Christopher Faulete18777b2019-04-16 16:46:36 +02002620 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002621 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002622 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002623 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2624 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002625 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002626 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002627 if (h1s->flags & H1S_F_SPLICED_DATA) {
2628 h1s->flags &= ~H1S_F_SPLICED_DATA;
2629 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2630 }
2631 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002632 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002633 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002634 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002635 return ret;
2636}
2637
2638
2639/* Called from the upper layer, to send data */
2640static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2641{
2642 struct h1s *h1s = cs->ctx;
2643 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002644 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002645
2646 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002647 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002648 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002649
Christopher Faulet6b81df72019-10-01 22:08:43 +02002650 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){count});
2651
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002652 /* If we're not connected yet, or we're waiting for a handshake, stop
2653 * now, as we don't want to remove everything from the channel buffer
2654 * before we're sure we can send it.
2655 */
2656 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002657 (h1c->conn->flags & CO_FL_HANDSHAKE)) {
2658 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002659 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002660 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002661
Christopher Fauletc31872f2019-06-04 22:09:36 +02002662 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002663 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002664
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002665 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2666 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002667 else
2668 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002669 if (!ret)
2670 break;
2671 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002672 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002673 if (!h1_send(h1c))
2674 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002675 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002676
Christopher Faulet6b81df72019-10-01 22:08:43 +02002677 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002678 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002679}
2680
Willy Tarreaue5733232019-05-22 19:24:06 +02002681#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002682/* Send and get, using splicing */
2683static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2684{
2685 struct h1s *h1s = cs->ctx;
2686 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2687 int ret = 0;
2688
Christopher Faulet6b81df72019-10-01 22:08:43 +02002689 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){count});
2690
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002691 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002692 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002693 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2694 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2695 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002696 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002697 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002698 goto end;
2699 }
2700
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002701 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002702 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002703 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002704 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002705 }
2706
2707 h1s->flags &= ~H1S_F_BUF_FLUSH;
2708 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002709 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002710 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2711 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002712 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002713 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002714 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002715 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002716 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002717 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2718 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002719 }
2720
Christopher Faulet1be55f92018-10-02 15:59:23 +02002721 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002722 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002723 h1s->flags |= H1S_F_REOS;
Christopher Fauletf3158e92019-11-15 11:14:23 +01002724 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002725 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002726 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002727
2728 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002729 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002730}
2731
2732static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2733{
2734 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002735 int ret = 0;
2736
Christopher Faulet6b81df72019-10-01 22:08:43 +02002737 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){pipe->data});
2738
Christopher Faulet1be55f92018-10-02 15:59:23 +02002739 if (b_data(&h1s->h1c->obuf))
2740 goto end;
2741
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002742 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002743 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002744 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002745 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2746 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002747 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002748 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002749 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002750
2751 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002752 return ret;
2753}
2754#endif
2755
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002756static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2757{
2758 int ret = 0;
2759 switch (mux_ctl) {
2760 case MUX_STATUS:
2761 if (conn->flags & CO_FL_CONNECTED)
2762 ret |= MUX_STATUS_READY;
2763 return ret;
2764 default:
2765 return -1;
2766 }
2767}
2768
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002769/* for debugging with CLI's "show fd" command */
2770static void h1_show_fd(struct buffer *msg, struct connection *conn)
2771{
2772 struct h1c *h1c = conn->ctx;
2773 struct h1s *h1s = h1c->h1s;
2774
Christopher Fauletf376a312019-01-04 15:16:06 +01002775 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2776 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002777 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2778 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2779 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2780 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2781
2782 if (h1s) {
2783 char *method;
2784
2785 if (h1s->meth < HTTP_METH_OTHER)
2786 method = http_known_methods[h1s->meth].ptr;
2787 else
2788 method = "UNKNOWN";
2789 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2790 " .meth=%s status=%d",
2791 h1s, h1s->flags,
2792 h1m_state_str(h1s->req.state),
2793 h1m_state_str(h1s->res.state), method, h1s->status);
2794 if (h1s->cs)
2795 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2796 h1s->cs->flags, h1s->cs->data);
2797 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002798}
2799
2800
2801/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2802static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2803{
2804 struct h1_hdr_entry *entry;
2805
2806 /* Be sure there is a non-empty <to> */
2807 if (!strlen(to)) {
2808 memprintf(err, "expect <to>");
2809 return -1;
2810 }
2811
2812 /* Be sure only the case differs between <from> and <to> */
2813 if (strcasecmp(from, to)) {
2814 memprintf(err, "<from> and <to> must not differ execpt the case");
2815 return -1;
2816 }
2817
2818 /* Be sure <from> does not already existsin the tree */
2819 if (ebis_lookup(&hdrs_map.map, from)) {
2820 memprintf(err, "duplicate entry '%s'", from);
2821 return -1;
2822 }
2823
2824 /* Create the entry and insert it in the tree */
2825 entry = malloc(sizeof(*entry));
2826 if (!entry) {
2827 memprintf(err, "out of memory");
2828 return -1;
2829 }
2830
2831 entry->node.key = strdup(from);
2832 entry->name.ptr = strdup(to);
2833 entry->name.len = strlen(to);
2834 if (!entry->node.key || !entry->name.ptr) {
2835 free(entry->node.key);
2836 free(entry->name.ptr);
2837 free(entry);
2838 memprintf(err, "out of memory");
2839 return -1;
2840 }
2841 ebis_insert(&hdrs_map.map, &entry->node);
2842 return 0;
2843}
2844
2845static void h1_hdeaders_case_adjust_deinit()
2846{
2847 struct ebpt_node *node, *next;
2848 struct h1_hdr_entry *entry;
2849
2850 node = ebpt_first(&hdrs_map.map);
2851 while (node) {
2852 next = ebpt_next(node);
2853 ebpt_delete(node);
2854 entry = container_of(node, struct h1_hdr_entry, node);
2855 free(entry->node.key);
2856 free(entry->name.ptr);
2857 free(entry);
2858 node = next;
2859 }
2860 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002861}
2862
Christopher Faulet98fbe952019-07-22 16:18:24 +02002863static int cfg_h1_headers_case_adjust_postparser()
2864{
2865 FILE *file = NULL;
2866 char *c, *key_beg, *key_end, *value_beg, *value_end;
2867 char *err;
2868 int rc, line = 0, err_code = 0;
2869
2870 if (!hdrs_map.name)
2871 goto end;
2872
2873 file = fopen(hdrs_map.name, "r");
2874 if (!file) {
2875 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2876 hdrs_map.name);
2877 err_code |= ERR_ALERT | ERR_FATAL;
2878 goto end;
2879 }
2880
2881 /* now parse all lines. The file may contain only two header name per
2882 * line, separated by spaces. All heading and trailing spaces will be
2883 * ignored. Lines starting with a # are ignored.
2884 */
2885 while (fgets(trash.area, trash.size, file) != NULL) {
2886 line++;
2887 c = trash.area;
2888
2889 /* strip leading spaces and tabs */
2890 while (*c == ' ' || *c == '\t')
2891 c++;
2892
2893 /* ignore emptu lines, or lines beginning with a dash */
2894 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2895 continue;
2896
2897 /* look for the end of the key */
2898 key_beg = c;
2899 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2900 c++;
2901 key_end = c;
2902
2903 /* strip middle spaces and tabs */
2904 while (*c == ' ' || *c == '\t')
2905 c++;
2906
2907 /* look for the end of the value, it is the end of the line */
2908 value_beg = c;
2909 while (*c && *c != '\n' && *c != '\r')
2910 c++;
2911 value_end = c;
2912
2913 /* trim possibly trailing spaces and tabs */
2914 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2915 value_end--;
2916
2917 /* set final \0 and check entries */
2918 *key_end = '\0';
2919 *value_end = '\0';
2920
2921 err = NULL;
2922 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2923 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002924 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2925 hdrs_map.name, err, line);
2926 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002927 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002928 goto end;
2929 }
2930 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002931 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2932 hdrs_map.name, err, line);
2933 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002934 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002935 }
2936 }
2937
2938 end:
2939 if (file)
2940 fclose(file);
2941 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2942 return err_code;
2943}
2944
2945
2946/* config parser for global "h1-outgoing-header-case-adjust" */
2947static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2948 struct proxy *defpx, const char *file, int line,
2949 char **err)
2950{
2951 if (too_many_args(2, args, err, NULL))
2952 return -1;
2953 if (!*(args[1]) || !*(args[2])) {
2954 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2955 return -1;
2956 }
2957 return add_hdr_case_adjust(args[1], args[2], err);
2958}
2959
2960/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2961static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2962 struct proxy *defpx, const char *file, int line,
2963 char **err)
2964{
2965 if (too_many_args(1, args, err, NULL))
2966 return -1;
2967 if (!*(args[1])) {
2968 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2969 return -1;
2970 }
2971 free(hdrs_map.name);
2972 hdrs_map.name = strdup(args[1]);
2973 return 0;
2974}
2975
2976
2977/* config keyword parsers */
2978static struct cfg_kw_list cfg_kws = {{ }, {
2979 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2980 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2981 { 0, NULL, NULL },
2982 }
2983};
2984
2985INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2986REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2987
2988
Christopher Faulet51dbc942018-09-13 09:05:15 +02002989/****************************************/
2990/* MUX initialization and instanciation */
2991/****************************************/
2992
2993/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002994static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002995 .init = h1_init,
2996 .wake = h1_wake,
2997 .attach = h1_attach,
2998 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002999 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003000 .detach = h1_detach,
3001 .destroy = h1_destroy,
3002 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003003 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003004 .rcv_buf = h1_rcv_buf,
3005 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003006#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003007 .rcv_pipe = h1_rcv_pipe,
3008 .snd_pipe = h1_snd_pipe,
3009#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003010 .subscribe = h1_subscribe,
3011 .unsubscribe = h1_unsubscribe,
3012 .shutr = h1_shutr,
3013 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003014 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003015 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003016 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003017 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003018 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003019};
3020
3021
3022/* this mux registers default HTX proto */
3023static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003024{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003025
Willy Tarreau0108d902018-11-25 19:14:37 +01003026INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3027
Christopher Faulet51dbc942018-09-13 09:05:15 +02003028/*
3029 * Local variables:
3030 * c-indent-level: 8
3031 * c-basic-offset: 8
3032 * End:
3033 */