blob: 277220b2e765e01f34b8676bbd0e08766082af2b [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) {
1693 void *old_area = h1c->obuf.area;
1694
Christopher Faulet6b81df72019-10-01 22:08:43 +02001695 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 +01001696 if (h1m->state == H1_MSG_DATA && chn_htx->flags & HTX_FL_EOM) {
1697 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1698 last_data = 1;
1699 }
1700
Willy Tarreau3815b222018-12-11 19:50:43 +01001701 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001702 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001703 h1c->obuf.data = count;
1704
1705 buf->area = old_area;
1706 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001707
Christopher Faulet6b81df72019-10-01 22:08:43 +02001708 chn_htx = (struct htx *)buf->area;
1709 htx_reset(chn_htx);
1710
Christopher Fauletadb22202018-12-12 10:32:09 +01001711 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001712 * size and eventually the last chunk. We have at least
1713 * the size of the struct htx to write the chunk
1714 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01001715 */
1716 if (h1m->flags & H1_MF_CHNK) {
1717 h1_emit_chunk_size(&h1c->obuf, count);
1718 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001719 if (last_data) {
1720 /* Emit the last chunk too at the buffer's end */
1721 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
1722 }
Christopher Fauletadb22202018-12-12 10:32:09 +01001723 }
1724
Willy Tarreau3815b222018-12-11 19:50:43 +01001725 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001726 if (h1m->state == H1_MSG_DATA)
1727 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001728 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001729 else
1730 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001731 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001732
1733 if (last_data) {
1734 h1m->state = H1_MSG_DONE;
1735 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
1736 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1737 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1738 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1739 }
1740
1741 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1742 H1_EV_TX_DATA, h1c->conn, h1s);
1743 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001744 goto out;
1745 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001746 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001747 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001748 else
1749 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001750
Christopher Fauletc2518a52019-06-25 21:41:02 +02001751 tmp.data = 0;
1752 tmp.size = b_room(&h1c->obuf);
Christopher Fauletdea24742021-01-22 15:12:30 +01001753 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && !(h1c->flags & H1C_F_WAIT_INPUT) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001754 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001755 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001756 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001757 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001758 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001759
Christopher Fauletb2e84162018-12-06 11:39:49 +01001760 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001761 if (type != HTX_BLK_DATA && vlen > count)
1762 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001763
Christopher Faulet94b2c762019-05-24 15:28:57 +02001764 if (type == HTX_BLK_UNUSED)
1765 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001766
Christopher Faulet94b2c762019-05-24 15:28:57 +02001767 switch (h1m->state) {
1768 case H1_MSG_RQBEFORE:
1769 if (type != HTX_BLK_REQ_SL)
1770 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001771 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 +02001772 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001773 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001774 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001775 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001776 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001777 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001778 if (sl->flags & HTX_SL_F_BODYLESS)
1779 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001780 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01001781 if (h1s->meth == HTTP_METH_HEAD)
1782 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Fauletb385b502021-01-13 18:47:57 +01001783 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1784 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Christopher Faulet089acd52020-09-21 10:57:52 +02001785 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1786 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1787 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001788 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001789
Christopher Faulet94b2c762019-05-24 15:28:57 +02001790 case H1_MSG_RPBEFORE:
1791 if (type != HTX_BLK_RES_SL)
1792 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001793 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 +02001794 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001795 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001796 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001797 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001798 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001799 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001800 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001801 if (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001802 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001803 else if (h1s->status == 204 || h1s->status == 304)
1804 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02001805 h1m->state = H1_MSG_HDR_FIRST;
1806 break;
1807
Christopher Faulet94b2c762019-05-24 15:28:57 +02001808 case H1_MSG_HDR_FIRST:
1809 case H1_MSG_HDR_NAME:
1810 case H1_MSG_HDR_L2_LWS:
1811 if (type == HTX_BLK_EOH)
1812 goto last_lf;
1813 if (type != HTX_BLK_HDR)
1814 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001815
Christopher Faulet9768c262018-10-22 09:34:31 +02001816 h1m->state = H1_MSG_HDR_NAME;
1817 n = htx_get_blk_name(chn_htx, blk);
1818 v = htx_get_blk_value(chn_htx, blk);
1819
Christopher Faulet86d144c2019-08-14 16:32:25 +02001820 /* Skip all pseudo-headers */
1821 if (*(n.ptr) == ':')
1822 goto skip_hdr;
1823
Christopher Fauletb045bb22020-02-28 10:42:20 +01001824 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001825 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001826 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001827 /* Only skip C-L header with invalid value. */
1828 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001829 goto skip_hdr;
1830 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001831 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001832 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001833 if (!v.len)
1834 goto skip_hdr;
1835 }
1836
Christopher Faulet67d58092019-10-02 10:51:38 +02001837 /* Skip header if same name is used to add the server name */
1838 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1839 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1840 goto skip_hdr;
1841
Christopher Faulet98fbe952019-07-22 16:18:24 +02001842 /* Try to adjust the case of the header name */
1843 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1844 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001845 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001846 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001847 skip_hdr:
1848 h1m->state = H1_MSG_HDR_L2_LWS;
1849 break;
1850
Christopher Faulet94b2c762019-05-24 15:28:57 +02001851 case H1_MSG_LAST_LF:
1852 if (type != HTX_BLK_EOH)
1853 goto error;
1854 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001855 h1m->state = H1_MSG_LAST_LF;
1856 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001857 /* If the reply comes from haproxy while the request is
1858 * not finished, we force the connection close. */
1859 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1860 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1861 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1862 }
1863
1864 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001865 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001866 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001867 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001868 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001869 /* Try to adjust the case of the header name */
1870 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1871 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001872 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001873 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001874 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001875 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001876 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001877
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001878 if ((h1s->meth != HTTP_METH_CONNECT &&
1879 (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 +01001880 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Fauletc75668e2020-12-07 18:10:32 +01001881 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && h1s->meth != HTTP_METH_HEAD &&
1882 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001883 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1884 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001885 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001886 n = ist("transfer-encoding");
1887 v = ist("chunked");
1888 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1889 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001890 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001891 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001892 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001893 h1m->flags |= H1_MF_CHNK;
1894 }
1895
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001896 /* Now add the server name to a header (if requested) */
1897 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1898 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1899 struct server *srv = objt_server(h1c->conn->target);
1900
1901 if (srv) {
1902 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1903 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001904
1905 /* Try to adjust the case of the header name */
1906 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1907 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001908 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001909 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001910 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001911 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001912 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1913 }
1914
Christopher Faulet6b81df72019-10-01 22:08:43 +02001915 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1916 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1917
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001918 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001919 if (!chunk_memcat(&tmp, "\r\n", 2))
1920 goto full;
1921 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001922 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001923 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001924 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001925 if (!chunk_memcat(&tmp, "\r\n", 2))
1926 goto full;
1927 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001928 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001929 else if ((h1m->flags & H1_MF_RESP) &&
1930 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001931 if (!chunk_memcat(&tmp, "\r\n", 2))
1932 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001933 h1m_init_res(&h1s->res);
1934 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001935 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001936 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001937 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001938 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001939 if (!chunk_memcat(&tmp, "\r\n", 2))
1940 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001941 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001942 goto done;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001943 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001944 else {
1945 /* EOM flag is set and it is the last block */
1946 if (htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
1947 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
1948 goto full;
1949 else if (!chunk_memcat(&tmp, "\r\n", 2))
1950 goto full;
1951 goto done;
1952 }
1953 else if (!chunk_memcat(&tmp, "\r\n", 2))
1954 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001955 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001956 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001957 break;
1958
Christopher Faulet94b2c762019-05-24 15:28:57 +02001959 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001960 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001961 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001962 /* If the message is not chunked, never
1963 * add the last chunk. */
1964 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001965 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001966 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 +02001967 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001968 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001969 else if (type != HTX_BLK_DATA)
1970 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001971
1972 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 +02001973
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001974 /* It is the last block of this message. After this one,
1975 * only tunneled data may be forwarded. */
1976 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
1977 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1978 last_data = 1;
1979 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02001980
1981 if (vlen > count) {
1982 /* Get the maximum amount of data we can xferred */
1983 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001984 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001985 }
1986
1987 chklen = 0;
1988 if (h1m->flags & H1_MF_CHNK) {
1989 chklen = b_room(&tmp);
1990 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1991 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1992 (chklen < 1048576) ? 5 : 8);
1993 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001994
1995 /* If it is the end of the chunked message (without EOT), reserve the
1996 * last chunk size */
1997 if (last_data)
1998 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001999 }
2000
2001 if (vlen + chklen > b_room(&tmp)) {
2002 /* too large for the buffer */
2003 if (chklen >= b_room(&tmp))
2004 goto full;
2005 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002006 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002007 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002008 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002009 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002010 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002011 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002012
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002013 /* Space already reserved, so it must succeed */
2014 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2015 goto error;
2016
Christopher Faulet6b81df72019-10-01 22:08:43 +02002017 if (h1m->state == H1_MSG_DATA)
2018 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002019 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002020 else
2021 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002022 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002023 if (last_data)
2024 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002025 break;
2026
Christopher Faulet94b2c762019-05-24 15:28:57 +02002027 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002028 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002029 goto error;
2030 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002031 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002032
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002033 /* If the message is not chunked, ignore
2034 * trailers. It may happen with H2 messages. */
2035 if (!(h1m->flags & H1_MF_CHNK))
2036 break;
2037
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002038 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002039 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002040 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002041 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2042 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002043 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002044 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002045 else { // HTX_BLK_TLR
2046 n = htx_get_blk_name(chn_htx, blk);
2047 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002048
2049 /* Try to adjust the case of the header name */
2050 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2051 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002052 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002053 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002054 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002055 break;
2056
Christopher Faulet94b2c762019-05-24 15:28:57 +02002057 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002058 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2059 goto error; /* For now return an error */
2060
Christopher Faulet94b2c762019-05-24 15:28:57 +02002061 done:
2062 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002063 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
2064 h1c->flags |= H1C_F_WAIT_INPUT;
2065 TRACE_STATE("Disable send on h1c (wait_input)", H1_EV_TX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
2066 }
2067 else if ((h1m->flags & H1_MF_RESP) &&
2068 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2069 /* a successful reply to a CONNECT or a protocol switching is sent
2070 * to the client. Switch the response to tunnel mode.
2071 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002072 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002073 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 +01002074 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002075
2076 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
Christopher Fauletb385b502021-01-13 18:47:57 +01002077 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002078 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02002079 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 +02002080 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002081
2082 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2083 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002084 break;
2085
Christopher Faulet9768c262018-10-22 09:34:31 +02002086 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002087 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002088 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002089 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002090 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002091 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002092 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002093 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2094 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002095 break;
2096 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002097
2098 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002099 total += vlen;
2100 count -= vlen;
2101 if (sz == vlen)
2102 blk = htx_remove_blk(chn_htx, blk);
2103 else {
2104 htx_cut_data_blk(chn_htx, blk, vlen);
2105 break;
2106 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002107 }
2108
Christopher Faulet9768c262018-10-22 09:34:31 +02002109 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002110 /* when the output buffer is empty, tmp shares the same area so that we
2111 * only have to update pointers and lengths.
2112 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002113 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2114 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002115 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002116 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002117
Willy Tarreau3815b222018-12-11 19:50:43 +01002118 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002119 out:
2120 if (!buf_room_for_htx_data(&h1c->obuf)) {
2121 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002122 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002123 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002124 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002125 /* Both the request and the response reached the DONE state. So set EOI
2126 * flag on the conn-stream. Most of time, the flag will already be set,
2127 * except for protocol upgrades. Report an error if data remains blocked
2128 * in the output buffer.
2129 */
2130 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2131 if (!htx_is_empty(chn_htx)) {
2132 h1c->flags |= H1C_F_ST_ERROR;
2133 TRACE_STATE("txn done but data waiting to be sent, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
2134 }
2135 h1s->cs->flags |= CS_FL_EOI;
2136 }
2137
Christopher Faulet6b81df72019-10-01 22:08:43 +02002138 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002139 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002140
2141 full:
2142 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2143 h1c->flags |= H1C_F_OUT_FULL;
2144 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002145}
2146
Christopher Faulet51dbc942018-09-13 09:05:15 +02002147/*********************************************************/
2148/* functions below are I/O callbacks from the connection */
2149/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002150static void h1_wake_stream_for_recv(struct h1s *h1s)
2151{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002152 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002153 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002154 tasklet_wakeup(h1s->subs->tasklet);
2155 h1s->subs->events &= ~SUB_RETRY_RECV;
2156 if (!h1s->subs->events)
2157 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002158 }
2159}
2160static void h1_wake_stream_for_send(struct h1s *h1s)
2161{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002162 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002163 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002164 tasklet_wakeup(h1s->subs->tasklet);
2165 h1s->subs->events &= ~SUB_RETRY_SEND;
2166 if (!h1s->subs->events)
2167 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002168 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002169}
2170
Christopher Fauletad4daf62021-01-21 17:49:01 +01002171/* alerts the data layer following this sequence :
2172 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2173 * - if its subscribed to send, then it's woken up for send
2174 * - if it was subscribed to neither, its ->wake() callback is called
2175 */
2176static void h1_alert(struct h1s *h1s)
2177{
2178 if (h1s->subs) {
2179 h1_wake_stream_for_recv(h1s);
2180 h1_wake_stream_for_send(h1s);
2181 }
2182 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2183 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2184 h1s->cs->data_cb->wake(h1s->cs);
2185 }
2186}
2187
Christopher Fauletc18fc232020-10-06 17:41:36 +02002188/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2189 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2190 * retryable errors (allocation error or buffer full). On success, the error is
2191 * copied in the output buffer.
2192*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002193static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002194{
2195 int rc = http_get_status_idx(h1c->errcode);
2196 int ret = 0;
2197
2198 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2199
2200 /* Verify if the error is mapped on /dev/null or any empty file */
2201 /// XXX: do a function !
2202 if (h1c->px->replies[rc] &&
2203 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2204 h1c->px->replies[rc]->body.errmsg &&
2205 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2206 /* Empty error, so claim a success */
2207 ret = 1;
2208 goto out;
2209 }
2210
2211 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2212 h1c->flags |= H1C_F_ERR_PENDING;
2213 goto out;
2214 }
2215
2216 if (!h1_get_buf(h1c, &h1c->obuf)) {
2217 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2218 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2219 goto out;
2220 }
2221 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2222 if (unlikely(ret <= 0)) {
2223 if (!ret) {
2224 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2225 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2226 goto out;
2227 }
2228 else {
2229 /* we cannot report this error, so claim a success */
2230 ret = 1;
2231 }
2232 }
2233 h1c->flags &= ~H1C_F_ERR_PENDING;
2234 out:
2235 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2236 return ret;
2237}
2238
2239/* Try to send a 500 internal error. It relies on h1_send_error to send the
2240 * error. This function takes care of incrementing stats and tracked counters.
2241 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002242static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002243{
2244 struct session *sess = h1c->conn->owner;
2245 int ret = 1;
2246
2247 session_inc_http_req_ctr(sess);
2248 session_inc_http_err_ctr(sess);
2249 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2250 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[5], 1);
2251 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
2252 if (sess->listener->counters)
2253 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
2254
2255 h1c->errcode = 500;
2256 ret = h1_send_error(h1c);
2257 sess_log(sess);
2258 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002259}
2260
Christopher Fauletc18fc232020-10-06 17:41:36 +02002261/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2262 * error. This function takes care of incrementing stats and tracked counters.
2263 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002264static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002265{
2266 struct session *sess = h1c->conn->owner;
2267 int ret = 1;
2268
2269 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2270 goto end;
2271
2272 session_inc_http_req_ctr(sess);
2273 session_inc_http_err_ctr(sess);
2274 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2275 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2276 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2277 if (sess->listener->counters)
2278 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2279
2280 h1c->errcode = 400;
2281 ret = h1_send_error(h1c);
2282 sess_log(sess);
2283
2284 end:
2285 return ret;
2286}
2287
Christopher Faulet2eed8002020-12-07 11:26:13 +01002288/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2289 * the error. This function takes care of incrementing stats and tracked
2290 * counters.
2291 */
2292static int h1_handle_not_impl_err(struct h1c *h1c)
2293{
2294 struct session *sess = h1c->conn->owner;
2295 int ret = 1;
2296
2297 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2298 goto end;
2299
2300 session_inc_http_req_ctr(sess);
2301 session_inc_http_err_ctr(sess);
2302 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2303 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2304 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2305 if (sess->listener->counters)
2306 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2307
2308 h1c->errcode = 501;
2309 ret = h1_send_error(h1c);
2310 sess_log(sess);
2311
2312 end:
2313 return ret;
2314}
2315
Christopher Fauletc18fc232020-10-06 17:41:36 +02002316/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2317 * error. This function takes care of incrementing stats and tracked counters.
2318 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002319static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002320{
2321 struct session *sess = h1c->conn->owner;
2322 int ret = 1;
2323
2324 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2325 goto end;
2326
2327 session_inc_http_req_ctr(sess);
2328 session_inc_http_err_ctr(sess);
2329 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2330 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2331 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2332 if (sess->listener->counters)
2333 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2334
2335 h1c->errcode = 408;
2336 ret = h1_send_error(h1c);
2337 sess_log(sess);
2338 end:
2339 return ret;
2340}
2341
2342
Christopher Faulet51dbc942018-09-13 09:05:15 +02002343/*
2344 * Attempt to read data, and subscribe if none available
2345 */
2346static int h1_recv(struct h1c *h1c)
2347{
2348 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002349 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002350 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002351
Christopher Faulet6b81df72019-10-01 22:08:43 +02002352 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2353
2354 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2355 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002356 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002357 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002358
Christopher Fauletae635762020-09-21 11:47:16 +02002359 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2360 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002361 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002362 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002363
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002364 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2365 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002366 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002367 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002368 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002369
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002370 /*
2371 * If we only have a small amount of data, realign it,
2372 * it's probably cheaper than doing 2 recv() calls.
2373 */
2374 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2375 b_slow_realign(&h1c->ibuf, trash.area, 0);
2376
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002377 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002378 if (!h1c->h1s ||
2379 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2380 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002381 flags |= CO_RFL_READ_ONCE;
2382
Willy Tarreau45f2b892018-12-05 07:59:27 +01002383 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002384 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002385 if (h1c->flags & H1C_F_IN_FULL) {
2386 h1c->flags &= ~H1C_F_IN_FULL;
2387 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2388 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002389
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002390 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002391 if (!b_data(&h1c->ibuf)) {
2392 /* try to pre-align the buffer like the rxbufs will be
2393 * to optimize memory copies.
2394 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002395 h1c->ibuf.head = sizeof(struct htx);
2396 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002397 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002398 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002399 if (max && !ret && h1_recv_allowed(h1c)) {
2400 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2401 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002402 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002403 else {
2404 h1_wake_stream_for_recv(h1c->h1s);
2405 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002406 }
2407
Christopher Faulet51dbc942018-09-13 09:05:15 +02002408 if (!b_data(&h1c->ibuf))
2409 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002410 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002411 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002412 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2413 }
2414
2415 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002416 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002417}
2418
2419
2420/*
2421 * Try to send data if possible
2422 */
2423static int h1_send(struct h1c *h1c)
2424{
2425 struct connection *conn = h1c->conn;
2426 unsigned int flags = 0;
2427 size_t ret;
2428 int sent = 0;
2429
Christopher Faulet6b81df72019-10-01 22:08:43 +02002430 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2431
2432 if (conn->flags & CO_FL_ERROR) {
2433 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002434 b_reset(&h1c->obuf);
2435 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002436 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002437
2438 if (!b_data(&h1c->obuf))
2439 goto end;
2440
Christopher Faulet46230362019-10-17 16:04:20 +02002441 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002442 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002443 if (h1c->flags & H1C_F_CO_STREAMER)
2444 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002445
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002446 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002448 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002449 if (h1c->flags & H1C_F_OUT_FULL) {
2450 h1c->flags &= ~H1C_F_OUT_FULL;
2451 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2452 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453 b_del(&h1c->obuf, ret);
2454 sent = 1;
2455 }
2456
Christopher Faulet145aa472018-12-06 10:56:20 +01002457 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002458 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002459 /* error or output closed, nothing to send, clear the buffer to release it */
2460 b_reset(&h1c->obuf);
2461 }
2462
Christopher Faulet51dbc942018-09-13 09:05:15 +02002463 end:
Christopher Fauletb385b502021-01-13 18:47:57 +01002464 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_WAIT_INPUT)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002465 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002466
Christopher Faulet51dbc942018-09-13 09:05:15 +02002467 /* We're done, no more to send */
2468 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002469 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002470 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002471 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002472 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002473 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002474 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002475 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002476 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2477 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002478 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002479 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002480
Christopher Faulet6b81df72019-10-01 22:08:43 +02002481 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002482 return sent;
2483}
2484
Christopher Faulet51dbc942018-09-13 09:05:15 +02002485/* callback called on any event by the connection handler.
2486 * It applies changes and returns zero, or < 0 if it wants immediate
2487 * destruction of the connection.
2488 */
2489static int h1_process(struct h1c * h1c)
2490{
2491 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002492 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002493
Christopher Faulet6b81df72019-10-01 22:08:43 +02002494 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2495
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002496 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002497 if (b_data(&h1c->ibuf) && /* Input data to be processed */
2498 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2499 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002500 struct buffer *buf;
2501 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002502
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002503 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2504 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002505 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002506
2507 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002508 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
2509 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE)) { /* H2 upgrade supported by the proxy */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002510 /* Try to match H2 preface before parsing the request headers. */
2511 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2512 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002513 if (h1c->flags & H1C_F_ST_ATTACHED) {
2514 /* Force the REOS here to be sure to release the CS.
2515 Here ATTACHED implies !READY, and h1s defined
2516 */
2517 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2518 h1s->flags |= H1S_F_REOS;
2519 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002520 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002521 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002522 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002523 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002524
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002525 /* Create the H1 stream if not already there */
2526 if (!h1s) {
2527 h1s = h1c_frt_stream_new(h1c);
2528 if (!h1s) {
2529 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002530 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002531 goto no_parsing;
2532 }
2533 }
2534
2535 if (h1s->sess->t_idle == -1)
2536 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2537
2538 /* Get the stream rxbuf */
2539 buf = h1_get_buf(h1c, &h1s->rxbuf);
2540 if (!buf) {
2541 h1c->flags |= H1C_F_IN_SALLOC;
2542 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2543 return 0;
2544 }
2545
2546 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2547 h1_process_input(h1c, buf, count);
2548 h1_release_buf(h1c, &h1s->rxbuf);
2549 h1_set_idle_expiration(h1c);
2550
2551 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002552 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002553 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002554 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002555 }
2556 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2557 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002558 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002559 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002560 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2561 h1_handle_not_impl_err(h1c);
2562 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
2563 }
Christopher Fauletae635762020-09-21 11:47:16 +02002564 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002565 h1_send(h1c);
2566
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002567 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002568 if (!(h1c->flags & H1C_F_ST_READY)) {
2569 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002570 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002571 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002572 if (h1_handle_bad_req(h1c))
2573 h1_send(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 }
2576
2577 /* Handle pending error, if any (only possible on frontend connection) */
2578 if (h1c->flags & H1C_F_ERR_PENDING) {
2579 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2580 if (h1_send_error(h1c))
2581 h1_send(h1c);
2582 }
Christopher Fauletae635762020-09-21 11:47:16 +02002583
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002584 /* If there is some pending outgoing data or error, just wait */
2585 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2586 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002587
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002588 /* Otherwise we can release the H1 connection */
2589 goto release;
2590 }
2591 else {
2592 /* Here there is still a H1 stream with a conn-stream.
2593 * Report the connection state at the stream level
2594 */
2595 if (conn_xprt_read0_pending(conn)) {
2596 h1s->flags |= H1S_F_REOS;
2597 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2598 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002599 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002600 h1s->cs->flags |= CS_FL_ERROR;
2601 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002602 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002603 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002604 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002605
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002606 if (!b_data(&h1c->ibuf))
2607 h1_release_buf(h1c, &h1c->ibuf);
2608
2609
2610 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2611 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2612 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002613 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002614
Christopher Faulet47365272018-10-31 17:40:50 +01002615 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002616 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002617 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002618 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002619
2620 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002621 if (h1c->flags & H1C_F_ST_ATTACHED) {
2622 /* Don't release the H1 connetion right now, we must destroy the
2623 * attached CS first. Here, the H1C must not be READY */
2624 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2625
2626 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2627 h1s->cs->flags |= CS_FL_EOS;
2628 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2629 h1s->cs->flags |= CS_FL_ERROR;
2630 h1_alert(h1s);
2631 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2632 }
2633 else {
2634 h1_release(h1c);
2635 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2636 }
Christopher Faulet539e0292018-11-19 10:40:09 +01002637 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002638}
2639
Willy Tarreau691d5032021-01-20 14:55:01 +01002640struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002641{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002642 struct connection *conn;
2643 struct tasklet *tl = (struct tasklet *)t;
2644 int conn_in_list;
2645 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002646 int ret = 0;
2647
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002648
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002649 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002650 if (tl->context == NULL) {
2651 /* The connection has been taken over by another thread,
2652 * we're no longer responsible for it, so just free the
2653 * tasklet, and do nothing.
2654 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002655 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002656 tasklet_free(tl);
2657 return NULL;
2658 }
2659 h1c = ctx;
2660 conn = h1c->conn;
2661
2662 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2663
2664 /* Remove the connection from the list, to be sure nobody attempts
2665 * to use it while we handle the I/O events
2666 */
2667 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2668 if (conn_in_list)
2669 MT_LIST_DEL(&conn->list);
2670
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002671 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002672
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002673 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002674 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002675 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002676 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002677 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002678 ret = h1_process(h1c);
2679 /* If we were in an idle list, we want to add it back into it,
2680 * unless h1_process() returned -1, which mean it has destroyed
2681 * the connection (testing !ret is enough, if h1_process() wasn't
2682 * called then ret will be 0 anyway.
2683 */
2684 if (!ret && conn_in_list) {
2685 struct server *srv = objt_server(conn->target);
2686
2687 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002688 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002689 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002690 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002691 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002692 return NULL;
2693}
2694
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002695static void h1_reset(struct connection *conn)
2696{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002697
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002698}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002699
2700static int h1_wake(struct connection *conn)
2701{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002702 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002703 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002704
Christopher Faulet6b81df72019-10-01 22:08:43 +02002705 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2706
Christopher Faulet539e0292018-11-19 10:40:09 +01002707 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002708 ret = h1_process(h1c);
2709 if (ret == 0) {
2710 struct h1s *h1s = h1c->h1s;
2711
Christopher Fauletad4daf62021-01-21 17:49:01 +01002712 if (h1c->flags & H1C_F_ST_ATTACHED)
2713 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002714 }
2715 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002716}
2717
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002718/* Connection timeout management. The principle is that if there's no receipt
2719 * nor sending for a certain amount of time, the connection is closed.
2720 */
2721static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2722{
2723 struct h1c *h1c = context;
2724 int expired = tick_is_expired(t->expire, now_ms);
2725
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002726 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002727
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002728 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002729 /* Make sure nobody stole the connection from us */
2730 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2731
2732 /* Somebody already stole the connection from us, so we should not
2733 * free it, we just have to free the task.
2734 */
2735 if (!t->context) {
2736 h1c = NULL;
2737 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2738 goto do_leave;
2739 }
2740
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002741 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002742 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2743 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002744 return t;
2745 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002746
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002747 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002748 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002749 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002750 if (h1c->flags & H1C_F_ST_READY) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002751 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2752 t->expire = TICK_ETERNITY;
2753 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2754 return t;
2755 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002756
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002757 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002758 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2759 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002760 if (h1_handle_req_tout(h1c))
2761 h1_send(h1c);
2762 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2763 h1_refresh_timeout(h1c);
Christopher Fauletcc043f62020-12-14 10:06:12 +01002764 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002765 return t;
2766 }
2767 }
2768
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002769 if (h1c->flags & H1C_F_ST_ATTACHED) {
2770 /* Don't release the H1 connetion right now, we must destroy the
2771 * attached CS first. Here, the H1C must not be READY */
2772 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
2773 h1_alert(h1c->h1s);
2774 h1_refresh_timeout(h1c);
2775 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2776 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2777 return t;
2778 }
2779
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002780 /* We're about to destroy the connection, so make sure nobody attempts
2781 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002782 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002783 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002784 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002785
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002786 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002787 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002788
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002789 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002790 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002791
2792 if (!h1c) {
2793 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002794 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002795 return NULL;
2796 }
2797
2798 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002799 h1_release(h1c);
2800 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002801 return NULL;
2802}
2803
Christopher Faulet51dbc942018-09-13 09:05:15 +02002804/*******************************************/
2805/* functions below are used by the streams */
2806/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002807
Christopher Faulet51dbc942018-09-13 09:05:15 +02002808/*
2809 * Attach a new stream to a connection
2810 * (Used for outgoing connections)
2811 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002812static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002813{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002814 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002815 struct conn_stream *cs = NULL;
2816 struct h1s *h1s;
2817
Christopher Faulet6b81df72019-10-01 22:08:43 +02002818 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002819 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002820 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 +02002821 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002822 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002823
Christopher Faulet236c93b2020-07-02 09:19:54 +02002824 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002825 if (!cs) {
2826 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 +02002827 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002828 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002829
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002830 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002831 if (h1s == NULL) {
2832 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 +02002833 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002834 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002835
Christopher Faulet6b81df72019-10-01 22:08:43 +02002836 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002837 return cs;
2838 end:
2839 cs_free(cs);
2840 return NULL;
2841}
2842
2843/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2844 * this mux, it's easy as we can only store a single conn_stream.
2845 */
2846static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2847{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002848 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002849 struct h1s *h1s = h1c->h1s;
2850
2851 if (h1s)
2852 return h1s->cs;
2853
2854 return NULL;
2855}
2856
Christopher Faulet73c12072019-04-08 11:23:22 +02002857static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002858{
Christopher Faulet73c12072019-04-08 11:23:22 +02002859 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002860
Christopher Faulet6b81df72019-10-01 22:08:43 +02002861 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002862 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002863 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002864}
2865
2866/*
2867 * Detach the stream from the connection and possibly release the connection.
2868 */
2869static void h1_detach(struct conn_stream *cs)
2870{
2871 struct h1s *h1s = cs->ctx;
2872 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002873 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002874 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002875
Christopher Faulet6b81df72019-10-01 22:08:43 +02002876 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2877
Christopher Faulet51dbc942018-09-13 09:05:15 +02002878 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002879 if (!h1s) {
2880 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002881 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002882 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002883
Olivier Houchardf502aca2018-12-14 19:42:40 +01002884 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002885 h1c = h1s->h1c;
2886 h1s->cs = NULL;
2887
Christopher Faulet42849b02020-10-05 11:35:17 +02002888 sess->accept_date = date;
2889 sess->tv_accept = now;
2890 sess->t_handshake = 0;
2891 sess->t_idle = -1;
2892
Olivier Houchard8a786902018-12-15 16:05:40 +01002893 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2894 h1s_destroy(h1s);
2895
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002896 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 +02002897 /* If there are any excess server data in the input buffer,
2898 * release it and close the connection ASAP (some data may
2899 * remain in the output buffer). This happens if a server sends
2900 * invalid responses. So in such case, we don't want to reuse
2901 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002902 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002903 if (b_data(&h1c->ibuf)) {
2904 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002905 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002906 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002907 goto release;
2908 }
Christopher Faulet03627242019-07-19 11:34:08 +02002909
Christopher Faulet08016ab2020-07-01 16:10:06 +02002910 if (h1c->conn->flags & CO_FL_PRIVATE) {
2911 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002912 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2913 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002914 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002915 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002916 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002917 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002918 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002919 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002920 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2921 goto end;
2922 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002923 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002924 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002925 if (h1c->conn->owner == sess)
2926 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002927 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002928 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002929 /* The server doesn't want it, let's kill the connection right away */
2930 h1c->conn->mux->destroy(h1c);
2931 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2932 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002933 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002934 /* At this point, the connection has been added to the
2935 * server idle list, so another thread may already have
2936 * hijacked it, so we can't do anything with it.
2937 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002938 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002939 }
2940 }
2941
Christopher Fauletf1204b82019-07-19 14:51:06 +02002942 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002943 /* 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 +01002944 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002945 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002946 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002947 !h1c->conn->owner) {
2948 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002949 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002950 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002951 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002952 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002953 /* If we have a new request, process it immediately or
2954 * subscribe for reads waiting for new data
2955 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01002956 if (unlikely(b_data(&h1c->ibuf))) {
2957 if (h1_process(h1c) == -1)
2958 goto end;
2959 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002960 else
2961 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2962 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002963 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002964 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002965 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002966 end:
2967 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002968}
2969
2970
2971static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2972{
2973 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002974 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002975
2976 if (!h1s)
2977 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002978 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002979
Christopher Faulet6b81df72019-10-01 22:08:43 +02002980 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2981
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002982 if (cs->flags & CS_FL_SHR)
2983 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002984 if (cs->flags & CS_FL_KILL_CONN) {
2985 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2986 goto do_shutr;
2987 }
2988 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2989 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002990 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002991 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002992
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002993 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
2994 /* Here attached is implicit because there is CS */
2995 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2996 goto end;
2997 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002998 if (h1s->flags & H1S_F_WANT_KAL) {
2999 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003000 goto end;
3001 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003002
Christopher Faulet7f366362019-04-08 10:51:20 +02003003 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003004 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
3005 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003006 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003007 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003008 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02003009 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003010 end:
3011 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003012}
3013
3014static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3015{
3016 struct h1s *h1s = cs->ctx;
3017 struct h1c *h1c;
3018
3019 if (!h1s)
3020 return;
3021 h1c = h1s->h1c;
3022
Christopher Faulet6b81df72019-10-01 22:08:43 +02003023 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3024
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003025 if (cs->flags & CS_FL_SHW)
3026 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003027 if (cs->flags & CS_FL_KILL_CONN) {
3028 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003029 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003030 }
3031 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3032 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3033 goto do_shutw;
3034 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003035
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003036 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3037 /* Here attached is implicit because there is CS */
3038 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3039 goto end;
3040 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003041 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3042 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003043 goto end;
3044 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003045
Christopher Faulet7f366362019-04-08 10:51:20 +02003046 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003047 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003048 if (!b_data(&h1c->obuf))
3049 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003050 end:
3051 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003052}
3053
Christopher Faulet666a0c42019-01-08 11:12:04 +01003054static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003055{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003056 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003057
Christopher Faulet6b81df72019-10-01 22:08:43 +02003058 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003059 conn_xprt_shutw(conn);
3060 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003061 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003062}
3063
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003064/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3065 * The <es> pointer is not allowed to differ from the one passed to the
3066 * subscribe() call. It always returns zero.
3067 */
3068static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003069{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003070 struct h1s *h1s = cs->ctx;
3071
3072 if (!h1s)
3073 return 0;
3074
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003075 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003076 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003077
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003078 es->events &= ~event_type;
3079 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003080 h1s->subs = NULL;
3081
3082 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003083 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003084
3085 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003086 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003087
Christopher Faulet51dbc942018-09-13 09:05:15 +02003088 return 0;
3089}
3090
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003091/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3092 * event subscriber <es> is not allowed to change from a previous call as long
3093 * as at least one event is still subscribed. The <event_type> must only be a
3094 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3095 * the conn_stream <cs> was already detached, in which case it will return -1.
3096 */
3097static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003098{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003099 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003100 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003101
3102 if (!h1s)
3103 return -1;
3104
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003105 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003106 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003107
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003108 es->events |= event_type;
3109 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003110
3111 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003112 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003113
3114
Christopher Faulet6b81df72019-10-01 22:08:43 +02003115 if (event_type & SUB_RETRY_SEND) {
3116 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003117 /*
3118 * If the conn_stream attempt to subscribe, and the
3119 * mux isn't subscribed to the connection, then it
3120 * probably means the connection wasn't established
3121 * yet, so we have to subscribe.
3122 */
3123 h1c = h1s->h1c;
3124 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3125 h1c->conn->xprt->subscribe(h1c->conn,
3126 h1c->conn->xprt_ctx,
3127 SUB_RETRY_SEND,
3128 &h1c->wait_event);
3129 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003130 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003131}
3132
3133/* Called from the upper layer, to receive data */
3134static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3135{
3136 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003137 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003138 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003139 size_t ret = 0;
3140
Willy Tarreau022e5e52020-09-10 09:33:15 +02003141 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003142
3143 /* Do nothing for now if not READY */
3144 if (!(h1c->flags & H1C_F_ST_READY)) {
3145 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3146 goto end;
3147 }
3148
Christopher Faulet539e0292018-11-19 10:40:09 +01003149 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02003150 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003151 else
3152 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003153
Christopher Faulete18777b2019-04-16 16:46:36 +02003154 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02003155 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02003156 h1c->flags |= H1C_F_WANT_SPLICE;
3157 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 +02003158 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003159 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003160 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003161 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003162 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003163 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003164
3165 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003166 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003167 return ret;
3168}
3169
3170
3171/* Called from the upper layer, to send data */
3172static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3173{
3174 struct h1s *h1s = cs->ctx;
3175 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003176 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003177
3178 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003179 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003180 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003181
Willy Tarreau022e5e52020-09-10 09:33:15 +02003182 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003183
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003184 /* If we're not connected yet, or we're waiting for a handshake, stop
3185 * now, as we don't want to remove everything from the channel buffer
3186 * before we're sure we can send it.
3187 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003188 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003189 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003190 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003191 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003192
Christopher Fauletdea24742021-01-22 15:12:30 +01003193 if (h1c->flags & H1C_F_ST_ERROR) {
3194 cs->flags |= CS_FL_ERROR;
3195 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);
3196 return 0;
3197 }
3198
Christopher Faulet46230362019-10-17 16:04:20 +02003199 /* Inherit some flags from the upper layer */
3200 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3201 if (flags & CO_SFL_MSG_MORE)
3202 h1c->flags |= H1C_F_CO_MSG_MORE;
3203 if (flags & CO_SFL_STREAMER)
3204 h1c->flags |= H1C_F_CO_STREAMER;
3205
Christopher Fauletc31872f2019-06-04 22:09:36 +02003206 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003207 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003208
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003209 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3210 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003211 else
3212 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003213
3214 if ((count - ret) > 0)
3215 h1c->flags |= H1C_F_CO_MSG_MORE;
3216
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003217 if (!ret)
3218 break;
3219 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003220 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003221 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003222 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003223 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003224
3225 if (h1c->flags & H1C_F_ST_ERROR) {
3226 TRACE_DEVEL("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
3227 cs->flags |= CS_FL_ERROR;
3228 }
3229
Christopher Faulet7a145d62020-08-05 11:31:16 +02003230 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003231 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003232 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003233}
3234
Willy Tarreaue5733232019-05-22 19:24:06 +02003235#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003236/* Send and get, using splicing */
3237static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3238{
3239 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003240 struct h1c *h1c = h1s->h1c;
3241 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003242 int ret = 0;
3243
Willy Tarreau022e5e52020-09-10 09:33:15 +02003244 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003245
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003246 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003247 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003248 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 +02003249 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003250 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003251 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003252 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003253 goto end;
3254 }
3255
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003256 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3257 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003258 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3259 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003260 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003261 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003262 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003263 }
3264
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003265 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003266 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3267 goto end;
3268 }
3269
Christopher Faulet1be55f92018-10-02 15:59:23 +02003270 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3271 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003272 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003273 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003274 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003275 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003276 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003277 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003278 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003279 }
3280
Christopher Faulet1be55f92018-10-02 15:59:23 +02003281 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003282 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003283 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003284 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003285 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003286 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003287
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003288 if ((h1s->flags & H1S_F_REOS) ||
3289 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003290 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003291 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003292 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003293 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003294
Willy Tarreau022e5e52020-09-10 09:33:15 +02003295 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003296 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003297}
3298
3299static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3300{
3301 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003302 int ret = 0;
3303
Willy Tarreau022e5e52020-09-10 09:33:15 +02003304 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003305
Christopher Faulet1be55f92018-10-02 15:59:23 +02003306 if (b_data(&h1s->h1c->obuf))
3307 goto end;
3308
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003309 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003310 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003311 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003312 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3313 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003314 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003315 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003316 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003317
Willy Tarreau022e5e52020-09-10 09:33:15 +02003318 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003319 return ret;
3320}
3321#endif
3322
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003323static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3324{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003325 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003326 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003327
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003328 switch (mux_ctl) {
3329 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003330 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003331 ret |= MUX_STATUS_READY;
3332 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003333 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003334 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3335 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003336 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3337 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3338 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003339 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003340 default:
3341 return -1;
3342 }
3343}
3344
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003345/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003346static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003347{
3348 struct h1c *h1c = conn->ctx;
3349 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003350 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003351
Christopher Fauletf376a312019-01-04 15:16:06 +01003352 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3353 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003354 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3355 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3356 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3357 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3358
3359 if (h1s) {
3360 char *method;
3361
3362 if (h1s->meth < HTTP_METH_OTHER)
3363 method = http_known_methods[h1s->meth].ptr;
3364 else
3365 method = "UNKNOWN";
3366 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3367 " .meth=%s status=%d",
3368 h1s, h1s->flags,
3369 h1m_state_str(h1s->req.state),
3370 h1m_state_str(h1s->res.state), method, h1s->status);
3371 if (h1s->cs)
3372 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3373 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003374
3375 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3376 if (h1s->subs) {
3377 if (h1s->subs) {
3378 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3379 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3380 h1s->subs->tasklet->calls,
3381 h1s->subs->tasklet->context);
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003382 if (h1s->subs->tasklet->calls >= 1000000)
3383 ret = 1;
Willy Tarreau150c4f82021-01-20 17:05:58 +01003384 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3385 chunk_appendf(&trash, ")");
3386 }
3387 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003388 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003389 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003390}
3391
3392
3393/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3394static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3395{
3396 struct h1_hdr_entry *entry;
3397
3398 /* Be sure there is a non-empty <to> */
3399 if (!strlen(to)) {
3400 memprintf(err, "expect <to>");
3401 return -1;
3402 }
3403
3404 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003405 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003406 memprintf(err, "<from> and <to> must not differ execpt the case");
3407 return -1;
3408 }
3409
3410 /* Be sure <from> does not already existsin the tree */
3411 if (ebis_lookup(&hdrs_map.map, from)) {
3412 memprintf(err, "duplicate entry '%s'", from);
3413 return -1;
3414 }
3415
3416 /* Create the entry and insert it in the tree */
3417 entry = malloc(sizeof(*entry));
3418 if (!entry) {
3419 memprintf(err, "out of memory");
3420 return -1;
3421 }
3422
3423 entry->node.key = strdup(from);
3424 entry->name.ptr = strdup(to);
3425 entry->name.len = strlen(to);
3426 if (!entry->node.key || !entry->name.ptr) {
3427 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003428 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003429 free(entry);
3430 memprintf(err, "out of memory");
3431 return -1;
3432 }
3433 ebis_insert(&hdrs_map.map, &entry->node);
3434 return 0;
3435}
3436
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003437/* Migrate the the connection to the current thread.
3438 * Return 0 if successful, non-zero otherwise.
3439 * Expected to be called with the old thread lock held.
3440 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003441static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003442{
3443 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003444 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003445
3446 if (fd_takeover(conn->handle.fd, conn) != 0)
3447 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003448
3449 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3450 /* We failed to takeover the xprt, even if the connection may
3451 * still be valid, flag it as error'd, as we have already
3452 * taken over the fd, and wake the tasklet, so that it will
3453 * destroy it.
3454 */
3455 conn->flags |= CO_FL_ERROR;
3456 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3457 return -1;
3458 }
3459
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003460 if (h1c->wait_event.events)
3461 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3462 h1c->wait_event.events, &h1c->wait_event);
3463 /* To let the tasklet know it should free itself, and do nothing else,
3464 * set its context to NULL.
3465 */
3466 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003467 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003468
3469 task = h1c->task;
3470 if (task) {
3471 task->context = NULL;
3472 h1c->task = NULL;
3473 __ha_barrier_store();
3474 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003475
3476 h1c->task = task_new(tid_bit);
3477 if (!h1c->task) {
3478 h1_release(h1c);
3479 return -1;
3480 }
3481 h1c->task->process = h1_timeout_task;
3482 h1c->task->context = h1c;
3483 }
3484 h1c->wait_event.tasklet = tasklet_new();
3485 if (!h1c->wait_event.tasklet) {
3486 h1_release(h1c);
3487 return -1;
3488 }
3489 h1c->wait_event.tasklet->process = h1_io_cb;
3490 h1c->wait_event.tasklet->context = h1c;
3491 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3492 SUB_RETRY_RECV, &h1c->wait_event);
3493
3494 return 0;
3495}
3496
3497
Christopher Faulet98fbe952019-07-22 16:18:24 +02003498static void h1_hdeaders_case_adjust_deinit()
3499{
3500 struct ebpt_node *node, *next;
3501 struct h1_hdr_entry *entry;
3502
3503 node = ebpt_first(&hdrs_map.map);
3504 while (node) {
3505 next = ebpt_next(node);
3506 ebpt_delete(node);
3507 entry = container_of(node, struct h1_hdr_entry, node);
3508 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003509 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003510 free(entry);
3511 node = next;
3512 }
3513 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003514}
3515
Christopher Faulet98fbe952019-07-22 16:18:24 +02003516static int cfg_h1_headers_case_adjust_postparser()
3517{
3518 FILE *file = NULL;
3519 char *c, *key_beg, *key_end, *value_beg, *value_end;
3520 char *err;
3521 int rc, line = 0, err_code = 0;
3522
3523 if (!hdrs_map.name)
3524 goto end;
3525
3526 file = fopen(hdrs_map.name, "r");
3527 if (!file) {
3528 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3529 hdrs_map.name);
3530 err_code |= ERR_ALERT | ERR_FATAL;
3531 goto end;
3532 }
3533
3534 /* now parse all lines. The file may contain only two header name per
3535 * line, separated by spaces. All heading and trailing spaces will be
3536 * ignored. Lines starting with a # are ignored.
3537 */
3538 while (fgets(trash.area, trash.size, file) != NULL) {
3539 line++;
3540 c = trash.area;
3541
3542 /* strip leading spaces and tabs */
3543 while (*c == ' ' || *c == '\t')
3544 c++;
3545
3546 /* ignore emptu lines, or lines beginning with a dash */
3547 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3548 continue;
3549
3550 /* look for the end of the key */
3551 key_beg = c;
3552 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3553 c++;
3554 key_end = c;
3555
3556 /* strip middle spaces and tabs */
3557 while (*c == ' ' || *c == '\t')
3558 c++;
3559
3560 /* look for the end of the value, it is the end of the line */
3561 value_beg = c;
3562 while (*c && *c != '\n' && *c != '\r')
3563 c++;
3564 value_end = c;
3565
3566 /* trim possibly trailing spaces and tabs */
3567 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3568 value_end--;
3569
3570 /* set final \0 and check entries */
3571 *key_end = '\0';
3572 *value_end = '\0';
3573
3574 err = NULL;
3575 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3576 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003577 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3578 hdrs_map.name, err, line);
3579 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003580 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003581 goto end;
3582 }
3583 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003584 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3585 hdrs_map.name, err, line);
3586 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003587 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003588 }
3589 }
3590
3591 end:
3592 if (file)
3593 fclose(file);
3594 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3595 return err_code;
3596}
3597
3598
3599/* config parser for global "h1-outgoing-header-case-adjust" */
3600static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3601 struct proxy *defpx, const char *file, int line,
3602 char **err)
3603{
3604 if (too_many_args(2, args, err, NULL))
3605 return -1;
3606 if (!*(args[1]) || !*(args[2])) {
3607 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3608 return -1;
3609 }
3610 return add_hdr_case_adjust(args[1], args[2], err);
3611}
3612
3613/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3614static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3615 struct proxy *defpx, const char *file, int line,
3616 char **err)
3617{
3618 if (too_many_args(1, args, err, NULL))
3619 return -1;
3620 if (!*(args[1])) {
3621 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3622 return -1;
3623 }
3624 free(hdrs_map.name);
3625 hdrs_map.name = strdup(args[1]);
3626 return 0;
3627}
3628
3629
3630/* config keyword parsers */
3631static struct cfg_kw_list cfg_kws = {{ }, {
3632 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3633 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3634 { 0, NULL, NULL },
3635 }
3636};
3637
3638INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3639REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3640
3641
Christopher Faulet51dbc942018-09-13 09:05:15 +02003642/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003643/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003644/****************************************/
3645
3646/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003647static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003648 .init = h1_init,
3649 .wake = h1_wake,
3650 .attach = h1_attach,
3651 .get_first_cs = h1_get_first_cs,
3652 .detach = h1_detach,
3653 .destroy = h1_destroy,
3654 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003655 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003656 .rcv_buf = h1_rcv_buf,
3657 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003658#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003659 .rcv_pipe = h1_rcv_pipe,
3660 .snd_pipe = h1_snd_pipe,
3661#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003662 .subscribe = h1_subscribe,
3663 .unsubscribe = h1_unsubscribe,
3664 .shutr = h1_shutr,
3665 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003666 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003667 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003668 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003669 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003670 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003671 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003672};
3673
3674
3675/* this mux registers default HTX proto */
3676static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003677{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003678
Willy Tarreau0108d902018-11-25 19:14:37 +01003679INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3680
Christopher Faulet51dbc942018-09-13 09:05:15 +02003681/*
3682 * Local variables:
3683 * c-indent-level: 8
3684 * c-basic-offset: 8
3685 * End:
3686 */