blob: fc4fde7bb1d89a291f5e4f8640bf735402f7e3e4 [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 */
Willy Tarreaub2551052020-06-09 09:07:15 +020012#include <import/ebistree.h>
13
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020016#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020017#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020018#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020019#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020021#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/istbuf.h>
23#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020024#include <haproxy/pipe-t.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020025#include <haproxy/proxy-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020026#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020029#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030
31/*
32 * H1 Connection flags (32 bits)
33 */
34#define H1C_F_NONE 0x00000000
35
36/* Flags indicating why writing output data are blocked */
37#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
38#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
39/* 0x00000004 - 0x00000008 unused */
40
41/* Flags indicating why reading input data are blocked. */
42#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
43#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Faulet7b109f22019-12-05 11:18:31 +010044#define H1C_F_IN_BUSY 0x00000040 /* mux is blocked on input waiting the other side */
Christopher Faulet539e0292018-11-19 10:40:09 +010045/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet51dbc942018-09-13 09:05:15 +020048#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
49#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet7b109f22019-12-05 11:18:31 +010050#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +010051#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
52 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020053
Christopher Fauletf2824e62018-10-01 12:12:37 +020054#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 +020055#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020056
Christopher Faulet46230362019-10-17 16:04:20 +020057#define H1C_F_CO_MSG_MORE 0x00040000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
58#define H1C_F_CO_STREAMER 0x00080000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
59
Christopher Faulet51dbc942018-09-13 09:05:15 +020060/*
61 * H1 Stream flags (32 bits)
62 */
Christopher Faulet129817b2018-09-20 16:14:40 +020063#define H1S_F_NONE 0x00000000
64#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020065#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
66#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020067#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020068#define H1S_F_WANT_KAL 0x00000010
69#define H1S_F_WANT_TUN 0x00000020
70#define H1S_F_WANT_CLO 0x00000040
71#define H1S_F_WANT_MSK 0x00000070
72#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010073#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010074#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet76014fd2019-12-10 11:47:22 +010075#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
76/* 0x00000800 .. 0x00001000 unused */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020077#define H1S_F_HAVE_SRV_NAME 0x00002000 /* Set during output process if the server name header was added to the request */
Christopher Fauletada34b62019-05-24 16:36:43 +020078#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020079
80/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020081struct h1c {
82 struct connection *conn;
83 struct proxy *px;
84 uint32_t flags; /* Connection flags: H1C_F_* */
85
86 struct buffer ibuf; /* Input buffer to store data before parsing */
87 struct buffer obuf; /* Output buffer to store data after reformatting */
88
89 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
90 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
91
92 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010093 struct task *task; /* timeout management task */
94 int timeout; /* idle timeout duration in ticks */
95 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096};
97
98/* H1 stream descriptor */
99struct h1s {
100 struct h1c *h1c;
101 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100102 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100104 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200105
Olivier Houchardf502aca2018-12-14 19:42:40 +0100106 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200107 struct h1m req;
108 struct h1m res;
109
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500110 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200111 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200112};
113
Christopher Faulet98fbe952019-07-22 16:18:24 +0200114/* Map of headers used to convert outgoing headers */
115struct h1_hdrs_map {
116 char *name;
117 struct eb_root map;
118};
119
120/* An entry in a headers map */
121struct h1_hdr_entry {
122 struct ist name;
123 struct ebpt_node node;
124};
125
126/* Declare the headers map */
127static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
128
129
Christopher Faulet6b81df72019-10-01 22:08:43 +0200130/* trace source and events */
131static void h1_trace(enum trace_level level, uint64_t mask,
132 const struct trace_source *src,
133 const struct ist where, const struct ist func,
134 const void *a1, const void *a2, const void *a3, const void *a4);
135
136/* The event representation is split like this :
137 * h1c - internal H1 connection
138 * h1s - internal H1 stream
139 * strm - application layer
140 * rx - data receipt
141 * tx - data transmission
142 *
143 */
144static const struct trace_event h1_trace_events[] = {
145#define H1_EV_H1C_NEW (1ULL << 0)
146 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
147#define H1_EV_H1C_RECV (1ULL << 1)
148 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
149#define H1_EV_H1C_SEND (1ULL << 2)
150 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
151#define H1_EV_H1C_BLK (1ULL << 3)
152 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
153#define H1_EV_H1C_WAKE (1ULL << 4)
154 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
155#define H1_EV_H1C_END (1ULL << 5)
156 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
157#define H1_EV_H1C_ERR (1ULL << 6)
158 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
159
160#define H1_EV_RX_DATA (1ULL << 7)
161 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
162#define H1_EV_RX_EOI (1ULL << 8)
163 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
164#define H1_EV_RX_HDRS (1ULL << 9)
165 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
166#define H1_EV_RX_BODY (1ULL << 10)
167 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
168#define H1_EV_RX_TLRS (1ULL << 11)
169 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
170
171#define H1_EV_TX_DATA (1ULL << 12)
172 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
173#define H1_EV_TX_EOI (1ULL << 13)
174 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
175#define H1_EV_TX_HDRS (1ULL << 14)
176 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
177#define H1_EV_TX_BODY (1ULL << 15)
178 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
179#define H1_EV_TX_TLRS (1ULL << 16)
180 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
181
182#define H1_EV_H1S_NEW (1ULL << 17)
183 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
184#define H1_EV_H1S_BLK (1ULL << 18)
185 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
186#define H1_EV_H1S_END (1ULL << 19)
187 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
188#define H1_EV_H1S_ERR (1ULL << 20)
189 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
190
191#define H1_EV_STRM_NEW (1ULL << 21)
192 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
193#define H1_EV_STRM_RECV (1ULL << 22)
194 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
195#define H1_EV_STRM_SEND (1ULL << 23)
196 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
197#define H1_EV_STRM_WAKE (1ULL << 24)
198 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
199#define H1_EV_STRM_SHUT (1ULL << 25)
200 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
201#define H1_EV_STRM_END (1ULL << 26)
202 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
203#define H1_EV_STRM_ERR (1ULL << 27)
204 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
205
206 { }
207};
208
209static const struct name_desc h1_trace_lockon_args[4] = {
210 /* arg1 */ { /* already used by the connection */ },
211 /* arg2 */ { .name="h1s", .desc="H1 stream" },
212 /* arg3 */ { },
213 /* arg4 */ { }
214};
215
216static const struct name_desc h1_trace_decoding[] = {
217#define H1_VERB_CLEAN 1
218 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
219#define H1_VERB_MINIMAL 2
220 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
221#define H1_VERB_SIMPLE 3
222 { .name="simple", .desc="add request/response status line or htx info when available" },
223#define H1_VERB_ADVANCED 4
224 { .name="advanced", .desc="add header fields or frame decoding when available" },
225#define H1_VERB_COMPLETE 5
226 { .name="complete", .desc="add full data dump when available" },
227 { /* end */ }
228};
229
230static struct trace_source trace_h1 = {
231 .name = IST("h1"),
232 .desc = "HTTP/1 multiplexer",
233 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
234 .default_cb = h1_trace,
235 .known_events = h1_trace_events,
236 .lockon_args = h1_trace_lockon_args,
237 .decoding = h1_trace_decoding,
238 .report_events = ~0, // report everything by default
239};
240
241#define TRACE_SOURCE &trace_h1
242INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
243
Christopher Faulet51dbc942018-09-13 09:05:15 +0200244/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100245DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
246DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200247
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248static int h1_recv(struct h1c *h1c);
249static int h1_send(struct h1c *h1c);
250static int h1_process(struct h1c *h1c);
251static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100252static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100253static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200254static void h1_wake_stream_for_recv(struct h1s *h1s);
255static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256
Christopher Faulet6b81df72019-10-01 22:08:43 +0200257/* the H1 traces always expect that arg1, if non-null, is of type connection
258 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
259 * that arg3, if non-null, is a htx for rx/tx headers.
260 */
261static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
262 const struct ist where, const struct ist func,
263 const void *a1, const void *a2, const void *a3, const void *a4)
264{
265 const struct connection *conn = a1;
266 const struct h1c *h1c = conn ? conn->ctx : NULL;
267 const struct h1s *h1s = a2;
268 const struct htx *htx = a3;
269 const size_t *val = a4;
270
271 if (!h1c)
272 h1c = (h1s ? h1s->h1c : NULL);
273
274 if (!h1c || src->verbosity < H1_VERB_CLEAN)
275 return;
276
277 /* Display frontend/backend info by default */
278 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
279
280 /* Display request and response states if h1s is defined */
281 if (h1s)
282 chunk_appendf(&trace_buf, " [%s, %s]",
283 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
284
285 if (src->verbosity == H1_VERB_CLEAN)
286 return;
287
288 /* Display the value to the 4th argument (level > STATE) */
289 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100290 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200291
292 /* Display status-line if possible (verbosity > MINIMAL) */
293 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
294 const struct htx_blk *blk = htx_get_head_blk(htx);
295 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
296 enum htx_blk_type type = htx_get_blk_type(blk);
297
298 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
299 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
300 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
301 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
302 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
303 }
304
305 /* Display h1c info and, if defined, h1s info (pointer + flags) */
306 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
307 if (h1s)
308 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
309
310 if (src->verbosity == H1_VERB_MINIMAL)
311 return;
312
313 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
314 if (src->level > TRACE_LEVEL_USER) {
315 if (src->verbosity == H1_VERB_COMPLETE ||
316 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
317 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
318 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
319 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
320 if (src->verbosity == H1_VERB_COMPLETE ||
321 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
322 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
323 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
324 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
325 }
326
327 /* Display htx info if defined (level > USER) */
328 if (src->level > TRACE_LEVEL_USER && htx) {
329 int full = 0;
330
331 /* Full htx info (level > STATE && verbosity > SIMPLE) */
332 if (src->level > TRACE_LEVEL_STATE) {
333 if (src->verbosity == H1_VERB_COMPLETE)
334 full = 1;
335 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
336 full = 1;
337 }
338
339 chunk_memcat(&trace_buf, "\n\t", 2);
340 htx_dump(&trace_buf, htx, full);
341 }
342}
343
344
Christopher Faulet51dbc942018-09-13 09:05:15 +0200345/*****************************************************/
346/* functions below are for dynamic buffer management */
347/*****************************************************/
348/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100349 * Indicates whether or not we may receive data. The rules are the following :
350 * - if an error or a shutdown for reads was detected on the connection we
351 must not attempt to receive
352 * - if the input buffer failed to be allocated or is full , we must not try
353 * to receive
354 * - if he input processing is busy waiting for the output side, we may
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500355 * attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200356 * - otherwise must may not attempt to receive
357 */
358static inline int h1_recv_allowed(const struct h1c *h1c)
359{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100360 if (h1c->flags & H1C_F_CS_ERROR) {
361 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 +0200362 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200363 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200364
Willy Tarreau2febb842020-07-31 09:15:43 +0200365 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
366 TRACE_DEVEL("recv not allowed because of (error|read0|waitl4|waitl6) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletcf56b992018-12-11 16:12:31 +0100367 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200368 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100369
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200370 if (conn_is_back(h1c->conn) && h1c->h1s && h1c->h1s->req.state == H1_MSG_RQBEFORE) {
371 TRACE_DEVEL("recv not allowed because back and request not sent yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
372 return 0;
373 }
374
Christopher Fauletcb55f482018-12-10 11:56:47 +0100375 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376 return 1;
377
Christopher Faulet6b81df72019-10-01 22:08:43 +0200378 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 +0200379 return 0;
380}
381
382/*
383 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
384 * flags are used to figure what buffer was requested. It returns 1 if the
385 * allocation succeeds, in which case the connection is woken up, or 0 if it's
386 * impossible to wake up and we prefer to be woken up later.
387 */
388static int h1_buf_available(void *target)
389{
390 struct h1c *h1c = target;
391
392 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200393 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 +0200394 h1c->flags &= ~H1C_F_IN_ALLOC;
395 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200396 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200397 return 1;
398 }
399
400 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200401 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 +0200402 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200403 if (h1c->h1s)
404 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200405 return 1;
406 }
407
Christopher Faulet51dbc942018-09-13 09:05:15 +0200408 return 0;
409}
410
411/*
412 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
413 */
414static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
415{
416 struct buffer *buf = NULL;
417
Willy Tarreau21046592020-02-26 10:39:36 +0100418 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200419 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
420 h1c->buf_wait.target = h1c;
421 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200422 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200423 }
424 return buf;
425}
426
427/*
428 * Release a buffer, if any, and try to wake up entities waiting in the buffer
429 * wait queue.
430 */
431static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
432{
433 if (bptr->size) {
434 b_free(bptr);
435 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
436 }
437}
438
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100439/* returns the number of streams in use on a connection to figure if it's idle
440 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
441 * not. This flag is only set when no H1S is attached and when the previous
442 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100443 */
444static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200445{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200447
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100448 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449}
450
Willy Tarreau00f18a32019-01-26 12:19:01 +0100451/* returns the number of streams still available on a connection */
452static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100453{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100454 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100455}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456
Christopher Faulet7a145d62020-08-05 11:31:16 +0200457/* Refresh the h1c task timeout if necessary */
458static void h1_refresh_timeout(struct h1c *h1c)
459{
460 if (h1c->task) {
461 h1c->task->expire = TICK_ETERNITY;
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200462 if (h1c->flags & H1C_F_CS_SHUTDOWN) {
463 /* half-closed connections switch to clientfin/serverfin
464 * timeouts so that we don't hang too long on clients
465 * that have gone away (especially in tunnel mode).
466 */
467 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
468 task_queue(h1c->task);
469 TRACE_DEVEL("refreshing connection's timeout (half-closed)", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet268c92e2020-12-01 11:42:53 +0100470 } else if (b_data(&h1c->obuf)) {
471 /* any connection with pending data, need a timeout (server or client).
Christopher Faulet7a145d62020-08-05 11:31:16 +0200472 */
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200473 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet7a145d62020-08-05 11:31:16 +0200474 ? h1c->shut_timeout
475 : h1c->timeout));
476 task_queue(h1c->task);
477 TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet268c92e2020-12-01 11:42:53 +0100478 } else if ((h1c->flags & (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ)) && !conn_is_back(h1c->conn)) {
479 /* front connections waiting for a stream need a timeout. client timeout by
480 * default but http-keep-alive if defined
481 */
482 int timeout = h1c->timeout;
483
484 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
485 timeout = tick_first(timeout, h1c->px->timeout.httpka);
486
487 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
488 ? h1c->shut_timeout
489 : timeout));
490 task_queue(h1c->task);
491 TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200492 }
493 }
494}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200495/*****************************************************************/
496/* functions below are dedicated to the mux setup and management */
497/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200498
499/* returns non-zero if there are input data pending for stream h1s. */
500static inline size_t h1s_data_pending(const struct h1s *h1s)
501{
502 const struct h1m *h1m;
503
504 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
505 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100506 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200507
508 return b_data(&h1s->h1c->ibuf);
509}
510
Christopher Faulet47365272018-10-31 17:40:50 +0100511static struct conn_stream *h1s_new_cs(struct h1s *h1s)
512{
513 struct conn_stream *cs;
514
Christopher Faulet6b81df72019-10-01 22:08:43 +0200515 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200516 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200517 if (!cs) {
518 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 +0100519 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200520 }
Christopher Faulet47365272018-10-31 17:40:50 +0100521 h1s->cs = cs;
522 cs->ctx = h1s;
523
524 if (h1s->flags & H1S_F_NOT_FIRST)
525 cs->flags |= CS_FL_NOT_FIRST;
526
Christopher Faulet27182292020-07-03 15:08:49 +0200527 if (global.tune.options & GTUNE_USE_SPLICE) {
528 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100529 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200530 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100531
Christopher Faulet6b81df72019-10-01 22:08:43 +0200532 if (stream_create_from_cs(cs) < 0) {
533 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 +0100534 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200535 }
536
537 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100538 return cs;
539
540 err:
541 cs_free(cs);
542 h1s->cs = NULL;
543 return NULL;
544}
545
Olivier Houchardf502aca2018-12-14 19:42:40 +0100546static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200547{
548 struct h1s *h1s;
549
Christopher Faulet6b81df72019-10-01 22:08:43 +0200550 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
551
Christopher Faulet51dbc942018-09-13 09:05:15 +0200552 h1s = pool_alloc(pool_head_h1s);
553 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100554 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200555
556 h1s->h1c = h1c;
557 h1c->h1s = h1s;
558
Olivier Houchardf502aca2018-12-14 19:42:40 +0100559 h1s->sess = sess;
560
Christopher Faulet51dbc942018-09-13 09:05:15 +0200561 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100562 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200563
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100564 h1s->subs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200565
566 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100567 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200568
Christopher Faulet129817b2018-09-20 16:14:40 +0200569 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100570 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200571
572 h1s->status = 0;
573 h1s->meth = HTTP_METH_OTHER;
574
Christopher Faulet47365272018-10-31 17:40:50 +0100575 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
576 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100577 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100578
Christopher Faulet129817b2018-09-20 16:14:40 +0200579 if (!conn_is_back(h1c->conn)) {
580 if (h1c->px->options2 & PR_O2_REQBUG_OK)
581 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200582
583 /* For frontend connections we should always have a session */
584 if (!sess)
Christopher Faulete9da9752020-09-30 15:00:13 +0200585 h1s->sess = sess = h1c->conn->owner;
Christopher Faulet129817b2018-09-20 16:14:40 +0200586 }
587 else {
588 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
589 h1s->res.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200590 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100591
Christopher Faulete9b70722019-04-08 10:46:02 +0200592 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
593 * create a new one.
594 */
595 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596 cs->ctx = h1s;
597 h1s->cs = cs;
598 }
Christopher Faulet47365272018-10-31 17:40:50 +0100599 else {
600 cs = h1s_new_cs(h1s);
601 if (!cs)
602 goto fail;
603 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200604 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200605 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100606
607 fail:
608 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200609 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 +0100610 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200611}
612
613static void h1s_destroy(struct h1s *h1s)
614{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200615 if (h1s) {
616 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200617
Christopher Faulet6b81df72019-10-01 22:08:43 +0200618 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200619 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200620
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100621 if (h1s->subs)
622 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200623
Christopher Fauletcb55f482018-12-10 11:56:47 +0100624 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200625 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100626 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200627 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
628 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100629
630 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
631 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100632 (h1s->flags & (H1S_F_WANT_KAL|H1S_F_PARSING_DONE)) == (H1S_F_WANT_KAL|H1S_F_PARSING_DONE) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100633 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
634 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
635 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
636 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200637 pool_free(pool_head_h1s, h1s);
638 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200639}
640
641/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200642 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500643 * to the existing conn_stream (for outgoing connections or for incoming ones
644 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200645 * establishment). <input> is always used as Input buffer and may contain
646 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
647 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200648 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200649static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
650 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200651{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200652 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100653 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200654 void *conn_ctx = conn->ctx;
655
656 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200657
658 h1c = pool_alloc(pool_head_h1c);
659 if (!h1c)
660 goto fail_h1c;
661 h1c->conn = conn;
662 h1c->px = proxy;
663
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100664 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200665 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200666 h1c->obuf = BUF_NULL;
667 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200668 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200669
Willy Tarreau21046592020-02-26 10:39:36 +0100670 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200671 h1c->wait_event.tasklet = tasklet_new();
672 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200673 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200674 h1c->wait_event.tasklet->process = h1_io_cb;
675 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100676 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200677
Christopher Faulete9b70722019-04-08 10:46:02 +0200678 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100679 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
680 if (tick_isset(proxy->timeout.serverfin))
681 h1c->shut_timeout = proxy->timeout.serverfin;
682 } else {
683 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
684 if (tick_isset(proxy->timeout.clientfin))
685 h1c->shut_timeout = proxy->timeout.clientfin;
686 }
687 if (tick_isset(h1c->timeout)) {
688 t = task_new(tid_bit);
689 if (!t)
690 goto fail;
691
692 h1c->task = t;
693 t->process = h1_timeout_task;
694 t->context = h1c;
695 t->expire = tick_add(now_ms, h1c->timeout);
696 }
697
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100698 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200699
Christopher Faulet6b81df72019-10-01 22:08:43 +0200700 /* Always Create a new H1S */
701 if (!h1s_create(h1c, conn_ctx, sess))
702 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100703
704 if (t)
705 task_queue(t);
706
Christopher Faulet51dbc942018-09-13 09:05:15 +0200707 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau2c1f37d2020-03-04 17:50:02 +0100708 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200709
710 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200711 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200712 return 0;
713
714 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200715 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200716 if (h1c->wait_event.tasklet)
717 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200718 pool_free(pool_head_h1c, h1c);
719 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200720 conn->ctx = conn_ctx; // restore saved context
721 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200722 return -1;
723}
724
Christopher Faulet73c12072019-04-08 11:23:22 +0200725/* release function. This one should be called to free all resources allocated
726 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200727 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200728static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200729{
Christopher Faulet61840e72019-04-15 09:33:32 +0200730 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200731
Christopher Faulet6b81df72019-10-01 22:08:43 +0200732 TRACE_POINT(H1_EV_H1C_END);
733
Christopher Faulet51dbc942018-09-13 09:05:15 +0200734 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200735 /* The connection must be aattached to this mux to be released */
736 if (h1c->conn && h1c->conn->ctx == h1c)
737 conn = h1c->conn;
738
Christopher Faulet6b81df72019-10-01 22:08:43 +0200739 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
740
Christopher Faulet61840e72019-04-15 09:33:32 +0200741 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200742 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200743 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200744 /* Make sure we're no longer subscribed to anything */
745 if (h1c->wait_event.events)
746 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
747 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200748 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200749 /* connection successfully upgraded to H2, this
750 * mux was already released */
751 return;
752 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200753 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200754 sess_log(conn->owner); /* Log if the upgrade failed */
755 }
756
Olivier Houchard45c44372019-06-11 14:06:23 +0200757
Willy Tarreau21046592020-02-26 10:39:36 +0100758 if (MT_LIST_ADDED(&h1c->buf_wait.list))
759 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200760
761 h1_release_buf(h1c, &h1c->ibuf);
762 h1_release_buf(h1c, &h1c->obuf);
763
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100764 if (h1c->task) {
765 h1c->task->context = NULL;
766 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
767 h1c->task = NULL;
768 }
769
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200770 if (h1c->wait_event.tasklet)
771 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200772
Christopher Fauletf2824e62018-10-01 12:12:37 +0200773 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200774 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100775 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200776 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200777 pool_free(pool_head_h1c, h1c);
778 }
779
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200780 if (conn) {
781 conn->mux = NULL;
782 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200783 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200784
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200785 conn_stop_tracking(conn);
786 conn_full_close(conn);
787 if (conn->destroy_cb)
788 conn->destroy_cb(conn);
789 conn_free(conn);
790 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200791}
792
793/******************************************************/
794/* functions below are for the H1 protocol processing */
795/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200796/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
797 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200798 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100799static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200800{
Christopher Faulet570d1612018-11-26 11:13:57 +0100801 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200802
Christopher Faulet570d1612018-11-26 11:13:57 +0100803 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200804 (*(p + 5) > '1' ||
805 (*(p + 5) == '1' && *(p + 7) >= '1')))
806 h1m->flags |= H1_MF_VER_11;
807}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200808
Christopher Faulet9768c262018-10-22 09:34:31 +0200809/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
810 * greater or equal to 1.1
811 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100812static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200813{
Christopher Faulet570d1612018-11-26 11:13:57 +0100814 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200815
Christopher Faulet570d1612018-11-26 11:13:57 +0100816 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200817 (*(p + 5) > '1' ||
818 (*(p + 5) == '1' && *(p + 7) >= '1')))
819 h1m->flags |= H1_MF_VER_11;
820}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200821
Christopher Fauletf2824e62018-10-01 12:12:37 +0200822/* Deduce the connection mode of the client connection, depending on the
823 * configuration and the H1 message flags. This function is called twice, the
824 * first time when the request is parsed and the second time when the response
825 * is parsed.
826 */
827static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
828{
829 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200830
831 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100832 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200833 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100834 h1s->status == 101) {
835 /* Either we've established an explicit tunnel, or we're
836 * switching the protocol. In both cases, we're very unlikely to
837 * understand the next protocols. We have to switch to tunnel
838 * mode, so that we transfer the request and responses then let
839 * this protocol pass unmodified. When we later implement
840 * specific parsers for such protocols, we'll want to check the
841 * Upgrade header which contains information about that protocol
842 * for responses with status 101 (eg: see RFC2817 about TLS).
843 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200844 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200845 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 +0100846 }
847 else if (h1s->flags & H1S_F_WANT_KAL) {
848 /* By default the client is in KAL mode. CLOSE mode mean
849 * it is imposed by the client itself. So only change
850 * KAL mode here. */
851 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
852 /* no length known or explicit close => close */
853 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200854 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 +0100855 }
856 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
857 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500858 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100859 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200860 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 +0100861 }
862 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200863 }
864 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100865 /* Input direction: first pass */
866 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
867 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200868 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200869 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 +0100870 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200871 }
872
873 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200874 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200875 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200876 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);
877 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878}
879
880/* Deduce the connection mode of the client connection, depending on the
881 * configuration and the H1 message flags. This function is called twice, the
882 * first time when the request is parsed and the second time when the response
883 * is parsed.
884 */
885static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
886{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100887 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100888 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100889 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200890
Christopher Fauletf2824e62018-10-01 12:12:37 +0200891 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100892 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200893 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100894 h1s->status == 101) {
895 /* Either we've established an explicit tunnel, or we're
896 * switching the protocol. In both cases, we're very unlikely to
897 * understand the next protocols. We have to switch to tunnel
898 * mode, so that we transfer the request and responses then let
899 * this protocol pass unmodified. When we later implement
900 * specific parsers for such protocols, we'll want to check the
901 * Upgrade header which contains information about that protocol
902 * for responses with status 101 (eg: see RFC2817 about TLS).
903 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200904 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200905 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 +0100906 }
907 else if (h1s->flags & H1S_F_WANT_KAL) {
908 /* By default the server is in KAL mode. CLOSE mode mean
909 * it is imposed by haproxy itself. So only change KAL
910 * mode here. */
911 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
912 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
913 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
914 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200915 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 +0100916 }
917 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200918 }
Christopher Faulet70033782018-12-05 13:50:11 +0100919 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100920 /* Output direction: first pass */
921 if (h1m->flags & H1_MF_CONN_CLO) {
922 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100923 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200924 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 +0100925 }
926 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
927 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
928 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
929 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
930 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
931 /* no explicit keep-alive option httpclose/server-close => close */
932 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200933 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 +0100934 }
Christopher Faulet70033782018-12-05 13:50:11 +0100935 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200936
937 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200938 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200939 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200940 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);
941 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200942}
943
Christopher Fauletb992af02019-03-28 15:42:24 +0100944static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200945{
946 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200947
948 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
949 * token is found
950 */
951 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200952 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200953
954 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200955 if (!(h1m->flags & H1_MF_VER_11)) {
956 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 +0100957 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200958 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200959 }
960 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200961 if (h1m->flags & H1_MF_VER_11) {
962 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100963 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200964 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200965 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200966}
967
Christopher Fauletb992af02019-03-28 15:42:24 +0100968static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200969{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200970 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
971 * token is found
972 */
973 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200974 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200975
976 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100977 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +0200978 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
979 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 +0100980 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200981 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200982 }
983 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200984 if (h1m->flags & H1_MF_VER_11) {
985 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100986 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200987 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200988 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200989}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200990
Christopher Fauletb992af02019-03-28 15:42:24 +0100991static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200992{
Christopher Fauletb992af02019-03-28 15:42:24 +0100993 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200994 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100995 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200996 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200997}
998
Christopher Fauletb992af02019-03-28 15:42:24 +0100999static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1000{
1001 if (!conn_is_back(h1s->h1c->conn))
1002 h1_set_cli_conn_mode(h1s, h1m);
1003 else
1004 h1_set_srv_conn_mode(h1s, h1m);
1005
1006 if (!(h1m->flags & H1_MF_RESP))
1007 h1_update_req_conn_value(h1s, h1m, conn_val);
1008 else
1009 h1_update_res_conn_value(h1s, h1m, conn_val);
1010}
Christopher Faulete44769b2018-11-29 23:01:45 +01001011
Christopher Faulet98fbe952019-07-22 16:18:24 +02001012/* Try to adjust the case of the message header name using the global map
1013 * <hdrs_map>.
1014 */
1015static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1016{
1017 struct ebpt_node *node;
1018 struct h1_hdr_entry *entry;
1019
1020 /* No entry in the map, do nothing */
1021 if (eb_is_empty(&hdrs_map.map))
1022 return;
1023
1024 /* No conversion fo the request headers */
1025 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1026 return;
1027
1028 /* No conversion fo the response headers */
1029 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1030 return;
1031
1032 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1033 if (!node)
1034 return;
1035 entry = container_of(node, struct h1_hdr_entry, node);
1036 name->ptr = entry->name.ptr;
1037 name->len = entry->name.len;
1038}
1039
Christopher Faulete44769b2018-11-29 23:01:45 +01001040/* Append the description of what is present in error snapshot <es> into <out>.
1041 * The description must be small enough to always fit in a buffer. The output
1042 * buffer may be the trash so the trash must not be used inside this function.
1043 */
1044static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1045{
1046 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001047 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1048 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1049 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1050 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1051 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1052 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001053}
1054/*
1055 * Capture a bad request or response and archive it in the proxy's structure.
1056 * By default it tries to report the error position as h1m->err_pos. However if
1057 * this one is not set, it will then report h1m->next, which is the last known
1058 * parsing point. The function is able to deal with wrapping buffers. It always
1059 * displays buffers as a contiguous area starting at buf->p. The direction is
1060 * determined thanks to the h1m's flags.
1061 */
1062static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1063 struct h1m *h1m, struct buffer *buf)
1064{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001065 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001066 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001067 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001068 union error_snapshot_ctx ctx;
1069
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001070 if (h1s->cs && h1s->cs->data) {
1071 if (sess == NULL)
1072 sess = si_strm(h1s->cs->data)->sess;
1073 if (!(h1m->flags & H1_MF_RESP))
1074 other_end = si_strm(h1s->cs->data)->be;
1075 else
1076 other_end = sess->fe;
1077 } else
1078 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001079
1080 /* http-specific part now */
1081 ctx.h1.state = h1m->state;
1082 ctx.h1.c_flags = h1c->flags;
1083 ctx.h1.s_flags = h1s->flags;
1084 ctx.h1.m_flags = h1m->flags;
1085 ctx.h1.m_clen = h1m->curr_len;
1086 ctx.h1.m_blen = h1m->body_len;
1087
1088 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1089 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001090 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1091 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001092}
1093
Christopher Fauletadb22202018-12-12 10:32:09 +01001094/* Emit the chunksize followed by a CRLF in front of data of the buffer
1095 * <buf>. It goes backwards and starts with the byte before the buffer's
1096 * head. The caller is responsible for ensuring there is enough room left before
1097 * the buffer's head for the string.
1098 */
1099static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1100{
1101 char *beg, *end;
1102
1103 beg = end = b_head(buf);
1104 *--beg = '\n';
1105 *--beg = '\r';
1106 do {
1107 *--beg = hextab[chksz & 0xF];
1108 } while (chksz >>= 4);
1109 buf->head -= (end - beg);
1110 b_add(buf, end - beg);
1111}
1112
1113/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1114 * ensuring there is enough room left in the buffer for the string. */
1115static void h1_emit_chunk_crlf(struct buffer *buf)
1116{
1117 *(b_peek(buf, b_data(buf))) = '\r';
1118 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1119 b_add(buf, 2);
1120}
1121
Christopher Faulet129817b2018-09-20 16:14:40 +02001122/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001123 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001124 * CONNECT requests. On the client side, if the response is not finished, the
1125 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001126 */
1127static void h1_set_req_tunnel_mode(struct h1s *h1s)
1128{
1129 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1130 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001131 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1132
Christopher Faulet76014fd2019-12-10 11:47:22 +01001133 if (!conn_is_back(h1s->h1c->conn)) {
1134 h1s->flags &= ~H1S_F_PARSING_DONE;
1135 if (h1s->res.state < H1_MSG_DONE) {
1136 h1s->h1c->flags |= H1C_F_IN_BUSY;
1137 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1138 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001139 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001140 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1141 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1142 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1143 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1144 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001145}
1146
1147/*
1148 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001149 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001150 * last case, this function takes care to switch the request to tunnel mode if
1151 * possible. On the server side, if the request is not finished, the mux is mark
1152 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001153 */
1154static void h1_set_res_tunnel_mode(struct h1s *h1s)
1155{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001156
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001157 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1158 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001159 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1160
Christopher Faulet76014fd2019-12-10 11:47:22 +01001161 if (conn_is_back(h1s->h1c->conn)) {
1162 h1s->flags &= ~H1S_F_PARSING_DONE;
1163 /* On protocol switching, switch the request to tunnel mode if it is in
1164 * DONE state. Otherwise we will wait the end of the request to switch
1165 * it in tunnel mode.
1166 */
1167 if (h1s->req.state < H1_MSG_DONE) {
1168 h1s->h1c->flags |= H1C_F_IN_BUSY;
1169 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1170 }
1171 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1172 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1173 h1s->req.state = H1_MSG_TUNNEL;
1174 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1175 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001176 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001177 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1178 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1179 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1180 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 +01001181 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001182}
1183
1184/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001185 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001186 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001187 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001188 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001189static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001190 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001191{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001192 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001193 int ret = 0;
1194
Willy Tarreau022e5e52020-09-10 09:33:15 +02001195 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001196
Christopher Faulet89aed322020-06-02 17:33:56 +02001197 if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
1198 !(h1s->flags & H1S_F_NOT_FIRST) && /* It is the first transaction */
1199 !(h1m->flags & H1_MF_RESP)) { /* It is a request */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001200 /* Try to match H2 preface before parsing the request headers. */
1201 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001202 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001203 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001204 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001205 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001206 else {
1207 if (h1s->meth == HTTP_METH_CONNECT)
1208 h1m->flags |= H1_MF_METH_CONNECT;
1209 if (h1s->meth == HTTP_METH_HEAD)
1210 h1m->flags |= H1_MF_METH_HEAD;
1211 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001212
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001213 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1214 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001215 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 +02001216 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001217 if (!(h1m->flags & H1_MF_RESP)) {
1218 h1s->flags |= H1S_F_REQ_ERROR;
1219 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1220 }
1221 else {
1222 h1s->flags |= H1S_F_RES_ERROR;
1223 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1224 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001225 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001226 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 +02001227 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1228 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001229 goto end;
1230 }
1231
Christopher Faulet486498c2019-10-11 14:22:00 +02001232 if (h1m->err_pos >= 0) {
1233 /* Maybe we found an error during the parsing while we were
1234 * configured not to block on that, so we have to capture it
1235 * now.
1236 */
1237 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1238 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1239 }
1240
Christopher Faulet129817b2018-09-20 16:14:40 +02001241 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001242 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001243 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001244 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001245 }
1246 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001247 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001248 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001249 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001250 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001251 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001252 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001253
Christopher Faulet129817b2018-09-20 16:14:40 +02001254 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001255 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001256 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001257
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001258 h2c_upgrade:
1259 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001260 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001261 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001262 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1263 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001264}
1265
1266/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001267 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001268 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1269 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001270 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001271static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001272 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001273 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001274{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001275 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001276
Willy Tarreau022e5e52020-09-10 09:33:15 +02001277 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001278 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001279 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001280 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 +01001281 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001282 if (!(h1m->flags & H1_MF_RESP)) {
1283 h1s->flags |= H1S_F_REQ_ERROR;
1284 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1285 }
1286 else {
1287 h1s->flags |= H1S_F_RES_ERROR;
1288 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1289 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001290 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001291 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 +02001292 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001293 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001294 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 }
1296
Christopher Faulet2eaf3092020-07-03 14:51:15 +02001297 if (h1s->cs && !(h1m->flags & H1_MF_CHNK) &&
Christopher Faulet27182292020-07-03 15:08:49 +02001298 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1299 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001300 h1s->cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02001301 }
1302 else if (h1s->cs) {
1303 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001304 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02001305 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001306
Christopher Faulet02a02532019-11-15 09:36:28 +01001307 *ofs += ret;
1308
1309 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001310 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001311 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001312}
1313
1314/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001315 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1316 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1317 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1318 * responsible to update the parser state <h1m>.
1319 */
1320static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1321 struct buffer *buf, size_t *ofs, size_t max)
1322{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001323 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001324
Willy Tarreau022e5e52020-09-10 09:33:15 +02001325 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001326 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001327 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001328 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 +01001329 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001330 if (!(h1m->flags & H1_MF_RESP)) {
1331 h1s->flags |= H1S_F_REQ_ERROR;
1332 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1333 }
1334 else {
1335 h1s->flags |= H1S_F_RES_ERROR;
1336 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1337 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001338 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001339 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 +02001340 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1341 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001342 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001343 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001344
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001345 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001346
1347 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001348 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001349 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001350}
1351
1352/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001353 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
1354 * proceed. This functions is responsible to update the parser state <h1m>. It
1355 * also add the flag CS_FL_EOI on the CS.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001356 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001357static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1358 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001359{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001360 int ret;
1361
Willy Tarreau022e5e52020-09-10 09:33:15 +02001362 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet76014fd2019-12-10 11:47:22 +01001363 ret = h1_parse_msg_eom(h1m, htx, max);
1364 if (!ret) {
1365 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1366 if (htx->flags & HTX_FL_PARSING_ERROR) {
1367 if (!(h1m->flags & H1_MF_RESP)) {
1368 h1s->flags |= H1S_F_REQ_ERROR;
1369 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1370 }
1371 else {
1372 h1s->flags |= H1S_F_RES_ERROR;
1373 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1374 }
1375 h1s->cs->flags |= CS_FL_EOI;
1376 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1377 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1378 }
1379 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001380 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001381
Christopher Faulet76014fd2019-12-10 11:47:22 +01001382 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001383 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001384 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001385 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet76014fd2019-12-10 11:47:22 +01001386 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001387}
1388
1389/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001390 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001391 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1392 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001393 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001394static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001395{
Christopher Faulet539e0292018-11-19 10:40:09 +01001396 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001397 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001398 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001399 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001400 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001401 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001402
Christopher Faulet539e0292018-11-19 10:40:09 +01001403 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001404 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001405
Christopher Fauletf2824e62018-10-01 12:12:37 +02001406 if (!conn_is_back(h1c->conn)) {
1407 h1m = &h1s->req;
1408 errflag = H1S_F_REQ_ERROR;
1409 }
1410 else {
1411 h1m = &h1s->res;
1412 errflag = H1S_F_RES_ERROR;
1413 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001414
Christopher Faulet74027762019-02-26 14:45:05 +01001415 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001416 if (h1s->flags & errflag)
1417 goto end;
1418
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001419 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001420 size_t used = htx_used_space(htx);
1421
Christopher Faulet129817b2018-09-20 16:14:40 +02001422 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001423 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001424 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001425 if (!ret)
1426 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001427
1428 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1429 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1430
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001431 if ((h1m->flags & H1_MF_RESP) &&
1432 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1433 h1m_init_res(&h1s->res);
1434 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001435 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001436 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001437 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001438 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001439 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001440 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001441 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001442 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001443
1444 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1445 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001446 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001447 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001448 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1449 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1450 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001451 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001452
Christopher Faulet76014fd2019-12-10 11:47:22 +01001453 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1454 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001455 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001456 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001457 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1458 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1459 break;
1460
1461 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1462 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1463 }
1464
Christopher Fauletf3158e92019-11-15 11:14:23 +01001465 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1466 h1_set_req_tunnel_mode(h1s);
1467 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001468 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001469 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
Christopher Faulet23021ad2020-07-10 10:01:26 +02001470 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001471 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001472 else
1473 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001474 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001475 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001476 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001477 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 if (!ret)
1479 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001480
1481 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1482 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001483 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001484 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001485 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001486 break;
1487 }
1488
Christopher Faulet30db3d72019-05-17 15:35:33 +02001489 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001490 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001491
Christopher Faulet6b81df72019-10-01 22:08:43 +02001492 if (h1s->flags & errflag) {
1493 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001494 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001495 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001496
Christopher Faulet539e0292018-11-19 10:40:09 +01001497 b_del(&h1c->ibuf, total);
1498
1499 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001500 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001501 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001502 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001503 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001504 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE);
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001505 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001506 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001507
Christopher Fauletcf56b992018-12-11 16:12:31 +01001508 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1509
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001510 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001511 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001512 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001513 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001514 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001515 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001516 if (h1m->state == H1_MSG_TUNNEL)
1517 h1s->cs->flags |= CS_FL_EOI;
1518 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001519 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001520 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001521
Christopher Faulet6b81df72019-10-01 22:08:43 +02001522 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001523 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001524
1525 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001526 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001527 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001528 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001529 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001530}
1531
Christopher Faulet129817b2018-09-20 16:14:40 +02001532/*
1533 * Process outgoing data. It parses data and transfer them from the channel buffer into
1534 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1535 * 0 if it couldn't proceed.
1536 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001537static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1538{
Christopher Faulet129817b2018-09-20 16:14:40 +02001539 struct h1s *h1s = h1c->h1s;
1540 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001541 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001542 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001543 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001544 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001545 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001546
Christopher Faulet47365272018-10-31 17:40:50 +01001547 if (!count)
1548 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001549
Christopher Faulet94b2c762019-05-24 15:28:57 +02001550 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001551 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1552
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001553 if (htx_is_empty(chn_htx))
1554 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001555
Christopher Faulet51dbc942018-09-13 09:05:15 +02001556 if (!h1_get_buf(h1c, &h1c->obuf)) {
1557 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001558 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 +02001559 goto end;
1560 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001561
Christopher Fauletf2824e62018-10-01 12:12:37 +02001562 if (!conn_is_back(h1c->conn)) {
1563 h1m = &h1s->res;
1564 errflag = H1S_F_RES_ERROR;
1565 }
1566 else {
1567 h1m = &h1s->req;
1568 errflag = H1S_F_REQ_ERROR;
1569 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001570
Christopher Faulet0e54d542019-07-04 21:22:34 +02001571 if (h1s->flags & errflag)
1572 goto end;
1573
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001574 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001575 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001576
Willy Tarreau3815b222018-12-11 19:50:43 +01001577 /* Perform some optimizations to reduce the number of buffer copies.
1578 * First, if the mux's buffer is empty and the htx area contains
1579 * exactly one data block of the same size as the requested count,
1580 * then it's possible to simply swap the caller's buffer with the
1581 * mux's output buffer and adjust offsets and length to match the
1582 * entire DATA HTX block in the middle. In this case we perform a
1583 * true zero-copy operation from end-to-end. This is the situation
1584 * that happens all the time with large files. Second, if this is not
1585 * possible, but the mux's output buffer is empty, we still have an
1586 * opportunity to avoid the copy to the intermediary buffer, by making
1587 * the intermediary buffer's area point to the output buffer's area.
1588 * In this case we want to skip the HTX header to make sure that copies
1589 * remain aligned and that this operation remains possible all the
1590 * time. This goes for headers, data blocks and any data extracted from
1591 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001592 */
1593 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001594 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001595 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001596 htx_get_blk_value(chn_htx, blk).len == count) {
1597 void *old_area = h1c->obuf.area;
1598
Christopher Faulet6b81df72019-10-01 22:08:43 +02001599 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 +01001600 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001601 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001602 h1c->obuf.data = count;
1603
1604 buf->area = old_area;
1605 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001606
Christopher Faulet6b81df72019-10-01 22:08:43 +02001607 chn_htx = (struct htx *)buf->area;
1608 htx_reset(chn_htx);
1609
Christopher Fauletadb22202018-12-12 10:32:09 +01001610 /* The message is chunked. We need to emit the chunk
1611 * size. We have at least the size of the struct htx to
1612 * write the chunk envelope. It should be enough.
1613 */
1614 if (h1m->flags & H1_MF_CHNK) {
1615 h1_emit_chunk_size(&h1c->obuf, count);
1616 h1_emit_chunk_crlf(&h1c->obuf);
1617 }
1618
Willy Tarreau3815b222018-12-11 19:50:43 +01001619 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001620 if (h1m->state == H1_MSG_DATA)
1621 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001622 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001623 else
1624 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001625 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001626 goto out;
1627 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001628 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001629 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001630 else
1631 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001632
Christopher Fauletc2518a52019-06-25 21:41:02 +02001633 tmp.data = 0;
1634 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001635 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001636 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001637 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001638 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001639 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001640 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001641
Christopher Fauletb2e84162018-12-06 11:39:49 +01001642 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001643 if (type != HTX_BLK_DATA && vlen > count)
1644 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001645
Christopher Faulet94b2c762019-05-24 15:28:57 +02001646 if (type == HTX_BLK_UNUSED)
1647 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001648
Christopher Faulet94b2c762019-05-24 15:28:57 +02001649 switch (h1m->state) {
1650 case H1_MSG_RQBEFORE:
1651 if (type != HTX_BLK_REQ_SL)
1652 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001653 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 +02001654 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001655 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001656 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001657 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001658 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001659 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001660 if (sl->flags & HTX_SL_F_BODYLESS)
1661 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001662 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001663 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001664
Christopher Faulet94b2c762019-05-24 15:28:57 +02001665 case H1_MSG_RPBEFORE:
1666 if (type != HTX_BLK_RES_SL)
1667 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001668 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 +02001669 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001670 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001671 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001672 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001673 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001674 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001675 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001676 if (sl->info.res.status < 200 &&
1677 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001678 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 h1m->state = H1_MSG_HDR_FIRST;
1680 break;
1681
Christopher Faulet94b2c762019-05-24 15:28:57 +02001682 case H1_MSG_HDR_FIRST:
1683 case H1_MSG_HDR_NAME:
1684 case H1_MSG_HDR_L2_LWS:
1685 if (type == HTX_BLK_EOH)
1686 goto last_lf;
1687 if (type != HTX_BLK_HDR)
1688 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001689
Christopher Faulet9768c262018-10-22 09:34:31 +02001690 h1m->state = H1_MSG_HDR_NAME;
1691 n = htx_get_blk_name(chn_htx, blk);
1692 v = htx_get_blk_value(chn_htx, blk);
1693
Christopher Faulet86d144c2019-08-14 16:32:25 +02001694 /* Skip all pseudo-headers */
1695 if (*(n.ptr) == ':')
1696 goto skip_hdr;
1697
Christopher Fauletb045bb22020-02-28 10:42:20 +01001698 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001699 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001700 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001701 /* Only skip C-L header with invalid value. */
1702 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001703 goto skip_hdr;
1704 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001705 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001706 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001707 if (!v.len)
1708 goto skip_hdr;
1709 }
1710
Christopher Faulet67d58092019-10-02 10:51:38 +02001711 /* Skip header if same name is used to add the server name */
1712 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1713 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1714 goto skip_hdr;
1715
Christopher Faulet98fbe952019-07-22 16:18:24 +02001716 /* Try to adjust the case of the header name */
1717 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1718 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001719 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001720 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001721 skip_hdr:
1722 h1m->state = H1_MSG_HDR_L2_LWS;
1723 break;
1724
Christopher Faulet94b2c762019-05-24 15:28:57 +02001725 case H1_MSG_LAST_LF:
1726 if (type != HTX_BLK_EOH)
1727 goto error;
1728 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001729 h1m->state = H1_MSG_LAST_LF;
1730 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001731 /* If the reply comes from haproxy while the request is
1732 * not finished, we force the connection close. */
1733 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1734 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1735 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1736 }
1737
1738 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001739 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001740 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001741 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001742 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001743 /* Try to adjust the case of the header name */
1744 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1745 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001746 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001747 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001748 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001749 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001750 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001751
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001752 if ((h1s->meth != HTTP_METH_CONNECT &&
1753 (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 +01001754 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1755 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1756 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1757 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1758 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001759 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001760 n = ist("transfer-encoding");
1761 v = ist("chunked");
1762 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1763 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001764 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001765 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001766 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001767 h1m->flags |= H1_MF_CHNK;
1768 }
1769
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001770 /* Now add the server name to a header (if requested) */
1771 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1772 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1773 struct server *srv = objt_server(h1c->conn->target);
1774
1775 if (srv) {
1776 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1777 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001778
1779 /* Try to adjust the case of the header name */
1780 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1781 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001782 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001783 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001784 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001785 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001786 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1787 }
1788
Christopher Fauletc2518a52019-06-25 21:41:02 +02001789 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001790 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001791
Christopher Faulet6b81df72019-10-01 22:08:43 +02001792 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1793 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1794
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001795 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1796 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1797 h1_set_req_tunnel_mode(h1s);
1798 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001799 else if ((h1m->flags & H1_MF_RESP) &&
1800 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001801 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001802 * to the client. Switch the response to tunnel mode.
1803 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001804 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001805 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 +01001806 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001807 else if ((h1m->flags & H1_MF_RESP) &&
1808 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1809 h1m_init_res(&h1s->res);
1810 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001811 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001812 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001813 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001814 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001815 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001816 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1817 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001818 else
1819 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001820 break;
1821
Christopher Faulet94b2c762019-05-24 15:28:57 +02001822 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001823 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001824 if (type == HTX_BLK_EOM) {
1825 /* Chunked message without explicit trailers */
1826 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001827 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001828 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001829 }
1830 goto done;
1831 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001832 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001833 /* If the message is not chunked, never
1834 * add the last chunk. */
1835 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001836 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001837 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 +02001838 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001839 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001840 else if (type != HTX_BLK_DATA)
1841 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001842
1843 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 +02001844
1845
1846 if (vlen > count) {
1847 /* Get the maximum amount of data we can xferred */
1848 vlen = count;
1849 }
1850
1851 chklen = 0;
1852 if (h1m->flags & H1_MF_CHNK) {
1853 chklen = b_room(&tmp);
1854 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1855 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1856 (chklen < 1048576) ? 5 : 8);
1857 chklen += 4; /* 2 x CRLF */
1858 }
1859
1860 if (vlen + chklen > b_room(&tmp)) {
1861 /* too large for the buffer */
1862 if (chklen >= b_room(&tmp))
1863 goto full;
1864 vlen = b_room(&tmp) - chklen;
1865 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001866 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001867 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001868 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001869 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001870
1871 if (h1m->state == H1_MSG_DATA)
1872 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001873 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001874 else
1875 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001876 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001877 break;
1878
Christopher Faulet94b2c762019-05-24 15:28:57 +02001879 case H1_MSG_TRAILERS:
1880 if (type == HTX_BLK_EOM)
1881 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001882 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001883 goto error;
1884 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001885 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001886 /* If the message is not chunked, ignore
1887 * trailers. It may happen with H2 messages. */
1888 if (!(h1m->flags & H1_MF_CHNK))
1889 break;
1890
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001891 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001892 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001893 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001894 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1895 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001896 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001897 else { // HTX_BLK_TLR
1898 n = htx_get_blk_name(chn_htx, blk);
1899 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001900
1901 /* Try to adjust the case of the header name */
1902 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1903 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001904 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001905 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001906 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001907 break;
1908
Christopher Faulet94b2c762019-05-24 15:28:57 +02001909 case H1_MSG_DONE:
1910 if (type != HTX_BLK_EOM)
1911 goto error;
1912 done:
1913 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001914 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1915 h1_set_req_tunnel_mode(h1s);
1916 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1917 }
1918 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001919 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001920 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001921 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 +02001922 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001923
1924 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1925 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001926 break;
1927
Christopher Faulet9768c262018-10-22 09:34:31 +02001928 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001929 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001930 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001931 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001932 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001933 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001934 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001935 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1936 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001937 break;
1938 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001939
1940 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001941 total += vlen;
1942 count -= vlen;
1943 if (sz == vlen)
1944 blk = htx_remove_blk(chn_htx, blk);
1945 else {
1946 htx_cut_data_blk(chn_htx, blk, vlen);
1947 break;
1948 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001949 }
1950
Christopher Faulet9768c262018-10-22 09:34:31 +02001951 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001952 /* when the output buffer is empty, tmp shares the same area so that we
1953 * only have to update pointers and lengths.
1954 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001955 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1956 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001957 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001958 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001959
Willy Tarreau3815b222018-12-11 19:50:43 +01001960 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001961 out:
1962 if (!buf_room_for_htx_data(&h1c->obuf)) {
1963 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001964 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001965 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001966 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001967 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001968 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001969
1970 full:
1971 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1972 h1c->flags |= H1C_F_OUT_FULL;
1973 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001974}
1975
Christopher Faulet51dbc942018-09-13 09:05:15 +02001976/*********************************************************/
1977/* functions below are I/O callbacks from the connection */
1978/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001979static void h1_wake_stream_for_recv(struct h1s *h1s)
1980{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001981 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001982 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001983 tasklet_wakeup(h1s->subs->tasklet);
1984 h1s->subs->events &= ~SUB_RETRY_RECV;
1985 if (!h1s->subs->events)
1986 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001987 }
1988}
1989static void h1_wake_stream_for_send(struct h1s *h1s)
1990{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001991 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001992 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001993 tasklet_wakeup(h1s->subs->tasklet);
1994 h1s->subs->events &= ~SUB_RETRY_SEND;
1995 if (!h1s->subs->events)
1996 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001997 }
1998}
1999
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000/*
2001 * Attempt to read data, and subscribe if none available
2002 */
2003static int h1_recv(struct h1c *h1c)
2004{
2005 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002006 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01002007 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002008 int rcvd = 0;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002009 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002010
Christopher Faulet6b81df72019-10-01 22:08:43 +02002011 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2012
2013 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2014 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002015 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002016 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002017
Olivier Houchard75159a92018-12-03 18:46:09 +01002018 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002019 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01002020 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002021 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01002022 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002023
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002024 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2025 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002026 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002027 goto end;
2028 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002029
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002030 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002031 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002032 h1_wake_stream_for_recv(h1s);
2033 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002034 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002035 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002036 }
2037
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002038 /*
2039 * If we only have a small amount of data, realign it,
2040 * it's probably cheaper than doing 2 recv() calls.
2041 */
2042 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2043 b_slow_realign(&h1c->ibuf, trash.area, 0);
2044
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002045 /* avoid useless reads after first responses */
Christopher Faulet69f2cb82020-09-21 11:59:21 +02002046 if (h1s && ((!conn_is_back(conn) && h1s->req.state == H1_MSG_RQBEFORE) ||
2047 (conn_is_back(conn) && h1s->res.state == H1_MSG_RPBEFORE)))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002048 flags |= CO_RFL_READ_ONCE;
2049
Willy Tarreau45f2b892018-12-05 07:59:27 +01002050 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002051 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002052 if (h1c->flags & H1C_F_IN_FULL) {
2053 h1c->flags &= ~H1C_F_IN_FULL;
2054 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2055 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002056
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002057 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002058 if (!b_data(&h1c->ibuf)) {
2059 /* try to pre-align the buffer like the rxbufs will be
2060 * to optimize memory copies.
2061 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002062 h1c->ibuf.head = sizeof(struct htx);
2063 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002064 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002065 }
Christopher Faulet47365272018-10-31 17:40:50 +01002066 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002067 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002068 rcvd = 1;
Christopher Faulet3b536a32020-09-30 14:06:23 +02002069 if (h1s && h1s->cs)
Christopher Faulet37e36072018-12-04 15:54:12 +01002070 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Faulet47365272018-10-31 17:40:50 +01002071 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002072
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002073 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002074 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002075 goto end;
2076 }
2077
Christopher Faulet6b81df72019-10-01 22:08:43 +02002078 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002079 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002080
Christopher Faulet9768c262018-10-22 09:34:31 +02002081 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002082 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2083 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002084
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002085 if (conn_xprt_read0_pending(conn) && h1s) {
2086 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002087 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002088 rcvd = 1;
2089 }
2090
Christopher Faulet51dbc942018-09-13 09:05:15 +02002091 if (!b_data(&h1c->ibuf))
2092 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002093 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002094 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002095 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2096 }
2097
2098 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002099 return rcvd;
2100}
2101
2102
2103/*
2104 * Try to send data if possible
2105 */
2106static int h1_send(struct h1c *h1c)
2107{
2108 struct connection *conn = h1c->conn;
2109 unsigned int flags = 0;
2110 size_t ret;
2111 int sent = 0;
2112
Christopher Faulet6b81df72019-10-01 22:08:43 +02002113 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2114
2115 if (conn->flags & CO_FL_ERROR) {
2116 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002117 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002118 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002119
2120 if (!b_data(&h1c->obuf))
2121 goto end;
2122
Christopher Faulet46230362019-10-17 16:04:20 +02002123 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002124 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002125 if (h1c->flags & H1C_F_CO_STREAMER)
2126 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002127
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002128 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002130 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002131 if (h1c->flags & H1C_F_OUT_FULL) {
2132 h1c->flags &= ~H1C_F_OUT_FULL;
2133 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2134 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002135 b_del(&h1c->obuf, ret);
2136 sent = 1;
2137 }
2138
Christopher Faulet145aa472018-12-06 10:56:20 +01002139 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002140 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002141 /* error or output closed, nothing to send, clear the buffer to release it */
2142 b_reset(&h1c->obuf);
2143 }
2144
Christopher Faulet51dbc942018-09-13 09:05:15 +02002145 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002146 if (!(h1c->flags & H1C_F_OUT_FULL))
2147 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002148
Christopher Faulet51dbc942018-09-13 09:05:15 +02002149 /* We're done, no more to send */
2150 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002151 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002152 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002153 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2154 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002155 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002156 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002157 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002158 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2159 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002160 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002161 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002162
Christopher Faulet6b81df72019-10-01 22:08:43 +02002163 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002164 return sent;
2165}
2166
Christopher Faulet51dbc942018-09-13 09:05:15 +02002167
2168/* callback called on any event by the connection handler.
2169 * It applies changes and returns zero, or < 0 if it wants immediate
2170 * destruction of the connection.
2171 */
2172static int h1_process(struct h1c * h1c)
2173{
2174 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002175 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002176
Christopher Faulet6b81df72019-10-01 22:08:43 +02002177 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2178
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002179 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002180 return -1;
2181
Christopher Fauletfeb11742018-11-29 15:12:34 +01002182 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002183 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002184 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002185 goto release;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002186 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002187 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002188 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002189 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002190 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002191 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002192 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002193 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002194 }
2195
Christopher Faulet4e741552020-09-30 13:47:09 +02002196 if (b_data(&h1c->ibuf) && h1s->sess->t_idle == -1)
2197 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002198
Christopher Faulet6b81df72019-10-01 22:08:43 +02002199 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002200 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002201 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2202 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002203
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002204 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002205 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002206 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002207 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002208 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002209 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002210 h1s->cs->data_cb->wake(h1s->cs);
2211 }
Christopher Faulet47365272018-10-31 17:40:50 +01002212 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002213 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002214 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002215 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002216
2217 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002218 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002219 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002220 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002221}
2222
2223static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2224{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002225 struct connection *conn;
2226 struct tasklet *tl = (struct tasklet *)t;
2227 int conn_in_list;
2228 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002229 int ret = 0;
2230
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002231
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002232 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002233 if (tl->context == NULL) {
2234 /* The connection has been taken over by another thread,
2235 * we're no longer responsible for it, so just free the
2236 * tasklet, and do nothing.
2237 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002238 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002239 tasklet_free(tl);
2240 return NULL;
2241 }
2242 h1c = ctx;
2243 conn = h1c->conn;
2244
2245 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2246
2247 /* Remove the connection from the list, to be sure nobody attempts
2248 * to use it while we handle the I/O events
2249 */
2250 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2251 if (conn_in_list)
2252 MT_LIST_DEL(&conn->list);
2253
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002254 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002255
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002256 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002257 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002258 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002259 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002260 if (ret || !h1c->h1s)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002261 ret = h1_process(h1c);
2262 /* If we were in an idle list, we want to add it back into it,
2263 * unless h1_process() returned -1, which mean it has destroyed
2264 * the connection (testing !ret is enough, if h1_process() wasn't
2265 * called then ret will be 0 anyway.
2266 */
2267 if (!ret && conn_in_list) {
2268 struct server *srv = objt_server(conn->target);
2269
2270 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002271 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002272 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002273 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002274 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002275 return NULL;
2276}
2277
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002278static void h1_reset(struct connection *conn)
2279{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002280
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002281}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002282
2283static int h1_wake(struct connection *conn)
2284{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002285 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002286 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002287
Christopher Faulet6b81df72019-10-01 22:08:43 +02002288 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2289
Christopher Faulet539e0292018-11-19 10:40:09 +01002290 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002291 ret = h1_process(h1c);
2292 if (ret == 0) {
2293 struct h1s *h1s = h1c->h1s;
2294
Christopher Faulet6b81df72019-10-01 22:08:43 +02002295 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2296 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002297 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002298 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002299 }
2300 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002301}
2302
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002303/* Connection timeout management. The principle is that if there's no receipt
2304 * nor sending for a certain amount of time, the connection is closed.
2305 */
2306static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2307{
2308 struct h1c *h1c = context;
2309 int expired = tick_is_expired(t->expire, now_ms);
2310
Christopher Faulet6b81df72019-10-01 22:08:43 +02002311 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2312
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002313 if (h1c) {
2314 if (!expired) {
2315 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
2316 return t;
2317 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002318
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002319 /* We're about to destroy the connection, so make sure nobody attempts
2320 * to steal it from us.
2321 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002322 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002323
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002324 /* Somebody already stole the connection from us, so we should not
2325 * free it, we just have to free the task.
2326 */
2327 if (!t->context)
2328 h1c = NULL;
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002329 else if (h1c->conn->flags & CO_FL_LIST_MASK)
2330 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002331
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002332 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002333 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002334
Olivier Houchard3f795f72019-04-17 22:51:06 +02002335 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002336
2337 if (!h1c) {
2338 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002339 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002340 return NULL;
2341 }
2342
2343 h1c->task = NULL;
2344 /* If a stream is still attached to the mux, just set an error and wait
2345 * for the stream's timeout. Otherwise, release the mux. This is only ok
2346 * because same timeouts are used.
2347 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002348 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002349 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002350 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2351 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002352 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002353 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002354
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002355 return NULL;
2356}
2357
Christopher Faulet51dbc942018-09-13 09:05:15 +02002358/*******************************************/
2359/* functions below are used by the streams */
2360/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002361
Christopher Faulet51dbc942018-09-13 09:05:15 +02002362/*
2363 * Attach a new stream to a connection
2364 * (Used for outgoing connections)
2365 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002366static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002367{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002368 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369 struct conn_stream *cs = NULL;
2370 struct h1s *h1s;
2371
Christopher Faulet6b81df72019-10-01 22:08:43 +02002372 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2373 if (h1c->flags & H1C_F_CS_ERROR) {
2374 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 +02002375 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002376 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002377
Christopher Faulet236c93b2020-07-02 09:19:54 +02002378 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002379 if (!cs) {
2380 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 +02002381 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002382 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002383
Olivier Houchardf502aca2018-12-14 19:42:40 +01002384 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002385 if (h1s == NULL) {
2386 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 +02002387 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002388 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002389
Christopher Faulet6b81df72019-10-01 22:08:43 +02002390 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391 return cs;
2392 end:
2393 cs_free(cs);
2394 return NULL;
2395}
2396
2397/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2398 * this mux, it's easy as we can only store a single conn_stream.
2399 */
2400static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2401{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002402 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002403 struct h1s *h1s = h1c->h1s;
2404
2405 if (h1s)
2406 return h1s->cs;
2407
2408 return NULL;
2409}
2410
Christopher Faulet73c12072019-04-08 11:23:22 +02002411static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002412{
Christopher Faulet73c12072019-04-08 11:23:22 +02002413 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002414
Christopher Faulet6b81df72019-10-01 22:08:43 +02002415 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002416 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002417 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002418}
2419
2420/*
2421 * Detach the stream from the connection and possibly release the connection.
2422 */
2423static void h1_detach(struct conn_stream *cs)
2424{
2425 struct h1s *h1s = cs->ctx;
2426 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002427 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002428 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002429
Christopher Faulet6b81df72019-10-01 22:08:43 +02002430 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2431
Christopher Faulet51dbc942018-09-13 09:05:15 +02002432 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002433 if (!h1s) {
2434 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002435 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002436 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002437
Olivier Houchardf502aca2018-12-14 19:42:40 +01002438 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002439 h1c = h1s->h1c;
2440 h1s->cs = NULL;
2441
Christopher Faulet42849b02020-10-05 11:35:17 +02002442 sess->accept_date = date;
2443 sess->tv_accept = now;
2444 sess->t_handshake = 0;
2445 sess->t_idle = -1;
2446
Olivier Houchard8a786902018-12-15 16:05:40 +01002447 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2448 h1s_destroy(h1s);
2449
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002450 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002451 /* If there are any excess server data in the input buffer,
2452 * release it and close the connection ASAP (some data may
2453 * remain in the output buffer). This happens if a server sends
2454 * invalid responses. So in such case, we don't want to reuse
2455 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002456 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002457 if (b_data(&h1c->ibuf)) {
2458 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002459 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002460 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002461 goto release;
2462 }
Christopher Faulet03627242019-07-19 11:34:08 +02002463
Christopher Faulet08016ab2020-07-01 16:10:06 +02002464 if (h1c->conn->flags & CO_FL_PRIVATE) {
2465 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002466 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2467 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002468 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002469 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002470 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002471 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002472 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002473 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002474 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2475 goto end;
2476 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002477 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002478 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002479 if (h1c->conn->owner == sess)
2480 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002481 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002482 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002483 /* The server doesn't want it, let's kill the connection right away */
2484 h1c->conn->mux->destroy(h1c);
2485 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2486 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002487 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002488 /* At this point, the connection has been added to the
2489 * server idle list, so another thread may already have
2490 * hijacked it, so we can't do anything with it.
2491 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002492 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002493 }
2494 }
2495
Christopher Fauletf1204b82019-07-19 14:51:06 +02002496 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002497 /* 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 +02002498 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2499 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2500 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002501 !h1c->conn->owner) {
2502 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002503 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002504 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002505 else {
Olivier Houchard69664412020-03-25 12:24:11 +01002506 /* If we have a new request, process it immediately */
Olivier Houchardc3500c32020-03-25 17:05:21 +01002507 if (unlikely(b_data(&h1c->ibuf)))
Olivier Houchard69664412020-03-25 12:24:11 +01002508 h1_process(h1c);
2509 else
2510 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002511 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002512 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002513 end:
2514 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002515}
2516
2517
2518static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2519{
2520 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002521 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002522
2523 if (!h1s)
2524 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002525 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002526
Christopher Faulet6b81df72019-10-01 22:08:43 +02002527 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2528
2529 if (cs->flags & CS_FL_KILL_CONN) {
2530 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2531 goto do_shutr;
2532 }
2533 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2534 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002535 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002536 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002537
Christopher Faulet6b81df72019-10-01 22:08:43 +02002538 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2539 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2540 goto end;
2541 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002542
Christopher Faulet7f366362019-04-08 10:51:20 +02002543 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002544 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2545 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002546 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002547 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002548 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002549 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002550 end:
2551 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002552}
2553
2554static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2555{
2556 struct h1s *h1s = cs->ctx;
2557 struct h1c *h1c;
2558
2559 if (!h1s)
2560 return;
2561 h1c = h1s->h1c;
2562
Christopher Faulet6b81df72019-10-01 22:08:43 +02002563 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2564
2565 if (cs->flags & CS_FL_KILL_CONN) {
2566 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002567 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002568 }
2569 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2570 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2571 goto do_shutw;
2572 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002573
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002574 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002575 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2576 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2577 goto end;
2578 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002579
Christopher Faulet7f366362019-04-08 10:51:20 +02002580 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002581 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2582 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002583 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002584 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002585 end:
2586 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002587}
2588
Christopher Faulet666a0c42019-01-08 11:12:04 +01002589static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002590{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002591 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002592
Christopher Faulet6b81df72019-10-01 22:08:43 +02002593 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002594 conn_xprt_shutw(conn);
2595 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002596 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002597 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002598}
2599
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002600/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2601 * The <es> pointer is not allowed to differ from the one passed to the
2602 * subscribe() call. It always returns zero.
2603 */
2604static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002605{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002606 struct h1s *h1s = cs->ctx;
2607
2608 if (!h1s)
2609 return 0;
2610
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002611 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002612 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002613
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002614 es->events &= ~event_type;
2615 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002616 h1s->subs = NULL;
2617
2618 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002619 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002620
2621 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002622 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002623
Christopher Faulet51dbc942018-09-13 09:05:15 +02002624 return 0;
2625}
2626
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002627/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2628 * event subscriber <es> is not allowed to change from a previous call as long
2629 * as at least one event is still subscribed. The <event_type> must only be a
2630 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2631 * the conn_stream <cs> was already detached, in which case it will return -1.
2632 */
2633static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002634{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002635 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002636 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002637
2638 if (!h1s)
2639 return -1;
2640
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002641 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002642 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002643
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002644 es->events |= event_type;
2645 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002646
2647 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002648 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002649
2650
Christopher Faulet6b81df72019-10-01 22:08:43 +02002651 if (event_type & SUB_RETRY_SEND) {
2652 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002653 /*
2654 * If the conn_stream attempt to subscribe, and the
2655 * mux isn't subscribed to the connection, then it
2656 * probably means the connection wasn't established
2657 * yet, so we have to subscribe.
2658 */
2659 h1c = h1s->h1c;
2660 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2661 h1c->conn->xprt->subscribe(h1c->conn,
2662 h1c->conn->xprt_ctx,
2663 SUB_RETRY_SEND,
2664 &h1c->wait_event);
2665 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002666 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002667}
2668
2669/* Called from the upper layer, to receive data */
2670static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2671{
2672 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002673 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002674 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002675 size_t ret = 0;
2676
Willy Tarreau022e5e52020-09-10 09:33:15 +02002677 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002678 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002679 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002680 else
2681 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002682
Christopher Faulete18777b2019-04-16 16:46:36 +02002683 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02002684 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002685 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002686 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2687 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002688 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002689 else {
Christopher Faulet7b7016b2020-07-03 15:12:00 +02002690 if (ret && h1s->flags & H1S_F_SPLICED_DATA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002691 h1s->flags &= ~H1S_F_SPLICED_DATA;
2692 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2693 }
2694 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002695 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002696 }
Willy Tarreau022e5e52020-09-10 09:33:15 +02002697 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002698 return ret;
2699}
2700
2701
2702/* Called from the upper layer, to send data */
2703static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2704{
2705 struct h1s *h1s = cs->ctx;
2706 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002707 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002708
2709 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002710 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002711 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002712
Willy Tarreau022e5e52020-09-10 09:33:15 +02002713 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002714
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002715 /* If we're not connected yet, or we're waiting for a handshake, stop
2716 * now, as we don't want to remove everything from the channel buffer
2717 * before we're sure we can send it.
2718 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01002719 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002720 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002721 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002722 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002723
Christopher Faulet46230362019-10-17 16:04:20 +02002724 /* Inherit some flags from the upper layer */
2725 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
2726 if (flags & CO_SFL_MSG_MORE)
2727 h1c->flags |= H1C_F_CO_MSG_MORE;
2728 if (flags & CO_SFL_STREAMER)
2729 h1c->flags |= H1C_F_CO_STREAMER;
2730
Christopher Fauletc31872f2019-06-04 22:09:36 +02002731 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002732 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002733
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002734 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2735 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002736 else
2737 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02002738
2739 if ((count - ret) > 0)
2740 h1c->flags |= H1C_F_CO_MSG_MORE;
2741
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002742 if (!ret)
2743 break;
2744 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002745 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002746 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002747 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002748 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02002749 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002750 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002751 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002752}
2753
Willy Tarreaue5733232019-05-22 19:24:06 +02002754#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002755/* Send and get, using splicing */
2756static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2757{
2758 struct h1s *h1s = cs->ctx;
2759 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2760 int ret = 0;
2761
Willy Tarreau022e5e52020-09-10 09:33:15 +02002762 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002763
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002764 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002765 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002766 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2767 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2768 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002769 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002770 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002771 goto end;
2772 }
2773
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002774 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002775 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002776 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002777 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002778 }
2779
Christopher Faulet27182292020-07-03 15:08:49 +02002780 if (!(h1s->flags & H1S_F_SPLICED_DATA)) {
2781 h1s->flags &= ~H1S_F_BUF_FLUSH;
2782 h1s->flags |= H1S_F_SPLICED_DATA;
2783 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
2784 }
Christopher Faulet0060be92020-07-03 15:02:25 +02002785
2786 if (!h1_recv_allowed(h1s->h1c)) {
2787 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
2788 goto end;
2789 }
2790
Christopher Faulet1be55f92018-10-02 15:59:23 +02002791 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2792 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002793 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002794 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002795 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002796 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002797 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002798 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2799 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002800 }
2801
Christopher Faulet1be55f92018-10-02 15:59:23 +02002802 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002803 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002804 h1s->flags |= H1S_F_REOS;
Christopher Fauletf3158e92019-11-15 11:14:23 +01002805 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002806 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002807 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002808
Christopher Fauleta131a8f2020-07-07 10:56:40 +02002809 if ((h1s->flags & H1S_F_REOS) ||
2810 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02002811 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
2812 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002813 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02002814 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002815
Willy Tarreau022e5e52020-09-10 09:33:15 +02002816 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002817 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002818}
2819
2820static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2821{
2822 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002823 int ret = 0;
2824
Willy Tarreau022e5e52020-09-10 09:33:15 +02002825 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002826
Christopher Faulet1be55f92018-10-02 15:59:23 +02002827 if (b_data(&h1s->h1c->obuf))
2828 goto end;
2829
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002830 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002831 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002832 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002833 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2834 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002835 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002836 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002837 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002838
Willy Tarreau022e5e52020-09-10 09:33:15 +02002839 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002840 return ret;
2841}
2842#endif
2843
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002844static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2845{
2846 int ret = 0;
2847 switch (mux_ctl) {
2848 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01002849 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002850 ret |= MUX_STATUS_READY;
2851 return ret;
2852 default:
2853 return -1;
2854 }
2855}
2856
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002857/* for debugging with CLI's "show fd" command */
2858static void h1_show_fd(struct buffer *msg, struct connection *conn)
2859{
2860 struct h1c *h1c = conn->ctx;
2861 struct h1s *h1s = h1c->h1s;
2862
Christopher Fauletf376a312019-01-04 15:16:06 +01002863 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2864 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002865 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2866 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2867 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2868 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2869
2870 if (h1s) {
2871 char *method;
2872
2873 if (h1s->meth < HTTP_METH_OTHER)
2874 method = http_known_methods[h1s->meth].ptr;
2875 else
2876 method = "UNKNOWN";
2877 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2878 " .meth=%s status=%d",
2879 h1s, h1s->flags,
2880 h1m_state_str(h1s->req.state),
2881 h1m_state_str(h1s->res.state), method, h1s->status);
2882 if (h1s->cs)
2883 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2884 h1s->cs->flags, h1s->cs->data);
2885 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002886}
2887
2888
2889/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2890static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2891{
2892 struct h1_hdr_entry *entry;
2893
2894 /* Be sure there is a non-empty <to> */
2895 if (!strlen(to)) {
2896 memprintf(err, "expect <to>");
2897 return -1;
2898 }
2899
2900 /* Be sure only the case differs between <from> and <to> */
2901 if (strcasecmp(from, to)) {
2902 memprintf(err, "<from> and <to> must not differ execpt the case");
2903 return -1;
2904 }
2905
2906 /* Be sure <from> does not already existsin the tree */
2907 if (ebis_lookup(&hdrs_map.map, from)) {
2908 memprintf(err, "duplicate entry '%s'", from);
2909 return -1;
2910 }
2911
2912 /* Create the entry and insert it in the tree */
2913 entry = malloc(sizeof(*entry));
2914 if (!entry) {
2915 memprintf(err, "out of memory");
2916 return -1;
2917 }
2918
2919 entry->node.key = strdup(from);
2920 entry->name.ptr = strdup(to);
2921 entry->name.len = strlen(to);
2922 if (!entry->node.key || !entry->name.ptr) {
2923 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01002924 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002925 free(entry);
2926 memprintf(err, "out of memory");
2927 return -1;
2928 }
2929 ebis_insert(&hdrs_map.map, &entry->node);
2930 return 0;
2931}
2932
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002933/* Migrate the the connection to the current thread.
2934 * Return 0 if successful, non-zero otherwise.
2935 * Expected to be called with the old thread lock held.
2936 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02002937static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002938{
2939 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02002940 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002941
2942 if (fd_takeover(conn->handle.fd, conn) != 0)
2943 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02002944
2945 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
2946 /* We failed to takeover the xprt, even if the connection may
2947 * still be valid, flag it as error'd, as we have already
2948 * taken over the fd, and wake the tasklet, so that it will
2949 * destroy it.
2950 */
2951 conn->flags |= CO_FL_ERROR;
2952 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
2953 return -1;
2954 }
2955
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002956 if (h1c->wait_event.events)
2957 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
2958 h1c->wait_event.events, &h1c->wait_event);
2959 /* To let the tasklet know it should free itself, and do nothing else,
2960 * set its context to NULL.
2961 */
2962 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02002963 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02002964
2965 task = h1c->task;
2966 if (task) {
2967 task->context = NULL;
2968 h1c->task = NULL;
2969 __ha_barrier_store();
2970 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002971
2972 h1c->task = task_new(tid_bit);
2973 if (!h1c->task) {
2974 h1_release(h1c);
2975 return -1;
2976 }
2977 h1c->task->process = h1_timeout_task;
2978 h1c->task->context = h1c;
2979 }
2980 h1c->wait_event.tasklet = tasklet_new();
2981 if (!h1c->wait_event.tasklet) {
2982 h1_release(h1c);
2983 return -1;
2984 }
2985 h1c->wait_event.tasklet->process = h1_io_cb;
2986 h1c->wait_event.tasklet->context = h1c;
2987 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
2988 SUB_RETRY_RECV, &h1c->wait_event);
2989
2990 return 0;
2991}
2992
2993
Christopher Faulet98fbe952019-07-22 16:18:24 +02002994static void h1_hdeaders_case_adjust_deinit()
2995{
2996 struct ebpt_node *node, *next;
2997 struct h1_hdr_entry *entry;
2998
2999 node = ebpt_first(&hdrs_map.map);
3000 while (node) {
3001 next = ebpt_next(node);
3002 ebpt_delete(node);
3003 entry = container_of(node, struct h1_hdr_entry, node);
3004 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003005 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003006 free(entry);
3007 node = next;
3008 }
3009 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003010}
3011
Christopher Faulet98fbe952019-07-22 16:18:24 +02003012static int cfg_h1_headers_case_adjust_postparser()
3013{
3014 FILE *file = NULL;
3015 char *c, *key_beg, *key_end, *value_beg, *value_end;
3016 char *err;
3017 int rc, line = 0, err_code = 0;
3018
3019 if (!hdrs_map.name)
3020 goto end;
3021
3022 file = fopen(hdrs_map.name, "r");
3023 if (!file) {
3024 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3025 hdrs_map.name);
3026 err_code |= ERR_ALERT | ERR_FATAL;
3027 goto end;
3028 }
3029
3030 /* now parse all lines. The file may contain only two header name per
3031 * line, separated by spaces. All heading and trailing spaces will be
3032 * ignored. Lines starting with a # are ignored.
3033 */
3034 while (fgets(trash.area, trash.size, file) != NULL) {
3035 line++;
3036 c = trash.area;
3037
3038 /* strip leading spaces and tabs */
3039 while (*c == ' ' || *c == '\t')
3040 c++;
3041
3042 /* ignore emptu lines, or lines beginning with a dash */
3043 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3044 continue;
3045
3046 /* look for the end of the key */
3047 key_beg = c;
3048 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3049 c++;
3050 key_end = c;
3051
3052 /* strip middle spaces and tabs */
3053 while (*c == ' ' || *c == '\t')
3054 c++;
3055
3056 /* look for the end of the value, it is the end of the line */
3057 value_beg = c;
3058 while (*c && *c != '\n' && *c != '\r')
3059 c++;
3060 value_end = c;
3061
3062 /* trim possibly trailing spaces and tabs */
3063 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3064 value_end--;
3065
3066 /* set final \0 and check entries */
3067 *key_end = '\0';
3068 *value_end = '\0';
3069
3070 err = NULL;
3071 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3072 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003073 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3074 hdrs_map.name, err, line);
3075 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003076 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003077 goto end;
3078 }
3079 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003080 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3081 hdrs_map.name, err, line);
3082 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003083 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003084 }
3085 }
3086
3087 end:
3088 if (file)
3089 fclose(file);
3090 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3091 return err_code;
3092}
3093
3094
3095/* config parser for global "h1-outgoing-header-case-adjust" */
3096static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3097 struct proxy *defpx, const char *file, int line,
3098 char **err)
3099{
3100 if (too_many_args(2, args, err, NULL))
3101 return -1;
3102 if (!*(args[1]) || !*(args[2])) {
3103 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3104 return -1;
3105 }
3106 return add_hdr_case_adjust(args[1], args[2], err);
3107}
3108
3109/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3110static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3111 struct proxy *defpx, const char *file, int line,
3112 char **err)
3113{
3114 if (too_many_args(1, args, err, NULL))
3115 return -1;
3116 if (!*(args[1])) {
3117 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3118 return -1;
3119 }
3120 free(hdrs_map.name);
3121 hdrs_map.name = strdup(args[1]);
3122 return 0;
3123}
3124
3125
3126/* config keyword parsers */
3127static struct cfg_kw_list cfg_kws = {{ }, {
3128 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3129 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3130 { 0, NULL, NULL },
3131 }
3132};
3133
3134INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3135REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3136
3137
Christopher Faulet51dbc942018-09-13 09:05:15 +02003138/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003139/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003140/****************************************/
3141
3142/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003143static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003144 .init = h1_init,
3145 .wake = h1_wake,
3146 .attach = h1_attach,
3147 .get_first_cs = h1_get_first_cs,
3148 .detach = h1_detach,
3149 .destroy = h1_destroy,
3150 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003151 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003152 .rcv_buf = h1_rcv_buf,
3153 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003154#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003155 .rcv_pipe = h1_rcv_pipe,
3156 .snd_pipe = h1_snd_pipe,
3157#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003158 .subscribe = h1_subscribe,
3159 .unsubscribe = h1_unsubscribe,
3160 .shutr = h1_shutr,
3161 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003162 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003163 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003164 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003165 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003166 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003167 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003168};
3169
3170
3171/* this mux registers default HTX proto */
3172static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003173{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003174
Willy Tarreau0108d902018-11-25 19:14:37 +01003175INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3176
Christopher Faulet51dbc942018-09-13 09:05:15 +02003177/*
3178 * Local variables:
3179 * c-indent-level: 8
3180 * c-basic-offset: 8
3181 * End:
3182 */