blob: 266a2e70d75085f8b2eb7a62f3ff291b34016c0c [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 Fauletd17ad822020-09-24 15:14:29 +020044#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010045/* 0x00000080 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010048#define H1C_F_ST_EMBRYONIC 0x00000100 /* Set when a H1 stream with no conn-stream is attached to the connection */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010049#define H1C_F_ST_ATTACHED 0x00000200 /* Set when a H1 stream with a conn-stream is attached to the connection (may be not READY) */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010050#define H1C_F_ST_IDLE 0x00000400 /* connection is idle and may be reused
51 * (exclusive to all H1C_F_ST flags and never set when an h1s is attached) */
52#define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
53#define H1C_F_ST_SHUTDOWN 0x00001000 /* connection must be shut down ASAP flushing output first (conn-stream may still be attached) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010054#define H1C_F_ST_READY 0x00002000 /* Set in ATTACHED state with a READY conn-stream. A conn-stream is not ready when
55 * a TCP>H1 upgrade is in progress Thus this flag is only set if ATTACHED is also set */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010056#define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED)
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010057/* 0x00004000 - 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020058
Christopher Fauletb385b502021-01-13 18:47:57 +010059#define H1C_F_WANT_SPLICE 0x00010000 /* Don't read into a buffer because we want to use or we are using splicing */
60#define H1C_F_ERR_PENDING 0x00020000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
61#define H1C_F_WAIT_NEXT_REQ 0x00040000 /* waiting for the next request to start, use keep-alive timeout */
62#define H1C_F_UPG_H2C 0x00080000 /* set if an upgrade to h2 should be done */
63#define H1C_F_CO_MSG_MORE 0x00100000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
64#define H1C_F_CO_STREAMER 0x00200000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
65#define H1C_F_WAIT_OUTPUT 0x00400000 /* Don't read more data for now, waiting sync with output side */
66#define H1C_F_WAIT_INPUT 0x00800000 /* Don't send more data for now, waiting sync with input side */
Christopher Faulet129817b2018-09-20 16:14:40 +020067
Christopher Fauletb385b502021-01-13 18:47:57 +010068/* 0x01000000 - 0x40000000 unusued*/
Christopher Faulet3ced1d12020-11-27 16:44:01 +010069#define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */
Christopher Faulet46230362019-10-17 16:04:20 +020070
Christopher Faulet51dbc942018-09-13 09:05:15 +020071/*
72 * H1 Stream flags (32 bits)
73 */
Christopher Faulet129817b2018-09-20 16:14:40 +020074#define H1S_F_NONE 0x00000000
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050075/* 0x00000001..0x00000004 unused */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020076#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020077#define H1S_F_WANT_KAL 0x00000010
78#define H1S_F_WANT_TUN 0x00000020
79#define H1S_F_WANT_CLO 0x00000040
80#define H1S_F_WANT_MSK 0x00000070
81#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet5696f542020-12-02 16:08:38 +010082#define H1S_F_BODYLESS_RESP 0x00000100 /* Bodyless response message */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020083
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010084/* 0x00000200 unsued */
Christopher Faulet2eed8002020-12-07 11:26:13 +010085#define H1S_F_NOT_IMPL_ERROR 0x00000400 /* Set when a feature is not implemented during the message parsing */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020086#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
87#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
88#define H1S_F_ERROR 0x00001800 /* stream error mask */
89
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020090#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 +020091#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020092
93/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020094struct h1c {
95 struct connection *conn;
96 struct proxy *px;
97 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +020098 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +020099 struct buffer ibuf; /* Input buffer to store data before parsing */
100 struct buffer obuf; /* Output buffer to store data after reformatting */
101
102 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
103 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
104
105 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100106 struct task *task; /* timeout management task */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200107 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
108 int timeout; /* client/server timeout duration */
109 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200110};
111
112/* H1 stream descriptor */
113struct h1s {
114 struct h1c *h1c;
115 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100116 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200117
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100118 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200119
Olivier Houchardf502aca2018-12-14 19:42:40 +0100120 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200121 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200122 struct h1m req;
123 struct h1m res;
124
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500125 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200126 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200127};
128
Christopher Faulet98fbe952019-07-22 16:18:24 +0200129/* Map of headers used to convert outgoing headers */
130struct h1_hdrs_map {
131 char *name;
132 struct eb_root map;
133};
134
135/* An entry in a headers map */
136struct h1_hdr_entry {
137 struct ist name;
138 struct ebpt_node node;
139};
140
141/* Declare the headers map */
142static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
143
144
Christopher Faulet6b81df72019-10-01 22:08:43 +0200145/* trace source and events */
146static void h1_trace(enum trace_level level, uint64_t mask,
147 const struct trace_source *src,
148 const struct ist where, const struct ist func,
149 const void *a1, const void *a2, const void *a3, const void *a4);
150
151/* The event representation is split like this :
152 * h1c - internal H1 connection
153 * h1s - internal H1 stream
154 * strm - application layer
155 * rx - data receipt
156 * tx - data transmission
157 *
158 */
159static const struct trace_event h1_trace_events[] = {
160#define H1_EV_H1C_NEW (1ULL << 0)
161 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
162#define H1_EV_H1C_RECV (1ULL << 1)
163 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
164#define H1_EV_H1C_SEND (1ULL << 2)
165 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
166#define H1_EV_H1C_BLK (1ULL << 3)
167 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
168#define H1_EV_H1C_WAKE (1ULL << 4)
169 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
170#define H1_EV_H1C_END (1ULL << 5)
171 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
172#define H1_EV_H1C_ERR (1ULL << 6)
173 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
174
175#define H1_EV_RX_DATA (1ULL << 7)
176 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
177#define H1_EV_RX_EOI (1ULL << 8)
178 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
179#define H1_EV_RX_HDRS (1ULL << 9)
180 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
181#define H1_EV_RX_BODY (1ULL << 10)
182 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
183#define H1_EV_RX_TLRS (1ULL << 11)
184 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
185
186#define H1_EV_TX_DATA (1ULL << 12)
187 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
188#define H1_EV_TX_EOI (1ULL << 13)
189 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
190#define H1_EV_TX_HDRS (1ULL << 14)
191 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
192#define H1_EV_TX_BODY (1ULL << 15)
193 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
194#define H1_EV_TX_TLRS (1ULL << 16)
195 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
196
197#define H1_EV_H1S_NEW (1ULL << 17)
198 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
199#define H1_EV_H1S_BLK (1ULL << 18)
200 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
201#define H1_EV_H1S_END (1ULL << 19)
202 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
203#define H1_EV_H1S_ERR (1ULL << 20)
204 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
205
206#define H1_EV_STRM_NEW (1ULL << 21)
207 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
208#define H1_EV_STRM_RECV (1ULL << 22)
209 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
210#define H1_EV_STRM_SEND (1ULL << 23)
211 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
212#define H1_EV_STRM_WAKE (1ULL << 24)
213 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
214#define H1_EV_STRM_SHUT (1ULL << 25)
215 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
216#define H1_EV_STRM_END (1ULL << 26)
217 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
218#define H1_EV_STRM_ERR (1ULL << 27)
219 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
220
221 { }
222};
223
224static const struct name_desc h1_trace_lockon_args[4] = {
225 /* arg1 */ { /* already used by the connection */ },
226 /* arg2 */ { .name="h1s", .desc="H1 stream" },
227 /* arg3 */ { },
228 /* arg4 */ { }
229};
230
231static const struct name_desc h1_trace_decoding[] = {
232#define H1_VERB_CLEAN 1
233 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
234#define H1_VERB_MINIMAL 2
235 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
236#define H1_VERB_SIMPLE 3
237 { .name="simple", .desc="add request/response status line or htx info when available" },
238#define H1_VERB_ADVANCED 4
239 { .name="advanced", .desc="add header fields or frame decoding when available" },
240#define H1_VERB_COMPLETE 5
241 { .name="complete", .desc="add full data dump when available" },
242 { /* end */ }
243};
244
245static struct trace_source trace_h1 = {
246 .name = IST("h1"),
247 .desc = "HTTP/1 multiplexer",
248 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
249 .default_cb = h1_trace,
250 .known_events = h1_trace_events,
251 .lockon_args = h1_trace_lockon_args,
252 .decoding = h1_trace_decoding,
253 .report_events = ~0, // report everything by default
254};
255
256#define TRACE_SOURCE &trace_h1
257INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
258
Christopher Faulet51dbc942018-09-13 09:05:15 +0200259/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100260DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
261DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200262
Christopher Faulet51dbc942018-09-13 09:05:15 +0200263static int h1_recv(struct h1c *h1c);
264static int h1_send(struct h1c *h1c);
265static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100266/* h1_io_cb is exported to see it resolved in "show fd" */
267struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100268static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200269static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200270static void h1_wake_stream_for_recv(struct h1s *h1s);
271static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200272
Christopher Faulet6b81df72019-10-01 22:08:43 +0200273/* the H1 traces always expect that arg1, if non-null, is of type connection
274 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
275 * that arg3, if non-null, is a htx for rx/tx headers.
276 */
277static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
278 const struct ist where, const struct ist func,
279 const void *a1, const void *a2, const void *a3, const void *a4)
280{
281 const struct connection *conn = a1;
282 const struct h1c *h1c = conn ? conn->ctx : NULL;
283 const struct h1s *h1s = a2;
284 const struct htx *htx = a3;
285 const size_t *val = a4;
286
287 if (!h1c)
288 h1c = (h1s ? h1s->h1c : NULL);
289
290 if (!h1c || src->verbosity < H1_VERB_CLEAN)
291 return;
292
293 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200294 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200295
296 /* Display request and response states if h1s is defined */
297 if (h1s)
298 chunk_appendf(&trace_buf, " [%s, %s]",
299 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
300
301 if (src->verbosity == H1_VERB_CLEAN)
302 return;
303
304 /* Display the value to the 4th argument (level > STATE) */
305 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100306 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200307
308 /* Display status-line if possible (verbosity > MINIMAL) */
309 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
310 const struct htx_blk *blk = htx_get_head_blk(htx);
311 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
312 enum htx_blk_type type = htx_get_blk_type(blk);
313
314 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
315 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
316 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
317 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
318 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
319 }
320
321 /* Display h1c info and, if defined, h1s info (pointer + flags) */
322 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
323 if (h1s)
324 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
325
326 if (src->verbosity == H1_VERB_MINIMAL)
327 return;
328
329 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
330 if (src->level > TRACE_LEVEL_USER) {
331 if (src->verbosity == H1_VERB_COMPLETE ||
332 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
333 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
334 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
335 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
336 if (src->verbosity == H1_VERB_COMPLETE ||
337 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
338 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
339 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
340 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
341 }
342
343 /* Display htx info if defined (level > USER) */
344 if (src->level > TRACE_LEVEL_USER && htx) {
345 int full = 0;
346
347 /* Full htx info (level > STATE && verbosity > SIMPLE) */
348 if (src->level > TRACE_LEVEL_STATE) {
349 if (src->verbosity == H1_VERB_COMPLETE)
350 full = 1;
351 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
352 full = 1;
353 }
354
355 chunk_memcat(&trace_buf, "\n\t", 2);
356 htx_dump(&trace_buf, htx, full);
357 }
358}
359
360
Christopher Faulet51dbc942018-09-13 09:05:15 +0200361/*****************************************************/
362/* functions below are for dynamic buffer management */
363/*****************************************************/
364/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100365 * Indicates whether or not we may receive data. The rules are the following :
366 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200367 * must not attempt to receive
368 * - if we are waiting for the connection establishment, we must not attempt
369 * to receive
370 * - if an error was detected on the stream we must not attempt to receive
371 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100372 * - if the input buffer failed to be allocated or is full , we must not try
373 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200374 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200375 * - otherwise must may not attempt to receive
376 */
377static inline int h1_recv_allowed(const struct h1c *h1c)
378{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100379 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100380 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 +0200381 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200382 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200383
Willy Tarreau2febb842020-07-31 09:15:43 +0200384 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
385 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 +0100386 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200387 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100388
Christopher Faulet119ac872020-09-30 17:33:22 +0200389 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
390 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
391 return 0;
392 }
393
Christopher Fauletb385b502021-01-13 18:47:57 +0100394 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
395 TRACE_DEVEL("recv not allowed (wait_output)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200396 return 0;
397 }
398
Christopher Fauletd17ad822020-09-24 15:14:29 +0200399 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200400 return 1;
401
Christopher Faulet6b81df72019-10-01 22:08:43 +0200402 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 +0200403 return 0;
404}
405
406/*
407 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
408 * flags are used to figure what buffer was requested. It returns 1 if the
409 * allocation succeeds, in which case the connection is woken up, or 0 if it's
410 * impossible to wake up and we prefer to be woken up later.
411 */
412static int h1_buf_available(void *target)
413{
414 struct h1c *h1c = target;
415
416 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200417 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 +0200418 h1c->flags &= ~H1C_F_IN_ALLOC;
419 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200420 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200421 return 1;
422 }
423
424 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200425 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 +0200426 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200427 if (h1c->h1s)
428 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200429 return 1;
430 }
431
Christopher Fauletd17ad822020-09-24 15:14:29 +0200432 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
433 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
434 h1c->flags &= ~H1C_F_IN_SALLOC;
435 tasklet_wakeup(h1c->wait_event.tasklet);
436 return 1;
437 }
438
Christopher Faulet51dbc942018-09-13 09:05:15 +0200439 return 0;
440}
441
442/*
443 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
444 */
445static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
446{
447 struct buffer *buf = NULL;
448
Willy Tarreau21046592020-02-26 10:39:36 +0100449 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200450 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
451 h1c->buf_wait.target = h1c;
452 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200453 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200454 }
455 return buf;
456}
457
458/*
459 * Release a buffer, if any, and try to wake up entities waiting in the buffer
460 * wait queue.
461 */
462static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
463{
464 if (bptr->size) {
465 b_free(bptr);
466 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
467 }
468}
469
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100470/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100471 * or not. We rely on H1C_F_ST_IDLE to know if the connection is in-use or
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100472 * not. This flag is only set when no H1S is attached and when the previous
473 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100474 */
475static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200476{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100477 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200478
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100479 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200480}
481
Willy Tarreau00f18a32019-01-26 12:19:01 +0100482/* returns the number of streams still available on a connection */
483static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100484{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100485 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100486}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200487
Christopher Faulet7a145d62020-08-05 11:31:16 +0200488/* Refresh the h1c task timeout if necessary */
489static void h1_refresh_timeout(struct h1c *h1c)
490{
491 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100492 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200493 /* half-closed or dead connections : switch to clientfin/serverfin
494 * timeouts so that we don't hang too long on clients that have
495 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200496 */
497 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200498 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
499 }
500 else if (b_data(&h1c->obuf)) {
501 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200502 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200503 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
504 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100505 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
506 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200507 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100508 TRACE_DEVEL("refreshing connection's timeout (alive front h1c but not ready)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200509 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200510 else {
511 /* alive back connections of front connections with a conn-stream attached */
512 h1c->task->expire = TICK_ETERNITY;
513 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
514 }
515
Christopher Fauletdbe57792020-10-05 17:50:58 +0200516 /* Finally set the idle expiration date if shorter */
517 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200518 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
519 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200520 }
521}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200522
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200523static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200524{
525 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
526 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
527 h1c->idle_exp = TICK_ETERNITY;
528 return;
529 }
530
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100531 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200532 if (!tick_isset(h1c->idle_exp)) {
533 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
534 !b_data(&h1c->ibuf) && /* No input data */
535 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
536 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
537 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
538 }
539 else {
540 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
541 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
542 }
543 }
544 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100545 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200546 if (!tick_isset(h1c->idle_exp)) {
547 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
548 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
549 }
550 }
551 else { // CS_ATTACHED or SHUTDOWN
552 h1c->idle_exp = TICK_ETERNITY;
553 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
554 }
555}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200556/*****************************************************************/
557/* functions below are dedicated to the mux setup and management */
558/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200559
560/* returns non-zero if there are input data pending for stream h1s. */
561static inline size_t h1s_data_pending(const struct h1s *h1s)
562{
563 const struct h1m *h1m;
564
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200565 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100566 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200567}
568
Christopher Faulet16df1782020-12-04 16:47:41 +0100569/* Creates a new conn-stream and the associate stream. <input> is used as input
570 * buffer for the stream. On success, it is transferred to the stream and the
571 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
572 * mux must still take care of it. However, there is nothing special to do
573 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
574 * b_free() on it is always safe. This function returns the conn-stream on
575 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200576static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100577{
578 struct conn_stream *cs;
579
Christopher Faulet6b81df72019-10-01 22:08:43 +0200580 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200581 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200582 if (!cs) {
583 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 +0100584 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200585 }
Christopher Faulet47365272018-10-31 17:40:50 +0100586 h1s->cs = cs;
587 cs->ctx = h1s;
588
589 if (h1s->flags & H1S_F_NOT_FIRST)
590 cs->flags |= CS_FL_NOT_FIRST;
591
Christopher Faulet27182292020-07-03 15:08:49 +0200592 if (global.tune.options & GTUNE_USE_SPLICE) {
593 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100594 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200595 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100596
Christopher Faulet26256f82020-09-14 11:40:13 +0200597 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200598 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 +0100599 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200600 }
601
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100602 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200603 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100604 return cs;
605
606 err:
607 cs_free(cs);
608 h1s->cs = NULL;
609 return NULL;
610}
611
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100612static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
613{
614 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
615
616 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
617 TRACE_DEVEL("leaving on stream upgrade failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
618 goto err;
619 }
620
621 if (global.tune.options & GTUNE_USE_SPLICE) {
622 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
623 h1s->cs->flags |= CS_FL_MAY_SPLICE;
624 }
625
626 h1s->h1c->flags |= H1C_F_ST_READY;
627 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
628 return h1s->cs;
629
630 err:
631 return NULL;
632}
633
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200634static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200635{
636 struct h1s *h1s;
637
Christopher Faulet6b81df72019-10-01 22:08:43 +0200638 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
639
Christopher Faulet51dbc942018-09-13 09:05:15 +0200640 h1s = pool_alloc(pool_head_h1s);
641 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100642 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200643 h1s->h1c = h1c;
644 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200645 h1s->sess = NULL;
646 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100647 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100648 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200649 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200650
651 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100652 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200653
Christopher Faulet129817b2018-09-20 16:14:40 +0200654 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100655 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200656
657 h1s->status = 0;
658 h1s->meth = HTTP_METH_OTHER;
659
Christopher Faulet47365272018-10-31 17:40:50 +0100660 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
661 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100662 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100663
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200664 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
665 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200666
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200667 fail:
668 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
669 return NULL;
670}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100671
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200672static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
673{
674 struct session *sess = h1c->conn->owner;
675 struct h1s *h1s;
676
677 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
678
679 h1s = h1s_new(h1c);
680 if (!h1s)
681 goto fail;
682
683 h1s->sess = sess;
684
685 if (h1c->px->options2 & PR_O2_REQBUG_OK)
686 h1s->req.err_pos = -1;
687
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200688 h1c->idle_exp = TICK_ETERNITY;
689 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200690 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200691 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100692
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200693 fail:
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200694 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
695 return NULL;
696}
697
698static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
699{
700 struct h1s *h1s;
701
702 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
703
704 h1s = h1s_new(h1c);
705 if (!h1s)
706 goto fail;
707
708 h1s->cs = cs;
709 h1s->sess = sess;
710 cs->ctx = h1s;
711
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100712 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200713
714 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
715 h1s->res.err_pos = -1;
716
717 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
718 return h1s;
719
720 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200721 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 +0100722 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200723}
724
725static void h1s_destroy(struct h1s *h1s)
726{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200727 if (h1s) {
728 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200729
Christopher Faulet6b81df72019-10-01 22:08:43 +0200730 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200731 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200732
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100733 if (h1s->subs)
734 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200735
Christopher Fauletd17ad822020-09-24 15:14:29 +0200736 h1_release_buf(h1c, &h1s->rxbuf);
737
Christopher Fauletb385b502021-01-13 18:47:57 +0100738 h1c->flags &= ~(H1C_F_WAIT_INPUT|H1C_F_WAIT_OUTPUT|H1C_F_WANT_SPLICE|
739 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200740 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
741 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200742 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100743 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200744 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
745 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100746
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100747 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100748 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100749 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100750 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100751 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100752 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
753 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200754 else {
755 TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100756 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200757 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200758 pool_free(pool_head_h1s, h1s);
759 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200760}
761
762/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200763 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500764 * to the existing conn_stream (for outgoing connections or for incoming ones
765 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200766 * establishment). <input> is always used as Input buffer and may contain
767 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
768 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200769 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200770static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
771 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200772{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200773 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100774 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200775 void *conn_ctx = conn->ctx;
776
777 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200778
779 h1c = pool_alloc(pool_head_h1c);
780 if (!h1c)
781 goto fail_h1c;
782 h1c->conn = conn;
783 h1c->px = proxy;
784
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100785 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200786 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200787 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200788 h1c->obuf = BUF_NULL;
789 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200790 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200791
Willy Tarreau21046592020-02-26 10:39:36 +0100792 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200793 h1c->wait_event.tasklet = tasklet_new();
794 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200795 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200796 h1c->wait_event.tasklet->process = h1_io_cb;
797 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100798 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200799 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200800
Christopher Faulete9b70722019-04-08 10:46:02 +0200801 if (conn_is_back(conn)) {
Christopher Fauletb385b502021-01-13 18:47:57 +0100802 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OUTPUT);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100803 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
804 if (tick_isset(proxy->timeout.serverfin))
805 h1c->shut_timeout = proxy->timeout.serverfin;
806 } else {
807 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
808 if (tick_isset(proxy->timeout.clientfin))
809 h1c->shut_timeout = proxy->timeout.clientfin;
810 }
811 if (tick_isset(h1c->timeout)) {
812 t = task_new(tid_bit);
813 if (!t)
814 goto fail;
815
816 h1c->task = t;
817 t->process = h1_timeout_task;
818 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200819
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100820 t->expire = tick_add(now_ms, h1c->timeout);
821 }
822
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100823 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200824
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200825 if (h1c->flags & H1C_F_IS_BACK) {
826 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200827 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
828 goto fail;
829 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100830 else if (conn_ctx) {
831 /* Upgraded frontend connection (from TCP) */
832 struct conn_stream *cs = conn_ctx;
833
834 if (!h1c_frt_stream_new(h1c))
835 goto fail;
836
837 h1c->h1s->cs = cs;
838 cs->ctx = h1c->h1s;
839
840 /* Attach the CS but Not ready yet */
841 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
842 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
843 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
844 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100845
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200846 if (t) {
847 h1_set_idle_expiration(h1c);
848 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100849 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200850 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100851
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200852 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100853 if (b_data(&h1c->ibuf))
854 tasklet_wakeup(h1c->wait_event.tasklet);
855 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200856 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200857
858 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200859 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200860 return 0;
861
862 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200863 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200864 if (h1c->wait_event.tasklet)
865 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200866 pool_free(pool_head_h1c, h1c);
867 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200868 conn->ctx = conn_ctx; // restore saved context
869 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200870 return -1;
871}
872
Christopher Faulet73c12072019-04-08 11:23:22 +0200873/* release function. This one should be called to free all resources allocated
874 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200875 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200876static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200877{
Christopher Faulet61840e72019-04-15 09:33:32 +0200878 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200879
Christopher Faulet6b81df72019-10-01 22:08:43 +0200880 TRACE_POINT(H1_EV_H1C_END);
881
Christopher Faulet51dbc942018-09-13 09:05:15 +0200882 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200883 /* The connection must be aattached to this mux to be released */
884 if (h1c->conn && h1c->conn->ctx == h1c)
885 conn = h1c->conn;
886
Christopher Faulet6b81df72019-10-01 22:08:43 +0200887 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
888
Christopher Faulet61840e72019-04-15 09:33:32 +0200889 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200890 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200891 /* Make sure we're no longer subscribed to anything */
892 if (h1c->wait_event.events)
893 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
894 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200895 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200896 /* connection successfully upgraded to H2, this
897 * mux was already released */
898 return;
899 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200900 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200901 sess_log(conn->owner); /* Log if the upgrade failed */
902 }
903
Olivier Houchard45c44372019-06-11 14:06:23 +0200904
Willy Tarreau21046592020-02-26 10:39:36 +0100905 if (MT_LIST_ADDED(&h1c->buf_wait.list))
906 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200907
908 h1_release_buf(h1c, &h1c->ibuf);
909 h1_release_buf(h1c, &h1c->obuf);
910
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100911 if (h1c->task) {
912 h1c->task->context = NULL;
913 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
914 h1c->task = NULL;
915 }
916
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200917 if (h1c->wait_event.tasklet)
918 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200919
Christopher Fauletf2824e62018-10-01 12:12:37 +0200920 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200921 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100922 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200923 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200924 pool_free(pool_head_h1c, h1c);
925 }
926
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200927 if (conn) {
928 conn->mux = NULL;
929 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200930 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200931
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200932 conn_stop_tracking(conn);
933 conn_full_close(conn);
934 if (conn->destroy_cb)
935 conn->destroy_cb(conn);
936 conn_free(conn);
937 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200938}
939
940/******************************************************/
941/* functions below are for the H1 protocol processing */
942/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200943/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
944 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200945 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100946static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200947{
Christopher Faulet570d1612018-11-26 11:13:57 +0100948 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200949
Christopher Faulet570d1612018-11-26 11:13:57 +0100950 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200951 (*(p + 5) > '1' ||
952 (*(p + 5) == '1' && *(p + 7) >= '1')))
953 h1m->flags |= H1_MF_VER_11;
954}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200955
Christopher Faulet9768c262018-10-22 09:34:31 +0200956/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
957 * greater or equal to 1.1
958 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100959static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200960{
Christopher Faulet570d1612018-11-26 11:13:57 +0100961 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200962
Christopher Faulet570d1612018-11-26 11:13:57 +0100963 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200964 (*(p + 5) > '1' ||
965 (*(p + 5) == '1' && *(p + 7) >= '1')))
966 h1m->flags |= H1_MF_VER_11;
967}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200968
Christopher Fauletf2824e62018-10-01 12:12:37 +0200969/* Deduce the connection mode of the client connection, depending on the
970 * configuration and the H1 message flags. This function is called twice, the
971 * first time when the request is parsed and the second time when the response
972 * is parsed.
973 */
974static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
975{
976 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200977
978 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100979 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100980 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100981 h1s->status == 101) {
982 /* Either we've established an explicit tunnel, or we're
983 * switching the protocol. In both cases, we're very unlikely to
984 * understand the next protocols. We have to switch to tunnel
985 * mode, so that we transfer the request and responses then let
986 * this protocol pass unmodified. When we later implement
987 * specific parsers for such protocols, we'll want to check the
988 * Upgrade header which contains information about that protocol
989 * for responses with status 101 (eg: see RFC2817 about TLS).
990 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200991 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200992 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 +0100993 }
994 else if (h1s->flags & H1S_F_WANT_KAL) {
995 /* By default the client is in KAL mode. CLOSE mode mean
996 * it is imposed by the client itself. So only change
997 * KAL mode here. */
998 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
999 /* no length known or explicit close => close */
1000 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001001 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 +01001002 }
1003 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1004 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001005 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001006 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001007 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 +01001008 }
1009 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001010 }
1011 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001012 /* Input direction: first pass */
1013 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1014 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001015 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001016 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 +01001017 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001018 }
1019
1020 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001021 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001022 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001023 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);
1024 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001025}
1026
1027/* Deduce the connection mode of the client connection, depending on the
1028 * configuration and the H1 message flags. This function is called twice, the
1029 * first time when the request is parsed and the second time when the response
1030 * is parsed.
1031 */
1032static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1033{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001034 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001035 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001036 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001037
Christopher Fauletf2824e62018-10-01 12:12:37 +02001038 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001039 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001040 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001041 h1s->status == 101) {
1042 /* Either we've established an explicit tunnel, or we're
1043 * switching the protocol. In both cases, we're very unlikely to
1044 * understand the next protocols. We have to switch to tunnel
1045 * mode, so that we transfer the request and responses then let
1046 * this protocol pass unmodified. When we later implement
1047 * specific parsers for such protocols, we'll want to check the
1048 * Upgrade header which contains information about that protocol
1049 * for responses with status 101 (eg: see RFC2817 about TLS).
1050 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001051 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001052 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 +01001053 }
1054 else if (h1s->flags & H1S_F_WANT_KAL) {
1055 /* By default the server is in KAL mode. CLOSE mode mean
1056 * it is imposed by haproxy itself. So only change KAL
1057 * mode here. */
1058 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1059 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1060 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1061 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001062 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 +01001063 }
1064 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001065 }
Christopher Faulet70033782018-12-05 13:50:11 +01001066 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001067 /* Output direction: first pass */
1068 if (h1m->flags & H1_MF_CONN_CLO) {
1069 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001070 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001071 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 +01001072 }
1073 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1074 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1075 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1076 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1077 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1078 /* no explicit keep-alive option httpclose/server-close => close */
1079 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001080 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 +01001081 }
Christopher Faulet70033782018-12-05 13:50:11 +01001082 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001083
1084 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001085 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001086 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001087 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);
1088 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001089}
1090
Christopher Fauletb992af02019-03-28 15:42:24 +01001091static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001092{
1093 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001094
1095 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1096 * token is found
1097 */
1098 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001100
1101 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001102 if (!(h1m->flags & H1_MF_VER_11)) {
1103 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 +01001104 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001105 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001106 }
1107 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001108 if (h1m->flags & H1_MF_VER_11) {
1109 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001110 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001111 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001112 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113}
1114
Christopher Fauletb992af02019-03-28 15:42:24 +01001115static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001117 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1118 * token is found
1119 */
1120 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001121 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001122
1123 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001124 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001125 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1126 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 +01001127 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001128 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001129 }
1130 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001131 if (h1m->flags & H1_MF_VER_11) {
1132 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001133 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001134 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001135 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001136}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001137
Christopher Fauletb992af02019-03-28 15:42:24 +01001138static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001139{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001140 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001141 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001142 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001143 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001144}
1145
Christopher Fauletb992af02019-03-28 15:42:24 +01001146static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1147{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001148 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001149 h1_set_cli_conn_mode(h1s, h1m);
1150 else
1151 h1_set_srv_conn_mode(h1s, h1m);
1152
1153 if (!(h1m->flags & H1_MF_RESP))
1154 h1_update_req_conn_value(h1s, h1m, conn_val);
1155 else
1156 h1_update_res_conn_value(h1s, h1m, conn_val);
1157}
Christopher Faulete44769b2018-11-29 23:01:45 +01001158
Christopher Faulet98fbe952019-07-22 16:18:24 +02001159/* Try to adjust the case of the message header name using the global map
1160 * <hdrs_map>.
1161 */
1162static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1163{
1164 struct ebpt_node *node;
1165 struct h1_hdr_entry *entry;
1166
1167 /* No entry in the map, do nothing */
1168 if (eb_is_empty(&hdrs_map.map))
1169 return;
1170
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001171 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001172 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1173 return;
1174
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001175 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001176 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1177 return;
1178
1179 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1180 if (!node)
1181 return;
1182 entry = container_of(node, struct h1_hdr_entry, node);
1183 name->ptr = entry->name.ptr;
1184 name->len = entry->name.len;
1185}
1186
Christopher Faulete44769b2018-11-29 23:01:45 +01001187/* Append the description of what is present in error snapshot <es> into <out>.
1188 * The description must be small enough to always fit in a buffer. The output
1189 * buffer may be the trash so the trash must not be used inside this function.
1190 */
1191static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1192{
1193 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001194 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1195 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1196 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1197 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1198 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1199 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001200}
1201/*
1202 * Capture a bad request or response and archive it in the proxy's structure.
1203 * By default it tries to report the error position as h1m->err_pos. However if
1204 * this one is not set, it will then report h1m->next, which is the last known
1205 * parsing point. The function is able to deal with wrapping buffers. It always
1206 * displays buffers as a contiguous area starting at buf->p. The direction is
1207 * determined thanks to the h1m's flags.
1208 */
1209static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1210 struct h1m *h1m, struct buffer *buf)
1211{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001212 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001213 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001214 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001215 union error_snapshot_ctx ctx;
1216
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001217 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001218 if (sess == NULL)
1219 sess = si_strm(h1s->cs->data)->sess;
1220 if (!(h1m->flags & H1_MF_RESP))
1221 other_end = si_strm(h1s->cs->data)->be;
1222 else
1223 other_end = sess->fe;
1224 } else
1225 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001226
1227 /* http-specific part now */
1228 ctx.h1.state = h1m->state;
1229 ctx.h1.c_flags = h1c->flags;
1230 ctx.h1.s_flags = h1s->flags;
1231 ctx.h1.m_flags = h1m->flags;
1232 ctx.h1.m_clen = h1m->curr_len;
1233 ctx.h1.m_blen = h1m->body_len;
1234
1235 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1236 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001237 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1238 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001239}
1240
Christopher Fauletadb22202018-12-12 10:32:09 +01001241/* Emit the chunksize followed by a CRLF in front of data of the buffer
1242 * <buf>. It goes backwards and starts with the byte before the buffer's
1243 * head. The caller is responsible for ensuring there is enough room left before
1244 * the buffer's head for the string.
1245 */
1246static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1247{
1248 char *beg, *end;
1249
1250 beg = end = b_head(buf);
1251 *--beg = '\n';
1252 *--beg = '\r';
1253 do {
1254 *--beg = hextab[chksz & 0xF];
1255 } while (chksz >>= 4);
1256 buf->head -= (end - beg);
1257 b_add(buf, end - beg);
1258}
1259
1260/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1261 * ensuring there is enough room left in the buffer for the string. */
1262static void h1_emit_chunk_crlf(struct buffer *buf)
1263{
1264 *(b_peek(buf, b_data(buf))) = '\r';
1265 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1266 b_add(buf, 2);
1267}
1268
Christopher Faulet129817b2018-09-20 16:14:40 +02001269/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001270 * Switch the stream to tunnel mode. This function must only be called on 2xx
1271 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001272 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001273static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001274{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001275 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001276
Christopher Faulet5be651d2021-01-22 15:28:03 +01001277 h1s->req.state = H1_MSG_TUNNEL;
1278 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001279
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001280 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001281 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001282
Christopher Faulet5be651d2021-01-22 15:28:03 +01001283 TRACE_STATE("switch H1 stream in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01001284
Christopher Faulet5be651d2021-01-22 15:28:03 +01001285 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1286 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1287 if (b_data(&h1c->ibuf))
1288 h1_wake_stream_for_recv(h1s);
1289 tasklet_wakeup(h1c->wait_event.tasklet);
1290 TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001291 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01001292 if (h1c->flags & H1C_F_WAIT_INPUT) {
1293 h1c->flags &= ~H1C_F_WAIT_INPUT;
1294 h1_wake_stream_for_send(h1s);
1295 if (b_data(&h1c->obuf))
1296 tasklet_wakeup(h1c->wait_event.tasklet);
1297 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001298 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001299}
1300
1301/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001302 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001303 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001304 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001305 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001306static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001307 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001308{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001309 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001310 int ret = 0;
1311
Willy Tarreau022e5e52020-09-10 09:33:15 +02001312 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 +02001313
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001314 if (h1s->meth == HTTP_METH_CONNECT)
1315 h1m->flags |= H1_MF_METH_CONNECT;
1316 if (h1s->meth == HTTP_METH_HEAD)
1317 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001318
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001319 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1320 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001321 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 +02001322 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001323 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001324 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001325 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1326 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001327 goto end;
1328 }
1329
Christopher Faulet486498c2019-10-11 14:22:00 +02001330 if (h1m->err_pos >= 0) {
1331 /* Maybe we found an error during the parsing while we were
1332 * configured not to block on that, so we have to capture it
1333 * now.
1334 */
1335 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1336 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1337 }
1338
Christopher Faulet5696f542020-12-02 16:08:38 +01001339 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001340 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001341 if (h1s->meth == HTTP_METH_HEAD)
1342 h1s->flags |= H1S_F_BODYLESS_RESP;
1343 }
1344 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001345 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001346 if (h1s->status == 204 || h1s->status == 304)
1347 h1s->flags |= H1S_F_BODYLESS_RESP;
1348 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001349 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001350 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001351
Christopher Faulet129817b2018-09-20 16:14:40 +02001352 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001353 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 +02001354 return ret;
1355}
1356
1357/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001358 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001359 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1360 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001361 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001362static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001363 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001364 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001365{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001366 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001367
Willy Tarreau022e5e52020-09-10 09:33:15 +02001368 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 +02001369 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001370 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001371 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 +01001372 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001373 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001374 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001375 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001376 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001377 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001378 }
1379
Christopher Faulet02a02532019-11-15 09:36:28 +01001380 *ofs += ret;
1381
1382 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001383 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 +02001384 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001385}
1386
1387/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001388 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1389 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1390 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1391 * responsible to update the parser state <h1m>.
1392 */
1393static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1394 struct buffer *buf, size_t *ofs, size_t max)
1395{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001396 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001397
Willy Tarreau022e5e52020-09-10 09:33:15 +02001398 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 +02001399 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001400 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001401 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 +01001402 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001403 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001404 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001405 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1406 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001407 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001408 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001409
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001410 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001411
1412 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001413 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 +02001414 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001415}
1416
1417/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001418 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001419 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1420 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001421 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001422static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001423{
Christopher Faulet539e0292018-11-19 10:40:09 +01001424 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001425 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001426 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001427 size_t data;
1428 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001429 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001430
Christopher Faulet539e0292018-11-19 10:40:09 +01001431 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001432 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001433
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001434 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001435 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001436
Christopher Faulet2eed8002020-12-07 11:26:13 +01001437 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001438 goto end;
1439
Christopher Faulet5be651d2021-01-22 15:28:03 +01001440 if (h1c->flags & H1C_F_WAIT_OUTPUT)
1441 goto end;
1442
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001443 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001444 size_t used = htx_used_space(htx);
1445
Christopher Faulet129817b2018-09-20 16:14:40 +02001446 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001447 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001448 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001449 if (!ret)
1450 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001451
1452 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1453 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1454
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001455 /* Reject Protocol upgrade request with payload */
1456 if ((h1m->flags & (H1_MF_RESP|H1_MF_CONN_UPG)) == H1_MF_CONN_UPG && h1m->state != H1_MSG_DONE) {
1457 h1s->flags |= H1S_F_NOT_IMPL_ERROR;
1458 TRACE_USER("Upgrade with body not implemented, reject H1 message",
1459 H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1460 break;
1461 }
1462
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001463 if ((h1m->flags & H1_MF_RESP) &&
1464 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1465 h1m_init_res(&h1s->res);
1466 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001467 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001468 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001469 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001470 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001471 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001472 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001473 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001474 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001475
1476 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1477 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001478 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001479 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001480 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1481 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1482 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001483 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001484
Christopher Faulet76014fd2019-12-10 11:47:22 +01001485 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1486 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001487 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001488 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001489 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1490 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001491
Christopher Faulet5be651d2021-01-22 15:28:03 +01001492 if ((h1m->flags & H1_MF_RESP) &&
1493 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1494 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001495 else {
1496 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1497 /* Unfinished transaction: block this input side waiting the end of the output side */
1498 h1c->flags |= H1C_F_WAIT_OUTPUT;
1499 TRACE_STATE("Disable read on h1c (wait_output)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1500 }
1501 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1502 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1503 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1504 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1505 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001506 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001507 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001508 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001509 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001510 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001511 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001512 if (!ret)
1513 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001514
1515 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1516 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001517 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001518 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001519 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001520 break;
1521 }
1522
Christopher Faulet30db3d72019-05-17 15:35:33 +02001523 count -= htx_used_space(htx) - used;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001524 } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) && !(h1c->flags & H1C_F_WAIT_OUTPUT));
1525
Christopher Faulet129817b2018-09-20 16:14:40 +02001526
Christopher Faulet2eed8002020-12-07 11:26:13 +01001527 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
1528 TRACE_PROTO("parsing or not-implemented error", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001529 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001530 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001531
Christopher Faulet539e0292018-11-19 10:40:09 +01001532 b_del(&h1c->ibuf, total);
1533
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001534 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001535 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1536
Christopher Faulet30db3d72019-05-17 15:35:33 +02001537 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001538 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001539 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001540 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001541 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001542 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001543
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001544 if (!b_data(&h1c->ibuf))
1545 h1_release_buf(h1c, &h1c->ibuf);
1546
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001547 if (!(h1c->flags & H1C_F_ST_READY)) {
1548 /* The H1 connection is not ready. Most of time, there is no CS
1549 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1550 * cases, it is only possible on the client side.
1551 */
1552 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1553
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001554 if (h1m->state <= H1_MSG_LAST_LF) {
1555 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1556 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1557 goto end;
1558 }
1559
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001560 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1561 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1562 BUG_ON(h1s->cs);
1563 if (!h1s_new_cs(h1s, buf)) {
1564 h1c->flags |= H1C_F_ST_ERROR;
1565 goto err;
1566 }
1567 }
1568 else {
1569 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1570 BUG_ON(h1s->cs == NULL);
1571 if (!h1s_upgrade_cs(h1s, buf)) {
1572 h1c->flags |= H1C_F_ST_ERROR;
1573 goto err;
1574 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001575 }
1576 }
1577
1578 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001579 if (!(h1m->flags & H1_MF_CHNK) &&
1580 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1581 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1582 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1583 }
1584 else {
1585 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1586 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1587 }
1588
Christopher Faulet5be651d2021-01-22 15:28:03 +01001589 /* Don't set EOI on the conn-stream for protocol upgrade or connect
1590 * requests, wait the response to do so or not depending on the status
1591 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001592 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001593 if ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG))
Christopher Fauleta583af62020-09-24 15:35:37 +02001594 h1s->cs->flags |= CS_FL_EOI;
1595
Christopher Faulet6716cc22019-12-20 17:33:24 +01001596 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001597 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001598 else {
1599 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1600 if (h1s->flags & H1S_F_REOS) {
1601 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001602 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1603 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1604 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001605 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001606 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001607 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
1608 h1s->cs->flags |= CS_FL_ERROR;
Christopher Fauletb385b502021-01-13 18:47:57 +01001609
1610 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1611 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1612 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1613 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1614 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001615 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001616 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001617
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001618 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001619 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001620 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001621
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001622 err:
Christopher Faulet47365272018-10-31 17:40:50 +01001623 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001624 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001625 if (h1s->cs)
1626 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001627 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001628 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001629}
1630
Christopher Faulet129817b2018-09-20 16:14:40 +02001631/*
1632 * Process outgoing data. It parses data and transfer them from the channel buffer into
1633 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1634 * 0 if it couldn't proceed.
1635 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001636static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1637{
Christopher Faulet129817b2018-09-20 16:14:40 +02001638 struct h1s *h1s = h1c->h1s;
1639 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001640 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001641 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001642 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001643 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001644 int last_data = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001645
Christopher Faulet47365272018-10-31 17:40:50 +01001646 if (!count)
1647 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001648
Christopher Faulet94b2c762019-05-24 15:28:57 +02001649 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001650 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1651
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001652 if (htx_is_empty(chn_htx))
1653 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001654
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001655 if (h1s->flags & H1S_F_PROCESSING_ERROR)
1656 goto end;
1657
Christopher Fauletdea24742021-01-22 15:12:30 +01001658 if (h1c->flags & H1C_F_WAIT_INPUT)
1659 goto end;
1660
Christopher Faulet51dbc942018-09-13 09:05:15 +02001661 if (!h1_get_buf(h1c, &h1c->obuf)) {
1662 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001663 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 +02001664 goto end;
1665 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001666
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001667 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001668
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001669 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001670 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001671
Willy Tarreau3815b222018-12-11 19:50:43 +01001672 /* Perform some optimizations to reduce the number of buffer copies.
1673 * First, if the mux's buffer is empty and the htx area contains
1674 * exactly one data block of the same size as the requested count,
1675 * then it's possible to simply swap the caller's buffer with the
1676 * mux's output buffer and adjust offsets and length to match the
1677 * entire DATA HTX block in the middle. In this case we perform a
1678 * true zero-copy operation from end-to-end. This is the situation
1679 * that happens all the time with large files. Second, if this is not
1680 * possible, but the mux's output buffer is empty, we still have an
1681 * opportunity to avoid the copy to the intermediary buffer, by making
1682 * the intermediary buffer's area point to the output buffer's area.
1683 * In this case we want to skip the HTX header to make sure that copies
1684 * remain aligned and that this operation remains possible all the
1685 * time. This goes for headers, data blocks and any data extracted from
1686 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001687 */
1688 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001689 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
1690 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001691 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001692 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001693 void *old_area;
1694
1695 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
1696 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
1697 goto skip_zero_copy;
1698 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001699
Christopher Faulet6b81df72019-10-01 22:08:43 +02001700 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001701 if (h1m->state == H1_MSG_DATA && chn_htx->flags & HTX_FL_EOM) {
1702 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1703 last_data = 1;
1704 }
1705
Christopher Faulete5596bf2020-12-02 16:13:22 +01001706 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01001707 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001708 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001709 h1c->obuf.data = count;
1710
1711 buf->area = old_area;
1712 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001713
Christopher Faulet6b81df72019-10-01 22:08:43 +02001714 chn_htx = (struct htx *)buf->area;
1715 htx_reset(chn_htx);
1716
Christopher Fauletadb22202018-12-12 10:32:09 +01001717 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001718 * size and eventually the last chunk. We have at least
1719 * the size of the struct htx to write the chunk
1720 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01001721 */
1722 if (h1m->flags & H1_MF_CHNK) {
1723 h1_emit_chunk_size(&h1c->obuf, count);
1724 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001725 if (last_data) {
1726 /* Emit the last chunk too at the buffer's end */
1727 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
1728 }
Christopher Fauletadb22202018-12-12 10:32:09 +01001729 }
1730
Christopher Faulet6b81df72019-10-01 22:08:43 +02001731 if (h1m->state == H1_MSG_DATA)
1732 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001733 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001734 else
1735 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001736 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001737
Christopher Faulete5596bf2020-12-02 16:13:22 +01001738 skip_zero_copy:
1739 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001740 if (last_data) {
1741 h1m->state = H1_MSG_DONE;
1742 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
1743 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1744 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1745 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1746 }
1747
1748 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1749 H1_EV_TX_DATA, h1c->conn, h1s);
1750 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001751 goto out;
1752 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001753 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001754 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001755 else
1756 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001757
Christopher Fauletc2518a52019-06-25 21:41:02 +02001758 tmp.data = 0;
1759 tmp.size = b_room(&h1c->obuf);
Christopher Fauletdea24742021-01-22 15:12:30 +01001760 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && !(h1c->flags & H1C_F_WAIT_INPUT) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001761 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001762 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001763 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001764 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001765 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001766
Christopher Fauletb2e84162018-12-06 11:39:49 +01001767 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001768 if (type != HTX_BLK_DATA && vlen > count)
1769 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001770
Christopher Faulet94b2c762019-05-24 15:28:57 +02001771 if (type == HTX_BLK_UNUSED)
1772 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001773
Christopher Faulet94b2c762019-05-24 15:28:57 +02001774 switch (h1m->state) {
1775 case H1_MSG_RQBEFORE:
1776 if (type != HTX_BLK_REQ_SL)
1777 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001778 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 +02001779 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001780 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001781 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001782 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001783 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001784 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001785 if (sl->flags & HTX_SL_F_BODYLESS)
1786 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001787 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01001788 if (h1s->meth == HTTP_METH_HEAD)
1789 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Fauletb385b502021-01-13 18:47:57 +01001790 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1791 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Christopher Faulet089acd52020-09-21 10:57:52 +02001792 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1793 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1794 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001795 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001796
Christopher Faulet94b2c762019-05-24 15:28:57 +02001797 case H1_MSG_RPBEFORE:
1798 if (type != HTX_BLK_RES_SL)
1799 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001800 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 +02001801 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001802 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001803 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001804 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001805 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001806 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001807 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001808 if (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001809 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001810 else if (h1s->status == 204 || h1s->status == 304)
1811 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02001812 h1m->state = H1_MSG_HDR_FIRST;
1813 break;
1814
Christopher Faulet94b2c762019-05-24 15:28:57 +02001815 case H1_MSG_HDR_FIRST:
1816 case H1_MSG_HDR_NAME:
1817 case H1_MSG_HDR_L2_LWS:
1818 if (type == HTX_BLK_EOH)
1819 goto last_lf;
1820 if (type != HTX_BLK_HDR)
1821 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001822
Christopher Faulet9768c262018-10-22 09:34:31 +02001823 h1m->state = H1_MSG_HDR_NAME;
1824 n = htx_get_blk_name(chn_htx, blk);
1825 v = htx_get_blk_value(chn_htx, blk);
1826
Christopher Faulet86d144c2019-08-14 16:32:25 +02001827 /* Skip all pseudo-headers */
1828 if (*(n.ptr) == ':')
1829 goto skip_hdr;
1830
Christopher Faulet91fcf212020-12-02 16:17:15 +01001831 if (isteq(n, ist("transfer-encoding"))) {
1832 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
1833 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02001834 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01001835 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001836 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01001837 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
1838 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01001839 /* Only skip C-L header with invalid value. */
1840 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001841 goto skip_hdr;
1842 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001843 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001844 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001845 if (!v.len)
1846 goto skip_hdr;
1847 }
1848
Christopher Faulet67d58092019-10-02 10:51:38 +02001849 /* Skip header if same name is used to add the server name */
1850 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1851 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1852 goto skip_hdr;
1853
Christopher Faulet98fbe952019-07-22 16:18:24 +02001854 /* Try to adjust the case of the header name */
1855 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1856 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001857 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001858 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001859 skip_hdr:
1860 h1m->state = H1_MSG_HDR_L2_LWS;
1861 break;
1862
Christopher Faulet94b2c762019-05-24 15:28:57 +02001863 case H1_MSG_LAST_LF:
1864 if (type != HTX_BLK_EOH)
1865 goto error;
1866 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001867 h1m->state = H1_MSG_LAST_LF;
1868 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001869 /* If the reply comes from haproxy while the request is
1870 * not finished, we force the connection close. */
1871 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1872 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1873 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1874 }
1875
1876 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001877 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001878 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001879 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001880 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001881 /* Try to adjust the case of the header name */
1882 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1883 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001884 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001885 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001886 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001887 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001888 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001889
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001890 if ((h1s->meth != HTTP_METH_CONNECT &&
1891 (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 +01001892 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01001893 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001894 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001895 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1896 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001897 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001898 n = ist("transfer-encoding");
1899 v = ist("chunked");
1900 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1901 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001902 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001903 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001904 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001905 h1m->flags |= H1_MF_CHNK;
1906 }
1907
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001908 /* Now add the server name to a header (if requested) */
1909 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1910 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1911 struct server *srv = objt_server(h1c->conn->target);
1912
1913 if (srv) {
1914 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1915 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001916
1917 /* Try to adjust the case of the header name */
1918 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1919 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001920 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001921 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001922 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001923 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001924 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1925 }
1926
Christopher Faulet6b81df72019-10-01 22:08:43 +02001927 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1928 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1929
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001930 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001931 if (!chunk_memcat(&tmp, "\r\n", 2))
1932 goto full;
1933 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001934 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001935 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001936 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001937 if (!chunk_memcat(&tmp, "\r\n", 2))
1938 goto full;
1939 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001940 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001941 else if ((h1m->flags & H1_MF_RESP) &&
1942 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001943 if (!chunk_memcat(&tmp, "\r\n", 2))
1944 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001945 h1m_init_res(&h1s->res);
1946 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001947 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001948 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001949 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001950 else {
1951 /* EOM flag is set and it is the last block */
1952 if (htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
1953 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
1954 goto full;
1955 else if (!chunk_memcat(&tmp, "\r\n", 2))
1956 goto full;
1957 goto done;
1958 }
1959 else if (!chunk_memcat(&tmp, "\r\n", 2))
1960 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001961 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001962 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001963 break;
1964
Christopher Faulet94b2c762019-05-24 15:28:57 +02001965 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001966 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001967 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001968 /* If the message is not chunked, never
1969 * add the last chunk. */
1970 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001971 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001972 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 +02001973 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001974 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001975 else if (type != HTX_BLK_DATA)
1976 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001977
Christopher Faulete5596bf2020-12-02 16:13:22 +01001978 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
1979 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
1980 break;
1981 }
1982
Christopher Faulet6b81df72019-10-01 22:08:43 +02001983 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 +02001984
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001985 /* It is the last block of this message. After this one,
1986 * only tunneled data may be forwarded. */
1987 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
1988 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1989 last_data = 1;
1990 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02001991
1992 if (vlen > count) {
1993 /* Get the maximum amount of data we can xferred */
1994 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001995 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001996 }
1997
1998 chklen = 0;
1999 if (h1m->flags & H1_MF_CHNK) {
2000 chklen = b_room(&tmp);
2001 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2002 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2003 (chklen < 1048576) ? 5 : 8);
2004 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002005
2006 /* If it is the end of the chunked message (without EOT), reserve the
2007 * last chunk size */
2008 if (last_data)
2009 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002010 }
2011
2012 if (vlen + chklen > b_room(&tmp)) {
2013 /* too large for the buffer */
2014 if (chklen >= b_room(&tmp))
2015 goto full;
2016 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002017 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002018 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002019 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002020 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002021 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002022 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002023
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002024 /* Space already reserved, so it must succeed */
2025 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2026 goto error;
2027
Christopher Faulet6b81df72019-10-01 22:08:43 +02002028 if (h1m->state == H1_MSG_DATA)
2029 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002030 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002031 else
2032 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002033 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002034 if (last_data)
2035 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002036 break;
2037
Christopher Faulet94b2c762019-05-24 15:28:57 +02002038 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002039 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002040 goto error;
2041 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002042 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002043
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002044 /* If the message is not chunked, ignore
2045 * trailers. It may happen with H2 messages. */
2046 if (!(h1m->flags & H1_MF_CHNK))
2047 break;
2048
Christopher Faulete5596bf2020-12-02 16:13:22 +01002049 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2050 TRACE_PROTO("Skip trailers for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2051 break;
2052 }
2053
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002054 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002055 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002056 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002057 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2058 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002059 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002060 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002061 else { // HTX_BLK_TLR
2062 n = htx_get_blk_name(chn_htx, blk);
2063 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002064
2065 /* Try to adjust the case of the header name */
2066 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2067 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002068 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002069 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002070 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002071 break;
2072
Christopher Faulet94b2c762019-05-24 15:28:57 +02002073 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002074 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2075 goto error; /* For now return an error */
2076
Christopher Faulet94b2c762019-05-24 15:28:57 +02002077 done:
2078 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002079 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
2080 h1c->flags |= H1C_F_WAIT_INPUT;
2081 TRACE_STATE("Disable send on h1c (wait_input)", H1_EV_TX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
2082 }
2083 else if ((h1m->flags & H1_MF_RESP) &&
2084 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2085 /* a successful reply to a CONNECT or a protocol switching is sent
2086 * to the client. Switch the response to tunnel mode.
2087 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002088 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002089 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletf3158e92019-11-15 11:14:23 +01002090 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002091
2092 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
Christopher Fauletb385b502021-01-13 18:47:57 +01002093 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002094 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02002095 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02002096 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002097
2098 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2099 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002100 break;
2101
Christopher Faulet9768c262018-10-22 09:34:31 +02002102 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002103 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002104 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002105 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002106 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002107 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002108 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002109 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2110 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002111 break;
2112 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002113
2114 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002115 total += vlen;
2116 count -= vlen;
2117 if (sz == vlen)
2118 blk = htx_remove_blk(chn_htx, blk);
2119 else {
2120 htx_cut_data_blk(chn_htx, blk, vlen);
2121 break;
2122 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002123 }
2124
Christopher Faulet9768c262018-10-22 09:34:31 +02002125 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002126 /* when the output buffer is empty, tmp shares the same area so that we
2127 * only have to update pointers and lengths.
2128 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002129 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2130 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002131 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002132 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133
Willy Tarreau3815b222018-12-11 19:50:43 +01002134 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002135 out:
2136 if (!buf_room_for_htx_data(&h1c->obuf)) {
2137 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002138 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002139 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002140 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002141 /* Both the request and the response reached the DONE state. So set EOI
2142 * flag on the conn-stream. Most of time, the flag will already be set,
2143 * except for protocol upgrades. Report an error if data remains blocked
2144 * in the output buffer.
2145 */
2146 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2147 if (!htx_is_empty(chn_htx)) {
2148 h1c->flags |= H1C_F_ST_ERROR;
2149 TRACE_STATE("txn done but data waiting to be sent, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
2150 }
2151 h1s->cs->flags |= CS_FL_EOI;
2152 }
2153
Christopher Faulet6b81df72019-10-01 22:08:43 +02002154 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002155 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002156
2157 full:
2158 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2159 h1c->flags |= H1C_F_OUT_FULL;
2160 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002161}
2162
Christopher Faulet51dbc942018-09-13 09:05:15 +02002163/*********************************************************/
2164/* functions below are I/O callbacks from the connection */
2165/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002166static void h1_wake_stream_for_recv(struct h1s *h1s)
2167{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002168 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002169 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002170 tasklet_wakeup(h1s->subs->tasklet);
2171 h1s->subs->events &= ~SUB_RETRY_RECV;
2172 if (!h1s->subs->events)
2173 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002174 }
2175}
2176static void h1_wake_stream_for_send(struct h1s *h1s)
2177{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002178 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002179 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002180 tasklet_wakeup(h1s->subs->tasklet);
2181 h1s->subs->events &= ~SUB_RETRY_SEND;
2182 if (!h1s->subs->events)
2183 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002184 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002185}
2186
Christopher Fauletad4daf62021-01-21 17:49:01 +01002187/* alerts the data layer following this sequence :
2188 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2189 * - if its subscribed to send, then it's woken up for send
2190 * - if it was subscribed to neither, its ->wake() callback is called
2191 */
2192static void h1_alert(struct h1s *h1s)
2193{
2194 if (h1s->subs) {
2195 h1_wake_stream_for_recv(h1s);
2196 h1_wake_stream_for_send(h1s);
2197 }
2198 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2199 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2200 h1s->cs->data_cb->wake(h1s->cs);
2201 }
2202}
2203
Christopher Fauletc18fc232020-10-06 17:41:36 +02002204/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2205 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2206 * retryable errors (allocation error or buffer full). On success, the error is
2207 * copied in the output buffer.
2208*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002209static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002210{
2211 int rc = http_get_status_idx(h1c->errcode);
2212 int ret = 0;
2213
2214 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2215
2216 /* Verify if the error is mapped on /dev/null or any empty file */
2217 /// XXX: do a function !
2218 if (h1c->px->replies[rc] &&
2219 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2220 h1c->px->replies[rc]->body.errmsg &&
2221 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2222 /* Empty error, so claim a success */
2223 ret = 1;
2224 goto out;
2225 }
2226
2227 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2228 h1c->flags |= H1C_F_ERR_PENDING;
2229 goto out;
2230 }
2231
2232 if (!h1_get_buf(h1c, &h1c->obuf)) {
2233 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2234 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2235 goto out;
2236 }
2237 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2238 if (unlikely(ret <= 0)) {
2239 if (!ret) {
2240 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2241 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2242 goto out;
2243 }
2244 else {
2245 /* we cannot report this error, so claim a success */
2246 ret = 1;
2247 }
2248 }
2249 h1c->flags &= ~H1C_F_ERR_PENDING;
2250 out:
2251 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2252 return ret;
2253}
2254
2255/* Try to send a 500 internal error. It relies on h1_send_error to send the
2256 * error. This function takes care of incrementing stats and tracked counters.
2257 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002258static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002259{
2260 struct session *sess = h1c->conn->owner;
2261 int ret = 1;
2262
2263 session_inc_http_req_ctr(sess);
2264 session_inc_http_err_ctr(sess);
2265 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2266 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[5], 1);
2267 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
2268 if (sess->listener->counters)
2269 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
2270
2271 h1c->errcode = 500;
2272 ret = h1_send_error(h1c);
2273 sess_log(sess);
2274 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002275}
2276
Christopher Fauletc18fc232020-10-06 17:41:36 +02002277/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2278 * error. This function takes care of incrementing stats and tracked counters.
2279 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002280static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002281{
2282 struct session *sess = h1c->conn->owner;
2283 int ret = 1;
2284
2285 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2286 goto end;
2287
2288 session_inc_http_req_ctr(sess);
2289 session_inc_http_err_ctr(sess);
2290 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2291 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2292 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2293 if (sess->listener->counters)
2294 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2295
2296 h1c->errcode = 400;
2297 ret = h1_send_error(h1c);
2298 sess_log(sess);
2299
2300 end:
2301 return ret;
2302}
2303
Christopher Faulet2eed8002020-12-07 11:26:13 +01002304/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2305 * the error. This function takes care of incrementing stats and tracked
2306 * counters.
2307 */
2308static int h1_handle_not_impl_err(struct h1c *h1c)
2309{
2310 struct session *sess = h1c->conn->owner;
2311 int ret = 1;
2312
2313 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2314 goto end;
2315
2316 session_inc_http_req_ctr(sess);
2317 session_inc_http_err_ctr(sess);
2318 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2319 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2320 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2321 if (sess->listener->counters)
2322 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2323
2324 h1c->errcode = 501;
2325 ret = h1_send_error(h1c);
2326 sess_log(sess);
2327
2328 end:
2329 return ret;
2330}
2331
Christopher Fauletc18fc232020-10-06 17:41:36 +02002332/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2333 * error. This function takes care of incrementing stats and tracked counters.
2334 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002335static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002336{
2337 struct session *sess = h1c->conn->owner;
2338 int ret = 1;
2339
2340 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2341 goto end;
2342
2343 session_inc_http_req_ctr(sess);
2344 session_inc_http_err_ctr(sess);
2345 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2346 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2347 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2348 if (sess->listener->counters)
2349 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2350
2351 h1c->errcode = 408;
2352 ret = h1_send_error(h1c);
2353 sess_log(sess);
2354 end:
2355 return ret;
2356}
2357
2358
Christopher Faulet51dbc942018-09-13 09:05:15 +02002359/*
2360 * Attempt to read data, and subscribe if none available
2361 */
2362static int h1_recv(struct h1c *h1c)
2363{
2364 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002365 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002366 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002367
Christopher Faulet6b81df72019-10-01 22:08:43 +02002368 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2369
2370 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2371 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002372 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002373 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374
Christopher Fauletae635762020-09-21 11:47:16 +02002375 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2376 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002377 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002378 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002379
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002380 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2381 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002382 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002383 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002384 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002385
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002386 /*
2387 * If we only have a small amount of data, realign it,
2388 * it's probably cheaper than doing 2 recv() calls.
2389 */
2390 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2391 b_slow_realign(&h1c->ibuf, trash.area, 0);
2392
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002393 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002394 if (!h1c->h1s ||
2395 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2396 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002397 flags |= CO_RFL_READ_ONCE;
2398
Willy Tarreau45f2b892018-12-05 07:59:27 +01002399 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002400 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002401 if (h1c->flags & H1C_F_IN_FULL) {
2402 h1c->flags &= ~H1C_F_IN_FULL;
2403 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2404 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002405
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002406 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002407 if (!b_data(&h1c->ibuf)) {
2408 /* try to pre-align the buffer like the rxbufs will be
2409 * to optimize memory copies.
2410 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002411 h1c->ibuf.head = sizeof(struct htx);
2412 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002413 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002414 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002415 if (max && !ret && h1_recv_allowed(h1c)) {
2416 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2417 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002418 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002419 else {
2420 h1_wake_stream_for_recv(h1c->h1s);
2421 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002422 }
2423
Christopher Faulet51dbc942018-09-13 09:05:15 +02002424 if (!b_data(&h1c->ibuf))
2425 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002426 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002427 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002428 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2429 }
2430
2431 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002432 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002433}
2434
2435
2436/*
2437 * Try to send data if possible
2438 */
2439static int h1_send(struct h1c *h1c)
2440{
2441 struct connection *conn = h1c->conn;
2442 unsigned int flags = 0;
2443 size_t ret;
2444 int sent = 0;
2445
Christopher Faulet6b81df72019-10-01 22:08:43 +02002446 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2447
2448 if (conn->flags & CO_FL_ERROR) {
2449 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002450 b_reset(&h1c->obuf);
2451 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002452 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453
2454 if (!b_data(&h1c->obuf))
2455 goto end;
2456
Christopher Faulet46230362019-10-17 16:04:20 +02002457 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002458 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002459 if (h1c->flags & H1C_F_CO_STREAMER)
2460 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002461
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002462 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002463 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002464 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002465 if (h1c->flags & H1C_F_OUT_FULL) {
2466 h1c->flags &= ~H1C_F_OUT_FULL;
2467 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2468 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002469 b_del(&h1c->obuf, ret);
2470 sent = 1;
2471 }
2472
Christopher Faulet145aa472018-12-06 10:56:20 +01002473 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002474 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002475 /* error or output closed, nothing to send, clear the buffer to release it */
2476 b_reset(&h1c->obuf);
2477 }
2478
Christopher Faulet51dbc942018-09-13 09:05:15 +02002479 end:
Christopher Fauletb385b502021-01-13 18:47:57 +01002480 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_WAIT_INPUT)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002481 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002482
Christopher Faulet51dbc942018-09-13 09:05:15 +02002483 /* We're done, no more to send */
2484 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002485 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002486 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002487 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002488 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002489 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002490 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002491 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002492 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2493 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002494 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002495 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002496
Christopher Faulet6b81df72019-10-01 22:08:43 +02002497 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002498 return sent;
2499}
2500
Christopher Faulet51dbc942018-09-13 09:05:15 +02002501/* callback called on any event by the connection handler.
2502 * It applies changes and returns zero, or < 0 if it wants immediate
2503 * destruction of the connection.
2504 */
2505static int h1_process(struct h1c * h1c)
2506{
2507 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002508 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002509
Christopher Faulet6b81df72019-10-01 22:08:43 +02002510 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2511
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002512 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002513 if (b_data(&h1c->ibuf) && /* Input data to be processed */
2514 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2515 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002516 struct buffer *buf;
2517 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002518
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002519 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2520 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002521 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002522
2523 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002524 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
2525 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE)) { /* H2 upgrade supported by the proxy */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002526 /* Try to match H2 preface before parsing the request headers. */
2527 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2528 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002529 if (h1c->flags & H1C_F_ST_ATTACHED) {
2530 /* Force the REOS here to be sure to release the CS.
2531 Here ATTACHED implies !READY, and h1s defined
2532 */
2533 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2534 h1s->flags |= H1S_F_REOS;
2535 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002536 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002537 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002538 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002539 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002540
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002541 /* Create the H1 stream if not already there */
2542 if (!h1s) {
2543 h1s = h1c_frt_stream_new(h1c);
2544 if (!h1s) {
2545 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002546 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002547 goto no_parsing;
2548 }
2549 }
2550
2551 if (h1s->sess->t_idle == -1)
2552 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2553
2554 /* Get the stream rxbuf */
2555 buf = h1_get_buf(h1c, &h1s->rxbuf);
2556 if (!buf) {
2557 h1c->flags |= H1C_F_IN_SALLOC;
2558 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2559 return 0;
2560 }
2561
2562 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2563 h1_process_input(h1c, buf, count);
2564 h1_release_buf(h1c, &h1s->rxbuf);
2565 h1_set_idle_expiration(h1c);
2566
2567 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002568 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002569 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002570 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002571 }
2572 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2573 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002574 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002575 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002576 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2577 h1_handle_not_impl_err(h1c);
2578 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
2579 }
Christopher Fauletae635762020-09-21 11:47:16 +02002580 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002581 h1_send(h1c);
2582
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002583 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002584 if (!(h1c->flags & H1C_F_ST_READY)) {
2585 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002586 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002587 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002588 if (h1_handle_bad_req(h1c))
2589 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002590 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002591 }
2592
2593 /* Handle pending error, if any (only possible on frontend connection) */
2594 if (h1c->flags & H1C_F_ERR_PENDING) {
2595 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2596 if (h1_send_error(h1c))
2597 h1_send(h1c);
2598 }
Christopher Fauletae635762020-09-21 11:47:16 +02002599
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002600 /* If there is some pending outgoing data or error, just wait */
2601 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2602 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002603
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002604 /* Otherwise we can release the H1 connection */
2605 goto release;
2606 }
2607 else {
2608 /* Here there is still a H1 stream with a conn-stream.
2609 * Report the connection state at the stream level
2610 */
2611 if (conn_xprt_read0_pending(conn)) {
2612 h1s->flags |= H1S_F_REOS;
2613 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2614 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002615 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002616 h1s->cs->flags |= CS_FL_ERROR;
2617 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002618 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002619 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002620 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002621
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002622 if (!b_data(&h1c->ibuf))
2623 h1_release_buf(h1c, &h1c->ibuf);
2624
2625
2626 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2627 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2628 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002629 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002630
Christopher Faulet47365272018-10-31 17:40:50 +01002631 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002632 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002633 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002634 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002635
2636 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002637 if (h1c->flags & H1C_F_ST_ATTACHED) {
2638 /* Don't release the H1 connetion right now, we must destroy the
2639 * attached CS first. Here, the H1C must not be READY */
2640 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2641
2642 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2643 h1s->cs->flags |= CS_FL_EOS;
2644 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2645 h1s->cs->flags |= CS_FL_ERROR;
2646 h1_alert(h1s);
2647 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2648 }
2649 else {
2650 h1_release(h1c);
2651 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2652 }
Christopher Faulet539e0292018-11-19 10:40:09 +01002653 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002654}
2655
Willy Tarreau691d5032021-01-20 14:55:01 +01002656struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002657{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002658 struct connection *conn;
2659 struct tasklet *tl = (struct tasklet *)t;
2660 int conn_in_list;
2661 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002662 int ret = 0;
2663
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002664
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002665 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002666 if (tl->context == NULL) {
2667 /* The connection has been taken over by another thread,
2668 * we're no longer responsible for it, so just free the
2669 * tasklet, and do nothing.
2670 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002671 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002672 tasklet_free(tl);
2673 return NULL;
2674 }
2675 h1c = ctx;
2676 conn = h1c->conn;
2677
2678 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2679
2680 /* Remove the connection from the list, to be sure nobody attempts
2681 * to use it while we handle the I/O events
2682 */
2683 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2684 if (conn_in_list)
2685 MT_LIST_DEL(&conn->list);
2686
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002687 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002688
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002689 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002690 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002691 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002692 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002693 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002694 ret = h1_process(h1c);
2695 /* If we were in an idle list, we want to add it back into it,
2696 * unless h1_process() returned -1, which mean it has destroyed
2697 * the connection (testing !ret is enough, if h1_process() wasn't
2698 * called then ret will be 0 anyway.
2699 */
2700 if (!ret && conn_in_list) {
2701 struct server *srv = objt_server(conn->target);
2702
2703 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002704 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002705 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002706 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002707 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002708 return NULL;
2709}
2710
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002711static void h1_reset(struct connection *conn)
2712{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002713
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002714}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002715
2716static int h1_wake(struct connection *conn)
2717{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002718 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002719 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002720
Christopher Faulet6b81df72019-10-01 22:08:43 +02002721 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2722
Christopher Faulet539e0292018-11-19 10:40:09 +01002723 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002724 ret = h1_process(h1c);
2725 if (ret == 0) {
2726 struct h1s *h1s = h1c->h1s;
2727
Christopher Fauletad4daf62021-01-21 17:49:01 +01002728 if (h1c->flags & H1C_F_ST_ATTACHED)
2729 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002730 }
2731 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002732}
2733
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002734/* Connection timeout management. The principle is that if there's no receipt
2735 * nor sending for a certain amount of time, the connection is closed.
2736 */
2737static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2738{
2739 struct h1c *h1c = context;
2740 int expired = tick_is_expired(t->expire, now_ms);
2741
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002742 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002743
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002744 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002745 /* Make sure nobody stole the connection from us */
2746 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2747
2748 /* Somebody already stole the connection from us, so we should not
2749 * free it, we just have to free the task.
2750 */
2751 if (!t->context) {
2752 h1c = NULL;
2753 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2754 goto do_leave;
2755 }
2756
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002757 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002758 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2759 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002760 return t;
2761 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002762
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002763 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002764 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002765 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002766 if (h1c->flags & H1C_F_ST_READY) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002767 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2768 t->expire = TICK_ETERNITY;
2769 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2770 return t;
2771 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002772
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002773 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002774 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2775 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002776 if (h1_handle_req_tout(h1c))
2777 h1_send(h1c);
2778 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2779 h1_refresh_timeout(h1c);
Christopher Fauletcc043f62020-12-14 10:06:12 +01002780 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002781 return t;
2782 }
2783 }
2784
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002785 if (h1c->flags & H1C_F_ST_ATTACHED) {
2786 /* Don't release the H1 connetion right now, we must destroy the
2787 * attached CS first. Here, the H1C must not be READY */
2788 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
2789 h1_alert(h1c->h1s);
2790 h1_refresh_timeout(h1c);
2791 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2792 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2793 return t;
2794 }
2795
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002796 /* We're about to destroy the connection, so make sure nobody attempts
2797 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002798 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002799 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002800 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002801
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002802 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002803 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002804
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002805 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002806 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002807
2808 if (!h1c) {
2809 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002810 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002811 return NULL;
2812 }
2813
2814 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002815 h1_release(h1c);
2816 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002817 return NULL;
2818}
2819
Christopher Faulet51dbc942018-09-13 09:05:15 +02002820/*******************************************/
2821/* functions below are used by the streams */
2822/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002823
Christopher Faulet51dbc942018-09-13 09:05:15 +02002824/*
2825 * Attach a new stream to a connection
2826 * (Used for outgoing connections)
2827 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002828static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002829{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002830 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002831 struct conn_stream *cs = NULL;
2832 struct h1s *h1s;
2833
Christopher Faulet6b81df72019-10-01 22:08:43 +02002834 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002835 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002836 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 +02002837 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002838 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002839
Christopher Faulet236c93b2020-07-02 09:19:54 +02002840 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002841 if (!cs) {
2842 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 +02002843 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002844 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002845
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002846 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002847 if (h1s == NULL) {
2848 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 +02002849 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002850 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002851
Christopher Faulet6b81df72019-10-01 22:08:43 +02002852 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002853 return cs;
2854 end:
2855 cs_free(cs);
2856 return NULL;
2857}
2858
2859/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2860 * this mux, it's easy as we can only store a single conn_stream.
2861 */
2862static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2863{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002864 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002865 struct h1s *h1s = h1c->h1s;
2866
2867 if (h1s)
2868 return h1s->cs;
2869
2870 return NULL;
2871}
2872
Christopher Faulet73c12072019-04-08 11:23:22 +02002873static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002874{
Christopher Faulet73c12072019-04-08 11:23:22 +02002875 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002876
Christopher Faulet6b81df72019-10-01 22:08:43 +02002877 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002878 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002879 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002880}
2881
2882/*
2883 * Detach the stream from the connection and possibly release the connection.
2884 */
2885static void h1_detach(struct conn_stream *cs)
2886{
2887 struct h1s *h1s = cs->ctx;
2888 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002889 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002890 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002891
Christopher Faulet6b81df72019-10-01 22:08:43 +02002892 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2893
Christopher Faulet51dbc942018-09-13 09:05:15 +02002894 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002895 if (!h1s) {
2896 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002897 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002898 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002899
Olivier Houchardf502aca2018-12-14 19:42:40 +01002900 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002901 h1c = h1s->h1c;
2902 h1s->cs = NULL;
2903
Christopher Faulet42849b02020-10-05 11:35:17 +02002904 sess->accept_date = date;
2905 sess->tv_accept = now;
2906 sess->t_handshake = 0;
2907 sess->t_idle = -1;
2908
Olivier Houchard8a786902018-12-15 16:05:40 +01002909 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2910 h1s_destroy(h1s);
2911
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002912 if ((h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_IDLE)) == (H1C_F_IS_BACK|H1C_F_ST_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002913 /* If there are any excess server data in the input buffer,
2914 * release it and close the connection ASAP (some data may
2915 * remain in the output buffer). This happens if a server sends
2916 * invalid responses. So in such case, we don't want to reuse
2917 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002918 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002919 if (b_data(&h1c->ibuf)) {
2920 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002921 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002922 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002923 goto release;
2924 }
Christopher Faulet03627242019-07-19 11:34:08 +02002925
Christopher Faulet08016ab2020-07-01 16:10:06 +02002926 if (h1c->conn->flags & CO_FL_PRIVATE) {
2927 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002928 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2929 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002930 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002931 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002932 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002933 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002934 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002935 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002936 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2937 goto end;
2938 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002939 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002940 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002941 if (h1c->conn->owner == sess)
2942 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002943 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002944 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002945 /* The server doesn't want it, let's kill the connection right away */
2946 h1c->conn->mux->destroy(h1c);
2947 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2948 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002949 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002950 /* At this point, the connection has been added to the
2951 * server idle list, so another thread may already have
2952 * hijacked it, so we can't do anything with it.
2953 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002954 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002955 }
2956 }
2957
Christopher Fauletf1204b82019-07-19 14:51:06 +02002958 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002959 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002960 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002961 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002962 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002963 !h1c->conn->owner) {
2964 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002965 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002966 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002967 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002968 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002969 /* If we have a new request, process it immediately or
2970 * subscribe for reads waiting for new data
2971 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01002972 if (unlikely(b_data(&h1c->ibuf))) {
2973 if (h1_process(h1c) == -1)
2974 goto end;
2975 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002976 else
2977 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2978 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002979 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002980 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002981 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002982 end:
2983 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002984}
2985
2986
2987static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2988{
2989 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002990 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002991
2992 if (!h1s)
2993 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002994 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002995
Christopher Faulet6b81df72019-10-01 22:08:43 +02002996 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2997
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002998 if (cs->flags & CS_FL_SHR)
2999 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003000 if (cs->flags & CS_FL_KILL_CONN) {
3001 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
3002 goto do_shutr;
3003 }
3004 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3005 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003006 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003007 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003008
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003009 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3010 /* Here attached is implicit because there is CS */
3011 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3012 goto end;
3013 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003014 if (h1s->flags & H1S_F_WANT_KAL) {
3015 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003016 goto end;
3017 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003018
Christopher Faulet7f366362019-04-08 10:51:20 +02003019 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003020 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
3021 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003022 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003023 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003024 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02003025 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003026 end:
3027 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003028}
3029
3030static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3031{
3032 struct h1s *h1s = cs->ctx;
3033 struct h1c *h1c;
3034
3035 if (!h1s)
3036 return;
3037 h1c = h1s->h1c;
3038
Christopher Faulet6b81df72019-10-01 22:08:43 +02003039 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3040
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003041 if (cs->flags & CS_FL_SHW)
3042 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003043 if (cs->flags & CS_FL_KILL_CONN) {
3044 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003045 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003046 }
3047 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3048 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3049 goto do_shutw;
3050 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003051
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003052 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3053 /* Here attached is implicit because there is CS */
3054 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3055 goto end;
3056 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003057 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3058 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003059 goto end;
3060 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003061
Christopher Faulet7f366362019-04-08 10:51:20 +02003062 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003063 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003064 if (!b_data(&h1c->obuf))
3065 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003066 end:
3067 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003068}
3069
Christopher Faulet666a0c42019-01-08 11:12:04 +01003070static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003071{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003072 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003073
Christopher Faulet6b81df72019-10-01 22:08:43 +02003074 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003075 conn_xprt_shutw(conn);
3076 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003077 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003078}
3079
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003080/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3081 * The <es> pointer is not allowed to differ from the one passed to the
3082 * subscribe() call. It always returns zero.
3083 */
3084static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003085{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003086 struct h1s *h1s = cs->ctx;
3087
3088 if (!h1s)
3089 return 0;
3090
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003091 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003092 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003093
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003094 es->events &= ~event_type;
3095 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003096 h1s->subs = NULL;
3097
3098 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003099 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003100
3101 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003102 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003103
Christopher Faulet51dbc942018-09-13 09:05:15 +02003104 return 0;
3105}
3106
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003107/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3108 * event subscriber <es> is not allowed to change from a previous call as long
3109 * as at least one event is still subscribed. The <event_type> must only be a
3110 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3111 * the conn_stream <cs> was already detached, in which case it will return -1.
3112 */
3113static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003114{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003115 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003116 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003117
3118 if (!h1s)
3119 return -1;
3120
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003121 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003122 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003123
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003124 es->events |= event_type;
3125 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003126
3127 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003128 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003129
3130
Christopher Faulet6b81df72019-10-01 22:08:43 +02003131 if (event_type & SUB_RETRY_SEND) {
3132 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003133 /*
3134 * If the conn_stream attempt to subscribe, and the
3135 * mux isn't subscribed to the connection, then it
3136 * probably means the connection wasn't established
3137 * yet, so we have to subscribe.
3138 */
3139 h1c = h1s->h1c;
3140 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3141 h1c->conn->xprt->subscribe(h1c->conn,
3142 h1c->conn->xprt_ctx,
3143 SUB_RETRY_SEND,
3144 &h1c->wait_event);
3145 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003146 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003147}
3148
3149/* Called from the upper layer, to receive data */
3150static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3151{
3152 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003153 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003154 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003155 size_t ret = 0;
3156
Willy Tarreau022e5e52020-09-10 09:33:15 +02003157 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003158
3159 /* Do nothing for now if not READY */
3160 if (!(h1c->flags & H1C_F_ST_READY)) {
3161 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3162 goto end;
3163 }
3164
Christopher Faulet539e0292018-11-19 10:40:09 +01003165 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02003166 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003167 else
3168 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003169
Christopher Faulete18777b2019-04-16 16:46:36 +02003170 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02003171 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02003172 h1c->flags |= H1C_F_WANT_SPLICE;
3173 TRACE_STATE("Block xprt rcv_buf to flush stream's buffer (want_splice)", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003174 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003175 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003176 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003177 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003178 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003179 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003180
3181 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003182 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003183 return ret;
3184}
3185
3186
3187/* Called from the upper layer, to send data */
3188static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3189{
3190 struct h1s *h1s = cs->ctx;
3191 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003192 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003193
3194 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003195 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003196 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003197
Willy Tarreau022e5e52020-09-10 09:33:15 +02003198 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003199
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003200 /* If we're not connected yet, or we're waiting for a handshake, stop
3201 * now, as we don't want to remove everything from the channel buffer
3202 * before we're sure we can send it.
3203 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003204 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003205 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003206 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003207 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003208
Christopher Fauletdea24742021-01-22 15:12:30 +01003209 if (h1c->flags & H1C_F_ST_ERROR) {
3210 cs->flags |= CS_FL_ERROR;
3211 TRACE_DEVEL("H1 connection is in error, leaving in error", H1_EV_STRM_SEND|H1_EV_H1C_ERR|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
3212 return 0;
3213 }
3214
Christopher Faulet46230362019-10-17 16:04:20 +02003215 /* Inherit some flags from the upper layer */
3216 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3217 if (flags & CO_SFL_MSG_MORE)
3218 h1c->flags |= H1C_F_CO_MSG_MORE;
3219 if (flags & CO_SFL_STREAMER)
3220 h1c->flags |= H1C_F_CO_STREAMER;
3221
Christopher Fauletc31872f2019-06-04 22:09:36 +02003222 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003223 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003224
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003225 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3226 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003227 else
3228 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003229
3230 if ((count - ret) > 0)
3231 h1c->flags |= H1C_F_CO_MSG_MORE;
3232
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003233 if (!ret)
3234 break;
3235 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003236 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003237 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003238 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003239 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003240
3241 if (h1c->flags & H1C_F_ST_ERROR) {
3242 TRACE_DEVEL("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
3243 cs->flags |= CS_FL_ERROR;
3244 }
3245
Christopher Faulet7a145d62020-08-05 11:31:16 +02003246 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003247 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003248 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003249}
3250
Willy Tarreaue5733232019-05-22 19:24:06 +02003251#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003252/* Send and get, using splicing */
3253static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3254{
3255 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003256 struct h1c *h1c = h1s->h1c;
3257 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003258 int ret = 0;
3259
Willy Tarreau022e5e52020-09-10 09:33:15 +02003260 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003261
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003262 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003263 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003264 TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003265 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003266 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003267 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003268 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003269 goto end;
3270 }
3271
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003272 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3273 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003274 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3275 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003276 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003277 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003278 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003279 }
3280
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003281 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003282 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3283 goto end;
3284 }
3285
Christopher Faulet1be55f92018-10-02 15:59:23 +02003286 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3287 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003288 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003289 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003290 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003291 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003292 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003293 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003294 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003295 }
3296
Christopher Faulet1be55f92018-10-02 15:59:23 +02003297 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003298 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003299 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003300 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003301 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003302 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003303
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003304 if ((h1s->flags & H1S_F_REOS) ||
3305 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003306 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003307 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003308 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003309 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003310
Willy Tarreau022e5e52020-09-10 09:33:15 +02003311 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003312 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003313}
3314
3315static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3316{
3317 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003318 int ret = 0;
3319
Willy Tarreau022e5e52020-09-10 09:33:15 +02003320 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003321
Christopher Faulet1be55f92018-10-02 15:59:23 +02003322 if (b_data(&h1s->h1c->obuf))
3323 goto end;
3324
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003325 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003326 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003327 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003328 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3329 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003330 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003331 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003332 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003333
Willy Tarreau022e5e52020-09-10 09:33:15 +02003334 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003335 return ret;
3336}
3337#endif
3338
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003339static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3340{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003341 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003342 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003343
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003344 switch (mux_ctl) {
3345 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003346 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003347 ret |= MUX_STATUS_READY;
3348 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003349 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003350 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3351 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003352 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3353 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3354 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003355 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003356 default:
3357 return -1;
3358 }
3359}
3360
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003361/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003362static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003363{
3364 struct h1c *h1c = conn->ctx;
3365 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003366 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003367
Christopher Fauletf376a312019-01-04 15:16:06 +01003368 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3369 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003370 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3371 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3372 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3373 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3374
3375 if (h1s) {
3376 char *method;
3377
3378 if (h1s->meth < HTTP_METH_OTHER)
3379 method = http_known_methods[h1s->meth].ptr;
3380 else
3381 method = "UNKNOWN";
3382 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3383 " .meth=%s status=%d",
3384 h1s, h1s->flags,
3385 h1m_state_str(h1s->req.state),
3386 h1m_state_str(h1s->res.state), method, h1s->status);
3387 if (h1s->cs)
3388 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3389 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003390
3391 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3392 if (h1s->subs) {
3393 if (h1s->subs) {
3394 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3395 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3396 h1s->subs->tasklet->calls,
3397 h1s->subs->tasklet->context);
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003398 if (h1s->subs->tasklet->calls >= 1000000)
3399 ret = 1;
Willy Tarreau150c4f82021-01-20 17:05:58 +01003400 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3401 chunk_appendf(&trash, ")");
3402 }
3403 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003404 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003405 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003406}
3407
3408
3409/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3410static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3411{
3412 struct h1_hdr_entry *entry;
3413
3414 /* Be sure there is a non-empty <to> */
3415 if (!strlen(to)) {
3416 memprintf(err, "expect <to>");
3417 return -1;
3418 }
3419
3420 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003421 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003422 memprintf(err, "<from> and <to> must not differ execpt the case");
3423 return -1;
3424 }
3425
3426 /* Be sure <from> does not already existsin the tree */
3427 if (ebis_lookup(&hdrs_map.map, from)) {
3428 memprintf(err, "duplicate entry '%s'", from);
3429 return -1;
3430 }
3431
3432 /* Create the entry and insert it in the tree */
3433 entry = malloc(sizeof(*entry));
3434 if (!entry) {
3435 memprintf(err, "out of memory");
3436 return -1;
3437 }
3438
3439 entry->node.key = strdup(from);
3440 entry->name.ptr = strdup(to);
3441 entry->name.len = strlen(to);
3442 if (!entry->node.key || !entry->name.ptr) {
3443 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003444 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003445 free(entry);
3446 memprintf(err, "out of memory");
3447 return -1;
3448 }
3449 ebis_insert(&hdrs_map.map, &entry->node);
3450 return 0;
3451}
3452
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003453/* Migrate the the connection to the current thread.
3454 * Return 0 if successful, non-zero otherwise.
3455 * Expected to be called with the old thread lock held.
3456 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003457static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003458{
3459 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003460 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003461
3462 if (fd_takeover(conn->handle.fd, conn) != 0)
3463 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003464
3465 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3466 /* We failed to takeover the xprt, even if the connection may
3467 * still be valid, flag it as error'd, as we have already
3468 * taken over the fd, and wake the tasklet, so that it will
3469 * destroy it.
3470 */
3471 conn->flags |= CO_FL_ERROR;
3472 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3473 return -1;
3474 }
3475
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003476 if (h1c->wait_event.events)
3477 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3478 h1c->wait_event.events, &h1c->wait_event);
3479 /* To let the tasklet know it should free itself, and do nothing else,
3480 * set its context to NULL.
3481 */
3482 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003483 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003484
3485 task = h1c->task;
3486 if (task) {
3487 task->context = NULL;
3488 h1c->task = NULL;
3489 __ha_barrier_store();
3490 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003491
3492 h1c->task = task_new(tid_bit);
3493 if (!h1c->task) {
3494 h1_release(h1c);
3495 return -1;
3496 }
3497 h1c->task->process = h1_timeout_task;
3498 h1c->task->context = h1c;
3499 }
3500 h1c->wait_event.tasklet = tasklet_new();
3501 if (!h1c->wait_event.tasklet) {
3502 h1_release(h1c);
3503 return -1;
3504 }
3505 h1c->wait_event.tasklet->process = h1_io_cb;
3506 h1c->wait_event.tasklet->context = h1c;
3507 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3508 SUB_RETRY_RECV, &h1c->wait_event);
3509
3510 return 0;
3511}
3512
3513
Christopher Faulet98fbe952019-07-22 16:18:24 +02003514static void h1_hdeaders_case_adjust_deinit()
3515{
3516 struct ebpt_node *node, *next;
3517 struct h1_hdr_entry *entry;
3518
3519 node = ebpt_first(&hdrs_map.map);
3520 while (node) {
3521 next = ebpt_next(node);
3522 ebpt_delete(node);
3523 entry = container_of(node, struct h1_hdr_entry, node);
3524 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003525 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003526 free(entry);
3527 node = next;
3528 }
3529 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003530}
3531
Christopher Faulet98fbe952019-07-22 16:18:24 +02003532static int cfg_h1_headers_case_adjust_postparser()
3533{
3534 FILE *file = NULL;
3535 char *c, *key_beg, *key_end, *value_beg, *value_end;
3536 char *err;
3537 int rc, line = 0, err_code = 0;
3538
3539 if (!hdrs_map.name)
3540 goto end;
3541
3542 file = fopen(hdrs_map.name, "r");
3543 if (!file) {
3544 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3545 hdrs_map.name);
3546 err_code |= ERR_ALERT | ERR_FATAL;
3547 goto end;
3548 }
3549
3550 /* now parse all lines. The file may contain only two header name per
3551 * line, separated by spaces. All heading and trailing spaces will be
3552 * ignored. Lines starting with a # are ignored.
3553 */
3554 while (fgets(trash.area, trash.size, file) != NULL) {
3555 line++;
3556 c = trash.area;
3557
3558 /* strip leading spaces and tabs */
3559 while (*c == ' ' || *c == '\t')
3560 c++;
3561
3562 /* ignore emptu lines, or lines beginning with a dash */
3563 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3564 continue;
3565
3566 /* look for the end of the key */
3567 key_beg = c;
3568 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3569 c++;
3570 key_end = c;
3571
3572 /* strip middle spaces and tabs */
3573 while (*c == ' ' || *c == '\t')
3574 c++;
3575
3576 /* look for the end of the value, it is the end of the line */
3577 value_beg = c;
3578 while (*c && *c != '\n' && *c != '\r')
3579 c++;
3580 value_end = c;
3581
3582 /* trim possibly trailing spaces and tabs */
3583 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3584 value_end--;
3585
3586 /* set final \0 and check entries */
3587 *key_end = '\0';
3588 *value_end = '\0';
3589
3590 err = NULL;
3591 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3592 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003593 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3594 hdrs_map.name, err, line);
3595 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003596 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003597 goto end;
3598 }
3599 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003600 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3601 hdrs_map.name, err, line);
3602 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003603 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003604 }
3605 }
3606
3607 end:
3608 if (file)
3609 fclose(file);
3610 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3611 return err_code;
3612}
3613
3614
3615/* config parser for global "h1-outgoing-header-case-adjust" */
3616static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3617 struct proxy *defpx, const char *file, int line,
3618 char **err)
3619{
3620 if (too_many_args(2, args, err, NULL))
3621 return -1;
3622 if (!*(args[1]) || !*(args[2])) {
3623 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3624 return -1;
3625 }
3626 return add_hdr_case_adjust(args[1], args[2], err);
3627}
3628
3629/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3630static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3631 struct proxy *defpx, const char *file, int line,
3632 char **err)
3633{
3634 if (too_many_args(1, args, err, NULL))
3635 return -1;
3636 if (!*(args[1])) {
3637 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3638 return -1;
3639 }
3640 free(hdrs_map.name);
3641 hdrs_map.name = strdup(args[1]);
3642 return 0;
3643}
3644
3645
3646/* config keyword parsers */
3647static struct cfg_kw_list cfg_kws = {{ }, {
3648 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3649 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3650 { 0, NULL, NULL },
3651 }
3652};
3653
3654INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3655REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3656
3657
Christopher Faulet51dbc942018-09-13 09:05:15 +02003658/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003659/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003660/****************************************/
3661
3662/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003663static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003664 .init = h1_init,
3665 .wake = h1_wake,
3666 .attach = h1_attach,
3667 .get_first_cs = h1_get_first_cs,
3668 .detach = h1_detach,
3669 .destroy = h1_destroy,
3670 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003671 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003672 .rcv_buf = h1_rcv_buf,
3673 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003674#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003675 .rcv_pipe = h1_rcv_pipe,
3676 .snd_pipe = h1_snd_pipe,
3677#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003678 .subscribe = h1_subscribe,
3679 .unsubscribe = h1_unsubscribe,
3680 .shutr = h1_shutr,
3681 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003682 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003683 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003684 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003685 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003686 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003687 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003688};
3689
3690
3691/* this mux registers default HTX proto */
3692static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003693{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003694
Willy Tarreau0108d902018-11-25 19:14:37 +01003695INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3696
Christopher Faulet51dbc942018-09-13 09:05:15 +02003697/*
3698 * Local variables:
3699 * c-indent-level: 8
3700 * c-basic-offset: 8
3701 * End:
3702 */