blob: 7b99f1a79c14de45e48496b95ed9aa3db1ef0882 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Willy Tarreaub2551052020-06-09 09:07:15 +020012#include <import/ebistree.h>
13
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020016#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020017#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020018#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020019#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020021#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/istbuf.h>
23#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020024#include <haproxy/pipe-t.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020025#include <haproxy/proxy-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020026#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020029#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030
31/*
32 * H1 Connection flags (32 bits)
33 */
34#define H1C_F_NONE 0x00000000
35
36/* Flags indicating why writing output data are blocked */
37#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
38#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
39/* 0x00000004 - 0x00000008 unused */
40
41/* Flags indicating why reading input data are blocked. */
42#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
43#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletd17ad822020-09-24 15:14:29 +020044#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010045/* 0x00000080 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010048#define H1C_F_ST_EMBRYONIC 0x00000100 /* Set when a H1 stream with no conn-stream is attached to the connection */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010049#define H1C_F_ST_ATTACHED 0x00000200 /* Set when a H1 stream with a conn-stream is attached to the connection (may be not READY) */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010050#define H1C_F_ST_IDLE 0x00000400 /* connection is idle and may be reused
51 * (exclusive to all H1C_F_ST flags and never set when an h1s is attached) */
52#define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
53#define H1C_F_ST_SHUTDOWN 0x00001000 /* connection must be shut down ASAP flushing output first (conn-stream may still be attached) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010054#define H1C_F_ST_READY 0x00002000 /* Set in ATTACHED state with a READY conn-stream. A conn-stream is not ready when
55 * a TCP>H1 upgrade is in progress Thus this flag is only set if ATTACHED is also set */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010056#define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED)
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010057/* 0x00004000 - 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020058
Christopher Fauletb385b502021-01-13 18:47:57 +010059#define H1C_F_WANT_SPLICE 0x00010000 /* Don't read into a buffer because we want to use or we are using splicing */
60#define H1C_F_ERR_PENDING 0x00020000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
61#define H1C_F_WAIT_NEXT_REQ 0x00040000 /* waiting for the next request to start, use keep-alive timeout */
62#define H1C_F_UPG_H2C 0x00080000 /* set if an upgrade to h2 should be done */
63#define H1C_F_CO_MSG_MORE 0x00100000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
64#define H1C_F_CO_STREAMER 0x00200000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
65#define H1C_F_WAIT_OUTPUT 0x00400000 /* Don't read more data for now, waiting sync with output side */
66#define H1C_F_WAIT_INPUT 0x00800000 /* Don't send more data for now, waiting sync with input side */
Christopher Faulet129817b2018-09-20 16:14:40 +020067
Christopher Fauletb385b502021-01-13 18:47:57 +010068/* 0x01000000 - 0x40000000 unusued*/
Christopher Faulet3ced1d12020-11-27 16:44:01 +010069#define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */
Christopher Faulet46230362019-10-17 16:04:20 +020070
Christopher Faulet51dbc942018-09-13 09:05:15 +020071/*
72 * H1 Stream flags (32 bits)
73 */
Christopher Faulet129817b2018-09-20 16:14:40 +020074#define H1S_F_NONE 0x00000000
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050075/* 0x00000001..0x00000004 unused */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020076#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020077#define H1S_F_WANT_KAL 0x00000010
78#define H1S_F_WANT_TUN 0x00000020
79#define H1S_F_WANT_CLO 0x00000040
80#define H1S_F_WANT_MSK 0x00000070
81#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet5696f542020-12-02 16:08:38 +010082#define H1S_F_BODYLESS_RESP 0x00000100 /* Bodyless response message */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020083
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010084/* 0x00000200 unsued */
Christopher Faulet2eed8002020-12-07 11:26:13 +010085#define H1S_F_NOT_IMPL_ERROR 0x00000400 /* Set when a feature is not implemented during the message parsing */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020086#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
87#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
88#define H1S_F_ERROR 0x00001800 /* stream error mask */
89
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020090#define H1S_F_HAVE_SRV_NAME 0x00002000 /* Set during output process if the server name header was added to the request */
Christopher Fauletada34b62019-05-24 16:36:43 +020091#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020092
93/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020094struct h1c {
95 struct connection *conn;
96 struct proxy *px;
97 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +020098 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +020099 struct buffer ibuf; /* Input buffer to store data before parsing */
100 struct buffer obuf; /* Output buffer to store data after reformatting */
101
102 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
103 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
104
105 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100106 struct task *task; /* timeout management task */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200107 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
108 int timeout; /* client/server timeout duration */
109 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200110};
111
112/* H1 stream descriptor */
113struct h1s {
114 struct h1c *h1c;
115 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100116 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200117
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100118 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200119
Olivier Houchardf502aca2018-12-14 19:42:40 +0100120 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200121 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200122 struct h1m req;
123 struct h1m res;
124
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500125 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200126 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200127};
128
Christopher Faulet98fbe952019-07-22 16:18:24 +0200129/* Map of headers used to convert outgoing headers */
130struct h1_hdrs_map {
131 char *name;
132 struct eb_root map;
133};
134
135/* An entry in a headers map */
136struct h1_hdr_entry {
137 struct ist name;
138 struct ebpt_node node;
139};
140
141/* Declare the headers map */
142static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
143
144
Christopher Faulet6b81df72019-10-01 22:08:43 +0200145/* trace source and events */
146static void h1_trace(enum trace_level level, uint64_t mask,
147 const struct trace_source *src,
148 const struct ist where, const struct ist func,
149 const void *a1, const void *a2, const void *a3, const void *a4);
150
151/* The event representation is split like this :
152 * h1c - internal H1 connection
153 * h1s - internal H1 stream
154 * strm - application layer
155 * rx - data receipt
156 * tx - data transmission
157 *
158 */
159static const struct trace_event h1_trace_events[] = {
160#define H1_EV_H1C_NEW (1ULL << 0)
161 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
162#define H1_EV_H1C_RECV (1ULL << 1)
163 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
164#define H1_EV_H1C_SEND (1ULL << 2)
165 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
166#define H1_EV_H1C_BLK (1ULL << 3)
167 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
168#define H1_EV_H1C_WAKE (1ULL << 4)
169 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
170#define H1_EV_H1C_END (1ULL << 5)
171 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
172#define H1_EV_H1C_ERR (1ULL << 6)
173 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
174
175#define H1_EV_RX_DATA (1ULL << 7)
176 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
177#define H1_EV_RX_EOI (1ULL << 8)
178 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
179#define H1_EV_RX_HDRS (1ULL << 9)
180 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
181#define H1_EV_RX_BODY (1ULL << 10)
182 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
183#define H1_EV_RX_TLRS (1ULL << 11)
184 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
185
186#define H1_EV_TX_DATA (1ULL << 12)
187 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
188#define H1_EV_TX_EOI (1ULL << 13)
189 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
190#define H1_EV_TX_HDRS (1ULL << 14)
191 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
192#define H1_EV_TX_BODY (1ULL << 15)
193 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
194#define H1_EV_TX_TLRS (1ULL << 16)
195 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
196
197#define H1_EV_H1S_NEW (1ULL << 17)
198 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
199#define H1_EV_H1S_BLK (1ULL << 18)
200 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
201#define H1_EV_H1S_END (1ULL << 19)
202 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
203#define H1_EV_H1S_ERR (1ULL << 20)
204 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
205
206#define H1_EV_STRM_NEW (1ULL << 21)
207 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
208#define H1_EV_STRM_RECV (1ULL << 22)
209 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
210#define H1_EV_STRM_SEND (1ULL << 23)
211 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
212#define H1_EV_STRM_WAKE (1ULL << 24)
213 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
214#define H1_EV_STRM_SHUT (1ULL << 25)
215 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
216#define H1_EV_STRM_END (1ULL << 26)
217 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
218#define H1_EV_STRM_ERR (1ULL << 27)
219 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
220
221 { }
222};
223
224static const struct name_desc h1_trace_lockon_args[4] = {
225 /* arg1 */ { /* already used by the connection */ },
226 /* arg2 */ { .name="h1s", .desc="H1 stream" },
227 /* arg3 */ { },
228 /* arg4 */ { }
229};
230
231static const struct name_desc h1_trace_decoding[] = {
232#define H1_VERB_CLEAN 1
233 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
234#define H1_VERB_MINIMAL 2
235 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
236#define H1_VERB_SIMPLE 3
237 { .name="simple", .desc="add request/response status line or htx info when available" },
238#define H1_VERB_ADVANCED 4
239 { .name="advanced", .desc="add header fields or frame decoding when available" },
240#define H1_VERB_COMPLETE 5
241 { .name="complete", .desc="add full data dump when available" },
242 { /* end */ }
243};
244
245static struct trace_source trace_h1 = {
246 .name = IST("h1"),
247 .desc = "HTTP/1 multiplexer",
248 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
249 .default_cb = h1_trace,
250 .known_events = h1_trace_events,
251 .lockon_args = h1_trace_lockon_args,
252 .decoding = h1_trace_decoding,
253 .report_events = ~0, // report everything by default
254};
255
256#define TRACE_SOURCE &trace_h1
257INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
258
Christopher Faulet51dbc942018-09-13 09:05:15 +0200259/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100260DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
261DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200262
Christopher Faulet51dbc942018-09-13 09:05:15 +0200263static int h1_recv(struct h1c *h1c);
264static int h1_send(struct h1c *h1c);
265static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100266/* h1_io_cb is exported to see it resolved in "show fd" */
267struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100268static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200269static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200270static void h1_wake_stream_for_recv(struct h1s *h1s);
271static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200272
Christopher Faulet6b81df72019-10-01 22:08:43 +0200273/* the H1 traces always expect that arg1, if non-null, is of type connection
274 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
275 * that arg3, if non-null, is a htx for rx/tx headers.
276 */
277static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
278 const struct ist where, const struct ist func,
279 const void *a1, const void *a2, const void *a3, const void *a4)
280{
281 const struct connection *conn = a1;
282 const struct h1c *h1c = conn ? conn->ctx : NULL;
283 const struct h1s *h1s = a2;
284 const struct htx *htx = a3;
285 const size_t *val = a4;
286
287 if (!h1c)
288 h1c = (h1s ? h1s->h1c : NULL);
289
290 if (!h1c || src->verbosity < H1_VERB_CLEAN)
291 return;
292
293 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200294 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200295
296 /* Display request and response states if h1s is defined */
297 if (h1s)
298 chunk_appendf(&trace_buf, " [%s, %s]",
299 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
300
301 if (src->verbosity == H1_VERB_CLEAN)
302 return;
303
304 /* Display the value to the 4th argument (level > STATE) */
305 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100306 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200307
308 /* Display status-line if possible (verbosity > MINIMAL) */
309 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
310 const struct htx_blk *blk = htx_get_head_blk(htx);
311 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
312 enum htx_blk_type type = htx_get_blk_type(blk);
313
314 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
315 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
316 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
317 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
318 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
319 }
320
321 /* Display h1c info and, if defined, h1s info (pointer + flags) */
322 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
323 if (h1s)
324 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
325
326 if (src->verbosity == H1_VERB_MINIMAL)
327 return;
328
329 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
330 if (src->level > TRACE_LEVEL_USER) {
331 if (src->verbosity == H1_VERB_COMPLETE ||
332 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
333 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
334 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
335 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
336 if (src->verbosity == H1_VERB_COMPLETE ||
337 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
338 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
339 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
340 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
341 }
342
343 /* Display htx info if defined (level > USER) */
344 if (src->level > TRACE_LEVEL_USER && htx) {
345 int full = 0;
346
347 /* Full htx info (level > STATE && verbosity > SIMPLE) */
348 if (src->level > TRACE_LEVEL_STATE) {
349 if (src->verbosity == H1_VERB_COMPLETE)
350 full = 1;
351 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
352 full = 1;
353 }
354
355 chunk_memcat(&trace_buf, "\n\t", 2);
356 htx_dump(&trace_buf, htx, full);
357 }
358}
359
360
Christopher Faulet51dbc942018-09-13 09:05:15 +0200361/*****************************************************/
362/* functions below are for dynamic buffer management */
363/*****************************************************/
364/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100365 * Indicates whether or not we may receive data. The rules are the following :
366 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200367 * must not attempt to receive
368 * - if we are waiting for the connection establishment, we must not attempt
369 * to receive
370 * - if an error was detected on the stream we must not attempt to receive
371 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100372 * - if the input buffer failed to be allocated or is full , we must not try
373 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200374 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200375 * - otherwise must may not attempt to receive
376 */
377static inline int h1_recv_allowed(const struct h1c *h1c)
378{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100379 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100380 TRACE_DEVEL("recv not allowed because of error on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200382 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200383
Willy Tarreau2febb842020-07-31 09:15:43 +0200384 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
385 TRACE_DEVEL("recv not allowed because of (error|read0|waitl4|waitl6) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletcf56b992018-12-11 16:12:31 +0100386 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200387 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100388
Christopher Faulet119ac872020-09-30 17:33:22 +0200389 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
390 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
391 return 0;
392 }
393
Christopher Fauletb385b502021-01-13 18:47:57 +0100394 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
395 TRACE_DEVEL("recv not allowed (wait_output)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200396 return 0;
397 }
398
Christopher Fauletd17ad822020-09-24 15:14:29 +0200399 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200400 return 1;
401
Christopher Faulet6b81df72019-10-01 22:08:43 +0200402 TRACE_DEVEL("recv not allowed because input is blocked", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200403 return 0;
404}
405
406/*
407 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
408 * flags are used to figure what buffer was requested. It returns 1 if the
409 * allocation succeeds, in which case the connection is woken up, or 0 if it's
410 * impossible to wake up and we prefer to be woken up later.
411 */
412static int h1_buf_available(void *target)
413{
414 struct h1c *h1c = target;
415
416 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200417 TRACE_STATE("unblocking h1c, ibuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200418 h1c->flags &= ~H1C_F_IN_ALLOC;
419 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200420 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200421 return 1;
422 }
423
424 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200425 TRACE_STATE("unblocking h1s, obuf allocated", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200427 if (h1c->h1s)
428 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200429 return 1;
430 }
431
Christopher Fauletd17ad822020-09-24 15:14:29 +0200432 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
433 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
434 h1c->flags &= ~H1C_F_IN_SALLOC;
435 tasklet_wakeup(h1c->wait_event.tasklet);
436 return 1;
437 }
438
Christopher Faulet51dbc942018-09-13 09:05:15 +0200439 return 0;
440}
441
442/*
443 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
444 */
445static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
446{
447 struct buffer *buf = NULL;
448
Willy Tarreau21046592020-02-26 10:39:36 +0100449 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200450 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
451 h1c->buf_wait.target = h1c;
452 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200453 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200454 }
455 return buf;
456}
457
458/*
459 * Release a buffer, if any, and try to wake up entities waiting in the buffer
460 * wait queue.
461 */
462static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
463{
464 if (bptr->size) {
465 b_free(bptr);
466 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
467 }
468}
469
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100470/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100471 * or not. We rely on H1C_F_ST_IDLE to know if the connection is in-use or
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100472 * not. This flag is only set when no H1S is attached and when the previous
473 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100474 */
475static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200476{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100477 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200478
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100479 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200480}
481
Willy Tarreau00f18a32019-01-26 12:19:01 +0100482/* returns the number of streams still available on a connection */
483static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100484{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100485 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100486}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200487
Christopher Faulet7a145d62020-08-05 11:31:16 +0200488/* Refresh the h1c task timeout if necessary */
489static void h1_refresh_timeout(struct h1c *h1c)
490{
491 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100492 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200493 /* half-closed or dead connections : switch to clientfin/serverfin
494 * timeouts so that we don't hang too long on clients that have
495 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200496 */
497 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200498 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
499 }
500 else if (b_data(&h1c->obuf)) {
501 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200502 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200503 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
504 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100505 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
506 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200507 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100508 TRACE_DEVEL("refreshing connection's timeout (alive front h1c but not ready)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200509 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200510 else {
511 /* alive back connections of front connections with a conn-stream attached */
512 h1c->task->expire = TICK_ETERNITY;
513 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
514 }
515
Christopher Fauletdbe57792020-10-05 17:50:58 +0200516 /* Finally set the idle expiration date if shorter */
517 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200518 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
519 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200520 }
521}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200522
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200523static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200524{
525 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
526 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
527 h1c->idle_exp = TICK_ETERNITY;
528 return;
529 }
530
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100531 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200532 if (!tick_isset(h1c->idle_exp)) {
533 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
534 !b_data(&h1c->ibuf) && /* No input data */
535 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
536 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
537 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
538 }
539 else {
540 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
541 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
542 }
543 }
544 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100545 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200546 if (!tick_isset(h1c->idle_exp)) {
547 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
548 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
549 }
550 }
551 else { // CS_ATTACHED or SHUTDOWN
552 h1c->idle_exp = TICK_ETERNITY;
553 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
554 }
555}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200556/*****************************************************************/
557/* functions below are dedicated to the mux setup and management */
558/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200559
560/* returns non-zero if there are input data pending for stream h1s. */
561static inline size_t h1s_data_pending(const struct h1s *h1s)
562{
563 const struct h1m *h1m;
564
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200565 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100566 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200567}
568
Christopher Faulet16df1782020-12-04 16:47:41 +0100569/* Creates a new conn-stream and the associate stream. <input> is used as input
570 * buffer for the stream. On success, it is transferred to the stream and the
571 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
572 * mux must still take care of it. However, there is nothing special to do
573 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
574 * b_free() on it is always safe. This function returns the conn-stream on
575 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200576static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100577{
578 struct conn_stream *cs;
579
Christopher Faulet6b81df72019-10-01 22:08:43 +0200580 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200581 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200582 if (!cs) {
583 TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100584 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200585 }
Christopher Faulet47365272018-10-31 17:40:50 +0100586 h1s->cs = cs;
587 cs->ctx = h1s;
588
589 if (h1s->flags & H1S_F_NOT_FIRST)
590 cs->flags |= CS_FL_NOT_FIRST;
591
Christopher Faulet27182292020-07-03 15:08:49 +0200592 if (global.tune.options & GTUNE_USE_SPLICE) {
593 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100594 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200595 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100596
Christopher Faulet26256f82020-09-14 11:40:13 +0200597 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200598 TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100599 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200600 }
601
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100602 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200603 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100604 return cs;
605
606 err:
607 cs_free(cs);
608 h1s->cs = NULL;
609 return NULL;
610}
611
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100612static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
613{
614 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
615
616 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
617 TRACE_DEVEL("leaving on stream upgrade failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
618 goto err;
619 }
620
621 if (global.tune.options & GTUNE_USE_SPLICE) {
622 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
623 h1s->cs->flags |= CS_FL_MAY_SPLICE;
624 }
625
626 h1s->h1c->flags |= H1C_F_ST_READY;
627 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
628 return h1s->cs;
629
630 err:
631 return NULL;
632}
633
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200634static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200635{
636 struct h1s *h1s;
637
Christopher Faulet6b81df72019-10-01 22:08:43 +0200638 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
639
Christopher Faulet51dbc942018-09-13 09:05:15 +0200640 h1s = pool_alloc(pool_head_h1s);
641 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100642 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200643 h1s->h1c = h1c;
644 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200645 h1s->sess = NULL;
646 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100647 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100648 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200649 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200650
651 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100652 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200653
Christopher Faulet129817b2018-09-20 16:14:40 +0200654 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100655 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200656
657 h1s->status = 0;
658 h1s->meth = HTTP_METH_OTHER;
659
Christopher Faulet47365272018-10-31 17:40:50 +0100660 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
661 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100662 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100663
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200664 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
665 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200666
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200667 fail:
668 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
669 return NULL;
670}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100671
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200672static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
673{
674 struct session *sess = h1c->conn->owner;
675 struct h1s *h1s;
676
677 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
678
679 h1s = h1s_new(h1c);
680 if (!h1s)
681 goto fail;
682
683 h1s->sess = sess;
684
685 if (h1c->px->options2 & PR_O2_REQBUG_OK)
686 h1s->req.err_pos = -1;
687
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200688 h1c->idle_exp = TICK_ETERNITY;
689 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200690 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200691 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100692
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200693 fail:
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200694 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
695 return NULL;
696}
697
698static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
699{
700 struct h1s *h1s;
701
702 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
703
704 h1s = h1s_new(h1c);
705 if (!h1s)
706 goto fail;
707
708 h1s->cs = cs;
709 h1s->sess = sess;
710 cs->ctx = h1s;
711
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100712 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200713
714 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
715 h1s->res.err_pos = -1;
716
717 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
718 return h1s;
719
720 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200721 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100722 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200723}
724
725static void h1s_destroy(struct h1s *h1s)
726{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200727 if (h1s) {
728 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200729
Christopher Faulet6b81df72019-10-01 22:08:43 +0200730 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200731 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200732
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100733 if (h1s->subs)
734 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200735
Christopher Fauletd17ad822020-09-24 15:14:29 +0200736 h1_release_buf(h1c, &h1s->rxbuf);
737
Christopher Fauletb385b502021-01-13 18:47:57 +0100738 h1c->flags &= ~(H1C_F_WAIT_INPUT|H1C_F_WAIT_OUTPUT|H1C_F_WANT_SPLICE|
739 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200740 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
741 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200742 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100743 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200744 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
745 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100746
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100747 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100748 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100749 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100750 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100751 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100752 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
753 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200754 else {
755 TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100756 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200757 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200758 pool_free(pool_head_h1s, h1s);
759 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200760}
761
762/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200763 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500764 * to the existing conn_stream (for outgoing connections or for incoming ones
765 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200766 * establishment). <input> is always used as Input buffer and may contain
767 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
768 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200769 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200770static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
771 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200772{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200773 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100774 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200775 void *conn_ctx = conn->ctx;
776
777 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200778
779 h1c = pool_alloc(pool_head_h1c);
780 if (!h1c)
781 goto fail_h1c;
782 h1c->conn = conn;
783 h1c->px = proxy;
784
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100785 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200786 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200787 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200788 h1c->obuf = BUF_NULL;
789 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200790 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200791
Willy Tarreau21046592020-02-26 10:39:36 +0100792 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200793 h1c->wait_event.tasklet = tasklet_new();
794 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200795 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200796 h1c->wait_event.tasklet->process = h1_io_cb;
797 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100798 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200799 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200800
Christopher Faulete9b70722019-04-08 10:46:02 +0200801 if (conn_is_back(conn)) {
Christopher Fauletb385b502021-01-13 18:47:57 +0100802 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OUTPUT);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100803 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
804 if (tick_isset(proxy->timeout.serverfin))
805 h1c->shut_timeout = proxy->timeout.serverfin;
806 } else {
807 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
808 if (tick_isset(proxy->timeout.clientfin))
809 h1c->shut_timeout = proxy->timeout.clientfin;
810 }
811 if (tick_isset(h1c->timeout)) {
812 t = task_new(tid_bit);
813 if (!t)
814 goto fail;
815
816 h1c->task = t;
817 t->process = h1_timeout_task;
818 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200819
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100820 t->expire = tick_add(now_ms, h1c->timeout);
821 }
822
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100823 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200824
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200825 if (h1c->flags & H1C_F_IS_BACK) {
826 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200827 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
828 goto fail;
829 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100830 else if (conn_ctx) {
831 /* Upgraded frontend connection (from TCP) */
832 struct conn_stream *cs = conn_ctx;
833
834 if (!h1c_frt_stream_new(h1c))
835 goto fail;
836
837 h1c->h1s->cs = cs;
838 cs->ctx = h1c->h1s;
839
840 /* Attach the CS but Not ready yet */
841 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
842 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
843 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
844 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100845
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200846 if (t) {
847 h1_set_idle_expiration(h1c);
848 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100849 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200850 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100851
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200852 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100853 if (b_data(&h1c->ibuf))
854 tasklet_wakeup(h1c->wait_event.tasklet);
855 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200856 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200857
858 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200859 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200860 return 0;
861
862 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200863 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200864 if (h1c->wait_event.tasklet)
865 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200866 pool_free(pool_head_h1c, h1c);
867 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200868 conn->ctx = conn_ctx; // restore saved context
869 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200870 return -1;
871}
872
Christopher Faulet73c12072019-04-08 11:23:22 +0200873/* release function. This one should be called to free all resources allocated
874 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200875 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200876static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200877{
Christopher Faulet61840e72019-04-15 09:33:32 +0200878 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200879
Christopher Faulet6b81df72019-10-01 22:08:43 +0200880 TRACE_POINT(H1_EV_H1C_END);
881
Christopher Faulet51dbc942018-09-13 09:05:15 +0200882 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200883 /* The connection must be aattached to this mux to be released */
884 if (h1c->conn && h1c->conn->ctx == h1c)
885 conn = h1c->conn;
886
Christopher Faulet6b81df72019-10-01 22:08:43 +0200887 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
888
Christopher Faulet61840e72019-04-15 09:33:32 +0200889 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200890 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200891 /* Make sure we're no longer subscribed to anything */
892 if (h1c->wait_event.events)
893 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
894 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200895 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200896 /* connection successfully upgraded to H2, this
897 * mux was already released */
898 return;
899 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200900 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200901 sess_log(conn->owner); /* Log if the upgrade failed */
902 }
903
Olivier Houchard45c44372019-06-11 14:06:23 +0200904
Willy Tarreau21046592020-02-26 10:39:36 +0100905 if (MT_LIST_ADDED(&h1c->buf_wait.list))
906 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200907
908 h1_release_buf(h1c, &h1c->ibuf);
909 h1_release_buf(h1c, &h1c->obuf);
910
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100911 if (h1c->task) {
912 h1c->task->context = NULL;
913 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
914 h1c->task = NULL;
915 }
916
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200917 if (h1c->wait_event.tasklet)
918 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200919
Christopher Fauletf2824e62018-10-01 12:12:37 +0200920 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200921 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100922 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200923 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200924 pool_free(pool_head_h1c, h1c);
925 }
926
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200927 if (conn) {
928 conn->mux = NULL;
929 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200930 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200931
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200932 conn_stop_tracking(conn);
933 conn_full_close(conn);
934 if (conn->destroy_cb)
935 conn->destroy_cb(conn);
936 conn_free(conn);
937 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200938}
939
940/******************************************************/
941/* functions below are for the H1 protocol processing */
942/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200943/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
944 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200945 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100946static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200947{
Christopher Faulet570d1612018-11-26 11:13:57 +0100948 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200949
Christopher Faulet570d1612018-11-26 11:13:57 +0100950 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200951 (*(p + 5) > '1' ||
952 (*(p + 5) == '1' && *(p + 7) >= '1')))
953 h1m->flags |= H1_MF_VER_11;
954}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200955
Christopher Faulet9768c262018-10-22 09:34:31 +0200956/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
957 * greater or equal to 1.1
958 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100959static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200960{
Christopher Faulet570d1612018-11-26 11:13:57 +0100961 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200962
Christopher Faulet570d1612018-11-26 11:13:57 +0100963 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200964 (*(p + 5) > '1' ||
965 (*(p + 5) == '1' && *(p + 7) >= '1')))
966 h1m->flags |= H1_MF_VER_11;
967}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200968
Christopher Fauletf2824e62018-10-01 12:12:37 +0200969/* Deduce the connection mode of the client connection, depending on the
970 * configuration and the H1 message flags. This function is called twice, the
971 * first time when the request is parsed and the second time when the response
972 * is parsed.
973 */
974static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
975{
976 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200977
978 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100979 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100980 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100981 h1s->status == 101) {
982 /* Either we've established an explicit tunnel, or we're
983 * switching the protocol. In both cases, we're very unlikely to
984 * understand the next protocols. We have to switch to tunnel
985 * mode, so that we transfer the request and responses then let
986 * this protocol pass unmodified. When we later implement
987 * specific parsers for such protocols, we'll want to check the
988 * Upgrade header which contains information about that protocol
989 * for responses with status 101 (eg: see RFC2817 about TLS).
990 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200991 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200992 TRACE_STATE("set tunnel mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100993 }
994 else if (h1s->flags & H1S_F_WANT_KAL) {
995 /* By default the client is in KAL mode. CLOSE mode mean
996 * it is imposed by the client itself. So only change
997 * KAL mode here. */
998 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
999 /* no length known or explicit close => close */
1000 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001001 TRACE_STATE("detect close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001002 }
1003 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1004 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001005 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001006 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001007 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001008 }
1009 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001010 }
1011 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001012 /* Input direction: first pass */
1013 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1014 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001015 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001016 TRACE_STATE("detect close mode (req)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001017 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001018 }
1019
1020 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001021 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001022 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001023 TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1024 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001025}
1026
1027/* Deduce the connection mode of the client connection, depending on the
1028 * configuration and the H1 message flags. This function is called twice, the
1029 * first time when the request is parsed and the second time when the response
1030 * is parsed.
1031 */
1032static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1033{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001034 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001035 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001036 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001037
Christopher Fauletf2824e62018-10-01 12:12:37 +02001038 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001039 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001040 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001041 h1s->status == 101) {
1042 /* Either we've established an explicit tunnel, or we're
1043 * switching the protocol. In both cases, we're very unlikely to
1044 * understand the next protocols. We have to switch to tunnel
1045 * mode, so that we transfer the request and responses then let
1046 * this protocol pass unmodified. When we later implement
1047 * specific parsers for such protocols, we'll want to check the
1048 * Upgrade header which contains information about that protocol
1049 * for responses with status 101 (eg: see RFC2817 about TLS).
1050 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001051 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001052 TRACE_STATE("set tunnel mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001053 }
1054 else if (h1s->flags & H1S_F_WANT_KAL) {
1055 /* By default the server is in KAL mode. CLOSE mode mean
1056 * it is imposed by haproxy itself. So only change KAL
1057 * mode here. */
1058 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1059 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1060 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1061 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001062 TRACE_STATE("detect close mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001063 }
1064 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001065 }
Christopher Faulet70033782018-12-05 13:50:11 +01001066 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001067 /* Output direction: first pass */
1068 if (h1m->flags & H1_MF_CONN_CLO) {
1069 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001070 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001071 TRACE_STATE("detect close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001072 }
1073 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1074 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1075 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1076 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1077 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1078 /* no explicit keep-alive option httpclose/server-close => close */
1079 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001080 TRACE_STATE("force close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001081 }
Christopher Faulet70033782018-12-05 13:50:11 +01001082 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001083
1084 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001085 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001086 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001087 TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1088 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001089}
1090
Christopher Fauletb992af02019-03-28 15:42:24 +01001091static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001092{
1093 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001094
1095 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1096 * token is found
1097 */
1098 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001100
1101 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001102 if (!(h1m->flags & H1_MF_VER_11)) {
1103 TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001104 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001105 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001106 }
1107 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001108 if (h1m->flags & H1_MF_VER_11) {
1109 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001110 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001111 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001112 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113}
1114
Christopher Fauletb992af02019-03-28 15:42:24 +01001115static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001117 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1118 * token is found
1119 */
1120 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001121 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001122
1123 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001124 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001125 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1126 TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001127 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001128 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001129 }
1130 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001131 if (h1m->flags & H1_MF_VER_11) {
1132 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001133 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001134 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001135 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001136}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001137
Christopher Fauletb992af02019-03-28 15:42:24 +01001138static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001139{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001140 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001141 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001142 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001143 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001144}
1145
Christopher Fauletb992af02019-03-28 15:42:24 +01001146static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1147{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001148 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001149 h1_set_cli_conn_mode(h1s, h1m);
1150 else
1151 h1_set_srv_conn_mode(h1s, h1m);
1152
1153 if (!(h1m->flags & H1_MF_RESP))
1154 h1_update_req_conn_value(h1s, h1m, conn_val);
1155 else
1156 h1_update_res_conn_value(h1s, h1m, conn_val);
1157}
Christopher Faulete44769b2018-11-29 23:01:45 +01001158
Christopher Faulet98fbe952019-07-22 16:18:24 +02001159/* Try to adjust the case of the message header name using the global map
1160 * <hdrs_map>.
1161 */
1162static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1163{
1164 struct ebpt_node *node;
1165 struct h1_hdr_entry *entry;
1166
1167 /* No entry in the map, do nothing */
1168 if (eb_is_empty(&hdrs_map.map))
1169 return;
1170
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001171 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001172 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1173 return;
1174
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001175 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001176 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1177 return;
1178
1179 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1180 if (!node)
1181 return;
1182 entry = container_of(node, struct h1_hdr_entry, node);
1183 name->ptr = entry->name.ptr;
1184 name->len = entry->name.len;
1185}
1186
Christopher Faulete44769b2018-11-29 23:01:45 +01001187/* Append the description of what is present in error snapshot <es> into <out>.
1188 * The description must be small enough to always fit in a buffer. The output
1189 * buffer may be the trash so the trash must not be used inside this function.
1190 */
1191static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1192{
1193 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001194 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1195 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1196 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1197 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1198 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1199 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001200}
1201/*
1202 * Capture a bad request or response and archive it in the proxy's structure.
1203 * By default it tries to report the error position as h1m->err_pos. However if
1204 * this one is not set, it will then report h1m->next, which is the last known
1205 * parsing point. The function is able to deal with wrapping buffers. It always
1206 * displays buffers as a contiguous area starting at buf->p. The direction is
1207 * determined thanks to the h1m's flags.
1208 */
1209static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1210 struct h1m *h1m, struct buffer *buf)
1211{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001212 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001213 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001214 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001215 union error_snapshot_ctx ctx;
1216
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001217 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001218 if (sess == NULL)
1219 sess = si_strm(h1s->cs->data)->sess;
1220 if (!(h1m->flags & H1_MF_RESP))
1221 other_end = si_strm(h1s->cs->data)->be;
1222 else
1223 other_end = sess->fe;
1224 } else
1225 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001226
1227 /* http-specific part now */
1228 ctx.h1.state = h1m->state;
1229 ctx.h1.c_flags = h1c->flags;
1230 ctx.h1.s_flags = h1s->flags;
1231 ctx.h1.m_flags = h1m->flags;
1232 ctx.h1.m_clen = h1m->curr_len;
1233 ctx.h1.m_blen = h1m->body_len;
1234
1235 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1236 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001237 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1238 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001239}
1240
Christopher Fauletadb22202018-12-12 10:32:09 +01001241/* Emit the chunksize followed by a CRLF in front of data of the buffer
1242 * <buf>. It goes backwards and starts with the byte before the buffer's
1243 * head. The caller is responsible for ensuring there is enough room left before
1244 * the buffer's head for the string.
1245 */
1246static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1247{
1248 char *beg, *end;
1249
1250 beg = end = b_head(buf);
1251 *--beg = '\n';
1252 *--beg = '\r';
1253 do {
1254 *--beg = hextab[chksz & 0xF];
1255 } while (chksz >>= 4);
1256 buf->head -= (end - beg);
1257 b_add(buf, end - beg);
1258}
1259
1260/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1261 * ensuring there is enough room left in the buffer for the string. */
1262static void h1_emit_chunk_crlf(struct buffer *buf)
1263{
1264 *(b_peek(buf, b_data(buf))) = '\r';
1265 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1266 b_add(buf, 2);
1267}
1268
Christopher Faulet129817b2018-09-20 16:14:40 +02001269/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001270 * Switch the stream to tunnel mode. This function must only be called on 2xx
1271 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001272 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001273static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001274{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001275 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001276
Christopher Faulet5be651d2021-01-22 15:28:03 +01001277 h1s->req.state = H1_MSG_TUNNEL;
1278 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001279
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001280 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001281 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001282
Christopher Faulet5be651d2021-01-22 15:28:03 +01001283 TRACE_STATE("switch H1 stream in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01001284
Christopher Faulet5be651d2021-01-22 15:28:03 +01001285 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1286 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1287 if (b_data(&h1c->ibuf))
1288 h1_wake_stream_for_recv(h1s);
1289 tasklet_wakeup(h1c->wait_event.tasklet);
1290 TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001291 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01001292 if (h1c->flags & H1C_F_WAIT_INPUT) {
1293 h1c->flags &= ~H1C_F_WAIT_INPUT;
1294 h1_wake_stream_for_send(h1s);
1295 if (b_data(&h1c->obuf))
1296 tasklet_wakeup(h1c->wait_event.tasklet);
1297 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001298 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001299}
1300
1301/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001302 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001303 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001304 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001305 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001306static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001307 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001308{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001309 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001310 int ret = 0;
1311
Willy Tarreau022e5e52020-09-10 09:33:15 +02001312 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001313
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001314 if (h1s->meth == HTTP_METH_CONNECT)
1315 h1m->flags |= H1_MF_METH_CONNECT;
1316 if (h1s->meth == HTTP_METH_HEAD)
1317 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001318
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001319 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1320 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001321 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001322 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001323 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001324 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001325 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1326 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001327 goto end;
1328 }
1329
Christopher Faulet486498c2019-10-11 14:22:00 +02001330 if (h1m->err_pos >= 0) {
1331 /* Maybe we found an error during the parsing while we were
1332 * configured not to block on that, so we have to capture it
1333 * now.
1334 */
1335 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1336 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1337 }
1338
Christopher Faulet5696f542020-12-02 16:08:38 +01001339 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001340 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001341 if (h1s->meth == HTTP_METH_HEAD)
1342 h1s->flags |= H1S_F_BODYLESS_RESP;
1343 }
1344 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001345 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001346 if (h1s->status == 204 || h1s->status == 304)
1347 h1s->flags |= H1S_F_BODYLESS_RESP;
1348 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001349 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001350 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001351
Christopher Faulet129817b2018-09-20 16:14:40 +02001352 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001353 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001354 return ret;
1355}
1356
1357/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001358 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001359 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1360 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001361 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001362static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001363 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001364 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001365{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001366 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001367
Willy Tarreau022e5e52020-09-10 09:33:15 +02001368 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001369 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001370 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001371 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Christopher Faulet02a02532019-11-15 09:36:28 +01001372 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001373 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001374 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001375 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001376 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001377 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001378 }
1379
Christopher Faulet02a02532019-11-15 09:36:28 +01001380 *ofs += ret;
1381
1382 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001383 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001384 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001385}
1386
1387/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001388 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1389 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1390 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1391 * responsible to update the parser state <h1m>.
1392 */
1393static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1394 struct buffer *buf, size_t *ofs, size_t max)
1395{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001396 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001397
Willy Tarreau022e5e52020-09-10 09:33:15 +02001398 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001399 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001400 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001401 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Christopher Faulet02a02532019-11-15 09:36:28 +01001402 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001403 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001404 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001405 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1406 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001407 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001408 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001409
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001410 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001411
1412 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001413 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001414 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001415}
1416
1417/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001418 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001419 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1420 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001421 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001422static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001423{
Christopher Faulet539e0292018-11-19 10:40:09 +01001424 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001425 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001426 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001427 size_t data;
1428 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001429 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001430
Christopher Faulet539e0292018-11-19 10:40:09 +01001431 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001432 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001433
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001434 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001435 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001436
Christopher Faulet2eed8002020-12-07 11:26:13 +01001437 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001438 goto end;
1439
Christopher Faulet5be651d2021-01-22 15:28:03 +01001440 if (h1c->flags & H1C_F_WAIT_OUTPUT)
1441 goto end;
1442
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001443 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001444 size_t used = htx_used_space(htx);
1445
Christopher Faulet129817b2018-09-20 16:14:40 +02001446 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001447 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001448 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001449 if (!ret)
1450 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001451
1452 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1453 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1454
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001455 /* Reject Protocol upgrade request with payload */
1456 if ((h1m->flags & (H1_MF_RESP|H1_MF_CONN_UPG)) == H1_MF_CONN_UPG && h1m->state != H1_MSG_DONE) {
1457 h1s->flags |= H1S_F_NOT_IMPL_ERROR;
1458 TRACE_USER("Upgrade with body not implemented, reject H1 message",
1459 H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1460 break;
1461 }
1462
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001463 if ((h1m->flags & H1_MF_RESP) &&
1464 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1465 h1m_init_res(&h1s->res);
1466 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001467 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001468 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001469 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001470 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001471 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001472 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001473 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001474 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001475
1476 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1477 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001478 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001479 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001480 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1481 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1482 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001483 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001484
Christopher Faulet76014fd2019-12-10 11:47:22 +01001485 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1486 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001487 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001488 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001489 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1490 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001491
Christopher Faulet5be651d2021-01-22 15:28:03 +01001492 if ((h1m->flags & H1_MF_RESP) &&
1493 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1494 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001495 else {
1496 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1497 /* Unfinished transaction: block this input side waiting the end of the output side */
1498 h1c->flags |= H1C_F_WAIT_OUTPUT;
1499 TRACE_STATE("Disable read on h1c (wait_output)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1500 }
1501 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1502 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1503 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1504 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1505 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001506 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001507 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001508 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001509 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001510 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001511 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001512 if (!ret)
1513 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001514
1515 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1516 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001517 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001518 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001519 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001520 break;
1521 }
1522
Christopher Faulet30db3d72019-05-17 15:35:33 +02001523 count -= htx_used_space(htx) - used;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001524 } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) && !(h1c->flags & H1C_F_WAIT_OUTPUT));
1525
Christopher Faulet129817b2018-09-20 16:14:40 +02001526
Christopher Faulet2eed8002020-12-07 11:26:13 +01001527 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
1528 TRACE_PROTO("parsing or not-implemented error", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001529 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001530 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001531
Christopher Faulet539e0292018-11-19 10:40:09 +01001532 b_del(&h1c->ibuf, total);
1533
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001534 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001535 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1536
Christopher Faulet30db3d72019-05-17 15:35:33 +02001537 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001538 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001539 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001540 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001541 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001542 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001543
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001544 if (!b_data(&h1c->ibuf))
1545 h1_release_buf(h1c, &h1c->ibuf);
1546
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001547 if (!(h1c->flags & H1C_F_ST_READY)) {
1548 /* The H1 connection is not ready. Most of time, there is no CS
1549 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1550 * cases, it is only possible on the client side.
1551 */
1552 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1553
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001554 if (h1m->state <= H1_MSG_LAST_LF) {
1555 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1556 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1557 goto end;
1558 }
1559
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001560 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1561 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1562 BUG_ON(h1s->cs);
1563 if (!h1s_new_cs(h1s, buf)) {
1564 h1c->flags |= H1C_F_ST_ERROR;
1565 goto err;
1566 }
1567 }
1568 else {
1569 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1570 BUG_ON(h1s->cs == NULL);
1571 if (!h1s_upgrade_cs(h1s, buf)) {
1572 h1c->flags |= H1C_F_ST_ERROR;
1573 goto err;
1574 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001575 }
1576 }
1577
1578 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001579 if (!(h1m->flags & H1_MF_CHNK) &&
1580 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1581 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1582 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1583 }
1584 else {
1585 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1586 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1587 }
1588
Christopher Faulet5be651d2021-01-22 15:28:03 +01001589 /* Don't set EOI on the conn-stream for protocol upgrade or connect
1590 * requests, wait the response to do so or not depending on the status
1591 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001592 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001593 if ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG))
Christopher Fauleta583af62020-09-24 15:35:37 +02001594 h1s->cs->flags |= CS_FL_EOI;
1595
Christopher Faulet6716cc22019-12-20 17:33:24 +01001596 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001597 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001598 else {
1599 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1600 if (h1s->flags & H1S_F_REOS) {
1601 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001602 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1603 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1604 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001605 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001606 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001607 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
1608 h1s->cs->flags |= CS_FL_ERROR;
Christopher Fauletb385b502021-01-13 18:47:57 +01001609
1610 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1611 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1612 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1613 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1614 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001615 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001616 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001617
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001618 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001619 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001620 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001621
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001622 err:
Christopher Faulet47365272018-10-31 17:40:50 +01001623 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001624 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001625 if (h1s->cs)
1626 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001627 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001628 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001629}
1630
Christopher Faulet129817b2018-09-20 16:14:40 +02001631/*
1632 * Process outgoing data. It parses data and transfer them from the channel buffer into
1633 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1634 * 0 if it couldn't proceed.
1635 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001636static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1637{
Christopher Faulet129817b2018-09-20 16:14:40 +02001638 struct h1s *h1s = h1c->h1s;
1639 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001640 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001641 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001642 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001643 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001644 int last_data = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001645
Christopher Faulet47365272018-10-31 17:40:50 +01001646 if (!count)
1647 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001648
Christopher Faulet94b2c762019-05-24 15:28:57 +02001649 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001650 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1651
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001652 if (htx_is_empty(chn_htx))
1653 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001654
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001655 if (h1s->flags & H1S_F_PROCESSING_ERROR)
1656 goto end;
1657
Christopher Fauletdea24742021-01-22 15:12:30 +01001658 if (h1c->flags & H1C_F_WAIT_INPUT)
1659 goto end;
1660
Christopher Faulet51dbc942018-09-13 09:05:15 +02001661 if (!h1_get_buf(h1c, &h1c->obuf)) {
1662 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001663 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001664 goto end;
1665 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001666
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001667 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001668
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001669 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001670 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001671
Willy Tarreau3815b222018-12-11 19:50:43 +01001672 /* Perform some optimizations to reduce the number of buffer copies.
1673 * First, if the mux's buffer is empty and the htx area contains
1674 * exactly one data block of the same size as the requested count,
1675 * then it's possible to simply swap the caller's buffer with the
1676 * mux's output buffer and adjust offsets and length to match the
1677 * entire DATA HTX block in the middle. In this case we perform a
1678 * true zero-copy operation from end-to-end. This is the situation
1679 * that happens all the time with large files. Second, if this is not
1680 * possible, but the mux's output buffer is empty, we still have an
1681 * opportunity to avoid the copy to the intermediary buffer, by making
1682 * the intermediary buffer's area point to the output buffer's area.
1683 * In this case we want to skip the HTX header to make sure that copies
1684 * remain aligned and that this operation remains possible all the
1685 * time. This goes for headers, data blocks and any data extracted from
1686 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001687 */
1688 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001689 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
1690 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001691 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001692 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001693 void *old_area;
1694
1695 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
1696 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
1697 goto skip_zero_copy;
1698 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001699
Christopher Faulet6b81df72019-10-01 22:08:43 +02001700 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001701 if (h1m->state == H1_MSG_DATA && chn_htx->flags & HTX_FL_EOM) {
1702 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1703 last_data = 1;
1704 }
1705
Christopher Faulete5596bf2020-12-02 16:13:22 +01001706 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01001707 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001708 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001709 h1c->obuf.data = count;
1710
1711 buf->area = old_area;
1712 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001713
Christopher Faulet6b81df72019-10-01 22:08:43 +02001714 chn_htx = (struct htx *)buf->area;
1715 htx_reset(chn_htx);
1716
Christopher Fauletadb22202018-12-12 10:32:09 +01001717 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001718 * size and eventually the last chunk. We have at least
1719 * the size of the struct htx to write the chunk
1720 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01001721 */
1722 if (h1m->flags & H1_MF_CHNK) {
1723 h1_emit_chunk_size(&h1c->obuf, count);
1724 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001725 if (last_data) {
1726 /* Emit the last chunk too at the buffer's end */
1727 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
1728 }
Christopher Fauletadb22202018-12-12 10:32:09 +01001729 }
1730
Christopher Faulet6b81df72019-10-01 22:08:43 +02001731 if (h1m->state == H1_MSG_DATA)
1732 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001733 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001734 else
1735 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001736 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001737
Christopher Faulete5596bf2020-12-02 16:13:22 +01001738 skip_zero_copy:
1739 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001740 if (last_data) {
1741 h1m->state = H1_MSG_DONE;
1742 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
1743 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1744 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1745 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1746 }
1747
1748 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1749 H1_EV_TX_DATA, h1c->conn, h1s);
1750 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001751 goto out;
1752 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001753 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001754 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001755 else
1756 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001757
Christopher Fauletc2518a52019-06-25 21:41:02 +02001758 tmp.data = 0;
1759 tmp.size = b_room(&h1c->obuf);
Christopher Fauletdea24742021-01-22 15:12:30 +01001760 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && !(h1c->flags & H1C_F_WAIT_INPUT) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001761 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001762 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001763 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001764 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001765 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001766
Christopher Fauletb2e84162018-12-06 11:39:49 +01001767 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001768 if (type != HTX_BLK_DATA && vlen > count)
1769 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001770
Christopher Faulet94b2c762019-05-24 15:28:57 +02001771 if (type == HTX_BLK_UNUSED)
1772 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001773
Christopher Faulet94b2c762019-05-24 15:28:57 +02001774 switch (h1m->state) {
1775 case H1_MSG_RQBEFORE:
1776 if (type != HTX_BLK_REQ_SL)
1777 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001778 TRACE_USER("sending request headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001779 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001780 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001781 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001782 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001783 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001784 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001785 if (sl->flags & HTX_SL_F_BODYLESS)
1786 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001787 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01001788 if (h1s->meth == HTTP_METH_HEAD)
1789 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Fauletb385b502021-01-13 18:47:57 +01001790 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1791 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Christopher Faulet089acd52020-09-21 10:57:52 +02001792 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1793 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1794 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001795 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001796
Christopher Faulet94b2c762019-05-24 15:28:57 +02001797 case H1_MSG_RPBEFORE:
1798 if (type != HTX_BLK_RES_SL)
1799 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001800 TRACE_USER("sending response headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001801 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001802 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001803 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001804 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001805 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001806 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001807 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001808 if (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001809 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001810 else if (h1s->status == 204 || h1s->status == 304)
1811 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02001812 h1m->state = H1_MSG_HDR_FIRST;
1813 break;
1814
Christopher Faulet94b2c762019-05-24 15:28:57 +02001815 case H1_MSG_HDR_FIRST:
1816 case H1_MSG_HDR_NAME:
1817 case H1_MSG_HDR_L2_LWS:
1818 if (type == HTX_BLK_EOH)
1819 goto last_lf;
1820 if (type != HTX_BLK_HDR)
1821 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001822
Christopher Faulet9768c262018-10-22 09:34:31 +02001823 h1m->state = H1_MSG_HDR_NAME;
1824 n = htx_get_blk_name(chn_htx, blk);
1825 v = htx_get_blk_value(chn_htx, blk);
1826
Christopher Faulet86d144c2019-08-14 16:32:25 +02001827 /* Skip all pseudo-headers */
1828 if (*(n.ptr) == ':')
1829 goto skip_hdr;
1830
Christopher Fauletb045bb22020-02-28 10:42:20 +01001831 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001832 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001833 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001834 /* Only skip C-L header with invalid value. */
1835 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001836 goto skip_hdr;
1837 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001838 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001839 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001840 if (!v.len)
1841 goto skip_hdr;
1842 }
1843
Christopher Faulet67d58092019-10-02 10:51:38 +02001844 /* Skip header if same name is used to add the server name */
1845 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1846 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1847 goto skip_hdr;
1848
Christopher Faulet98fbe952019-07-22 16:18:24 +02001849 /* Try to adjust the case of the header name */
1850 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1851 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001852 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001853 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001854 skip_hdr:
1855 h1m->state = H1_MSG_HDR_L2_LWS;
1856 break;
1857
Christopher Faulet94b2c762019-05-24 15:28:57 +02001858 case H1_MSG_LAST_LF:
1859 if (type != HTX_BLK_EOH)
1860 goto error;
1861 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001862 h1m->state = H1_MSG_LAST_LF;
1863 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001864 /* If the reply comes from haproxy while the request is
1865 * not finished, we force the connection close. */
1866 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1867 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1868 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1869 }
1870
1871 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001872 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001873 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001874 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001875 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001876 /* Try to adjust the case of the header name */
1877 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1878 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001879 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001880 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001881 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001882 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001883 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001884
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001885 if ((h1s->meth != HTTP_METH_CONNECT &&
1886 (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 +01001887 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01001888 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001889 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001890 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1891 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001892 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001893 n = ist("transfer-encoding");
1894 v = ist("chunked");
1895 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1896 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001897 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001898 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001899 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001900 h1m->flags |= H1_MF_CHNK;
1901 }
1902
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001903 /* Now add the server name to a header (if requested) */
1904 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1905 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1906 struct server *srv = objt_server(h1c->conn->target);
1907
1908 if (srv) {
1909 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1910 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001911
1912 /* Try to adjust the case of the header name */
1913 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1914 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001915 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001916 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001917 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001918 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001919 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1920 }
1921
Christopher Faulet6b81df72019-10-01 22:08:43 +02001922 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1923 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1924
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001925 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001926 if (!chunk_memcat(&tmp, "\r\n", 2))
1927 goto full;
1928 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001929 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001930 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001931 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001932 if (!chunk_memcat(&tmp, "\r\n", 2))
1933 goto full;
1934 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001935 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001936 else if ((h1m->flags & H1_MF_RESP) &&
1937 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001938 if (!chunk_memcat(&tmp, "\r\n", 2))
1939 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001940 h1m_init_res(&h1s->res);
1941 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001942 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001943 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001944 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001945 else {
1946 /* EOM flag is set and it is the last block */
1947 if (htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
1948 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
1949 goto full;
1950 else if (!chunk_memcat(&tmp, "\r\n", 2))
1951 goto full;
1952 goto done;
1953 }
1954 else if (!chunk_memcat(&tmp, "\r\n", 2))
1955 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001956 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001957 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001958 break;
1959
Christopher Faulet94b2c762019-05-24 15:28:57 +02001960 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001961 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001962 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001963 /* If the message is not chunked, never
1964 * add the last chunk. */
1965 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001966 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001967 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 +02001968 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001969 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001970 else if (type != HTX_BLK_DATA)
1971 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001972
Christopher Faulete5596bf2020-12-02 16:13:22 +01001973 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
1974 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
1975 break;
1976 }
1977
Christopher Faulet6b81df72019-10-01 22:08:43 +02001978 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 +02001979
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001980 /* It is the last block of this message. After this one,
1981 * only tunneled data may be forwarded. */
1982 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
1983 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1984 last_data = 1;
1985 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02001986
1987 if (vlen > count) {
1988 /* Get the maximum amount of data we can xferred */
1989 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001990 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001991 }
1992
1993 chklen = 0;
1994 if (h1m->flags & H1_MF_CHNK) {
1995 chklen = b_room(&tmp);
1996 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1997 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1998 (chklen < 1048576) ? 5 : 8);
1999 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002000
2001 /* If it is the end of the chunked message (without EOT), reserve the
2002 * last chunk size */
2003 if (last_data)
2004 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002005 }
2006
2007 if (vlen + chklen > b_room(&tmp)) {
2008 /* too large for the buffer */
2009 if (chklen >= b_room(&tmp))
2010 goto full;
2011 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002012 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002013 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002014 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002015 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002016 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002017 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002018
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002019 /* Space already reserved, so it must succeed */
2020 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2021 goto error;
2022
Christopher Faulet6b81df72019-10-01 22:08:43 +02002023 if (h1m->state == H1_MSG_DATA)
2024 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002025 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002026 else
2027 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002028 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002029 if (last_data)
2030 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002031 break;
2032
Christopher Faulet94b2c762019-05-24 15:28:57 +02002033 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002034 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002035 goto error;
2036 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002037 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002038
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002039 /* If the message is not chunked, ignore
2040 * trailers. It may happen with H2 messages. */
2041 if (!(h1m->flags & H1_MF_CHNK))
2042 break;
2043
Christopher Faulete5596bf2020-12-02 16:13:22 +01002044 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2045 TRACE_PROTO("Skip trailers for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2046 break;
2047 }
2048
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002049 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002050 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002051 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002052 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2053 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002054 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002055 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002056 else { // HTX_BLK_TLR
2057 n = htx_get_blk_name(chn_htx, blk);
2058 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002059
2060 /* Try to adjust the case of the header name */
2061 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2062 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002063 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002064 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002065 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002066 break;
2067
Christopher Faulet94b2c762019-05-24 15:28:57 +02002068 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002069 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2070 goto error; /* For now return an error */
2071
Christopher Faulet94b2c762019-05-24 15:28:57 +02002072 done:
2073 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002074 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
2075 h1c->flags |= H1C_F_WAIT_INPUT;
2076 TRACE_STATE("Disable send on h1c (wait_input)", H1_EV_TX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
2077 }
2078 else if ((h1m->flags & H1_MF_RESP) &&
2079 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2080 /* a successful reply to a CONNECT or a protocol switching is sent
2081 * to the client. Switch the response to tunnel mode.
2082 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002083 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002084 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 +01002085 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002086
2087 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
Christopher Fauletb385b502021-01-13 18:47:57 +01002088 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002089 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02002090 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 +02002091 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002092
2093 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2094 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002095 break;
2096
Christopher Faulet9768c262018-10-22 09:34:31 +02002097 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002098 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002099 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002100 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002101 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002102 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002103 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002104 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2105 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002106 break;
2107 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002108
2109 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002110 total += vlen;
2111 count -= vlen;
2112 if (sz == vlen)
2113 blk = htx_remove_blk(chn_htx, blk);
2114 else {
2115 htx_cut_data_blk(chn_htx, blk, vlen);
2116 break;
2117 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002118 }
2119
Christopher Faulet9768c262018-10-22 09:34:31 +02002120 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002121 /* when the output buffer is empty, tmp shares the same area so that we
2122 * only have to update pointers and lengths.
2123 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002124 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2125 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002126 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002127 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002128
Willy Tarreau3815b222018-12-11 19:50:43 +01002129 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002130 out:
2131 if (!buf_room_for_htx_data(&h1c->obuf)) {
2132 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002134 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002135 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002136 /* Both the request and the response reached the DONE state. So set EOI
2137 * flag on the conn-stream. Most of time, the flag will already be set,
2138 * except for protocol upgrades. Report an error if data remains blocked
2139 * in the output buffer.
2140 */
2141 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2142 if (!htx_is_empty(chn_htx)) {
2143 h1c->flags |= H1C_F_ST_ERROR;
2144 TRACE_STATE("txn done but data waiting to be sent, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
2145 }
2146 h1s->cs->flags |= CS_FL_EOI;
2147 }
2148
Christopher Faulet6b81df72019-10-01 22:08:43 +02002149 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002150 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002151
2152 full:
2153 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2154 h1c->flags |= H1C_F_OUT_FULL;
2155 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002156}
2157
Christopher Faulet51dbc942018-09-13 09:05:15 +02002158/*********************************************************/
2159/* functions below are I/O callbacks from the connection */
2160/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002161static void h1_wake_stream_for_recv(struct h1s *h1s)
2162{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002163 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002164 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002165 tasklet_wakeup(h1s->subs->tasklet);
2166 h1s->subs->events &= ~SUB_RETRY_RECV;
2167 if (!h1s->subs->events)
2168 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002169 }
2170}
2171static void h1_wake_stream_for_send(struct h1s *h1s)
2172{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002173 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002174 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002175 tasklet_wakeup(h1s->subs->tasklet);
2176 h1s->subs->events &= ~SUB_RETRY_SEND;
2177 if (!h1s->subs->events)
2178 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002179 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002180}
2181
Christopher Fauletad4daf62021-01-21 17:49:01 +01002182/* alerts the data layer following this sequence :
2183 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2184 * - if its subscribed to send, then it's woken up for send
2185 * - if it was subscribed to neither, its ->wake() callback is called
2186 */
2187static void h1_alert(struct h1s *h1s)
2188{
2189 if (h1s->subs) {
2190 h1_wake_stream_for_recv(h1s);
2191 h1_wake_stream_for_send(h1s);
2192 }
2193 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2194 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2195 h1s->cs->data_cb->wake(h1s->cs);
2196 }
2197}
2198
Christopher Fauletc18fc232020-10-06 17:41:36 +02002199/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2200 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2201 * retryable errors (allocation error or buffer full). On success, the error is
2202 * copied in the output buffer.
2203*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002204static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002205{
2206 int rc = http_get_status_idx(h1c->errcode);
2207 int ret = 0;
2208
2209 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2210
2211 /* Verify if the error is mapped on /dev/null or any empty file */
2212 /// XXX: do a function !
2213 if (h1c->px->replies[rc] &&
2214 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2215 h1c->px->replies[rc]->body.errmsg &&
2216 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2217 /* Empty error, so claim a success */
2218 ret = 1;
2219 goto out;
2220 }
2221
2222 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2223 h1c->flags |= H1C_F_ERR_PENDING;
2224 goto out;
2225 }
2226
2227 if (!h1_get_buf(h1c, &h1c->obuf)) {
2228 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2229 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2230 goto out;
2231 }
2232 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2233 if (unlikely(ret <= 0)) {
2234 if (!ret) {
2235 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2236 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2237 goto out;
2238 }
2239 else {
2240 /* we cannot report this error, so claim a success */
2241 ret = 1;
2242 }
2243 }
2244 h1c->flags &= ~H1C_F_ERR_PENDING;
2245 out:
2246 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2247 return ret;
2248}
2249
2250/* Try to send a 500 internal error. It relies on h1_send_error to send the
2251 * error. This function takes care of incrementing stats and tracked counters.
2252 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002253static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002254{
2255 struct session *sess = h1c->conn->owner;
2256 int ret = 1;
2257
2258 session_inc_http_req_ctr(sess);
2259 session_inc_http_err_ctr(sess);
2260 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2261 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[5], 1);
2262 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
2263 if (sess->listener->counters)
2264 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
2265
2266 h1c->errcode = 500;
2267 ret = h1_send_error(h1c);
2268 sess_log(sess);
2269 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002270}
2271
Christopher Fauletc18fc232020-10-06 17:41:36 +02002272/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2273 * error. This function takes care of incrementing stats and tracked counters.
2274 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002275static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002276{
2277 struct session *sess = h1c->conn->owner;
2278 int ret = 1;
2279
2280 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2281 goto end;
2282
2283 session_inc_http_req_ctr(sess);
2284 session_inc_http_err_ctr(sess);
2285 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2286 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2287 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2288 if (sess->listener->counters)
2289 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2290
2291 h1c->errcode = 400;
2292 ret = h1_send_error(h1c);
2293 sess_log(sess);
2294
2295 end:
2296 return ret;
2297}
2298
Christopher Faulet2eed8002020-12-07 11:26:13 +01002299/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2300 * the error. This function takes care of incrementing stats and tracked
2301 * counters.
2302 */
2303static int h1_handle_not_impl_err(struct h1c *h1c)
2304{
2305 struct session *sess = h1c->conn->owner;
2306 int ret = 1;
2307
2308 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2309 goto end;
2310
2311 session_inc_http_req_ctr(sess);
2312 session_inc_http_err_ctr(sess);
2313 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2314 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2315 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2316 if (sess->listener->counters)
2317 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2318
2319 h1c->errcode = 501;
2320 ret = h1_send_error(h1c);
2321 sess_log(sess);
2322
2323 end:
2324 return ret;
2325}
2326
Christopher Fauletc18fc232020-10-06 17:41:36 +02002327/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2328 * error. This function takes care of incrementing stats and tracked counters.
2329 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002330static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002331{
2332 struct session *sess = h1c->conn->owner;
2333 int ret = 1;
2334
2335 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2336 goto end;
2337
2338 session_inc_http_req_ctr(sess);
2339 session_inc_http_err_ctr(sess);
2340 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2341 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2342 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2343 if (sess->listener->counters)
2344 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2345
2346 h1c->errcode = 408;
2347 ret = h1_send_error(h1c);
2348 sess_log(sess);
2349 end:
2350 return ret;
2351}
2352
2353
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354/*
2355 * Attempt to read data, and subscribe if none available
2356 */
2357static int h1_recv(struct h1c *h1c)
2358{
2359 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002360 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002361 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002362
Christopher Faulet6b81df72019-10-01 22:08:43 +02002363 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2364
2365 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2366 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002367 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002368 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369
Christopher Fauletae635762020-09-21 11:47:16 +02002370 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2371 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002372 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002373 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002375 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2376 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002377 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002378 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002379 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002380
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002381 /*
2382 * If we only have a small amount of data, realign it,
2383 * it's probably cheaper than doing 2 recv() calls.
2384 */
2385 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2386 b_slow_realign(&h1c->ibuf, trash.area, 0);
2387
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002388 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002389 if (!h1c->h1s ||
2390 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2391 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002392 flags |= CO_RFL_READ_ONCE;
2393
Willy Tarreau45f2b892018-12-05 07:59:27 +01002394 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002395 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002396 if (h1c->flags & H1C_F_IN_FULL) {
2397 h1c->flags &= ~H1C_F_IN_FULL;
2398 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2399 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002400
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002401 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002402 if (!b_data(&h1c->ibuf)) {
2403 /* try to pre-align the buffer like the rxbufs will be
2404 * to optimize memory copies.
2405 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002406 h1c->ibuf.head = sizeof(struct htx);
2407 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002408 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002409 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002410 if (max && !ret && h1_recv_allowed(h1c)) {
2411 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2412 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002413 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002414 else {
2415 h1_wake_stream_for_recv(h1c->h1s);
2416 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002417 }
2418
Christopher Faulet51dbc942018-09-13 09:05:15 +02002419 if (!b_data(&h1c->ibuf))
2420 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002421 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002422 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002423 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2424 }
2425
2426 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002427 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002428}
2429
2430
2431/*
2432 * Try to send data if possible
2433 */
2434static int h1_send(struct h1c *h1c)
2435{
2436 struct connection *conn = h1c->conn;
2437 unsigned int flags = 0;
2438 size_t ret;
2439 int sent = 0;
2440
Christopher Faulet6b81df72019-10-01 22:08:43 +02002441 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2442
2443 if (conn->flags & CO_FL_ERROR) {
2444 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002445 b_reset(&h1c->obuf);
2446 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002447 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002448
2449 if (!b_data(&h1c->obuf))
2450 goto end;
2451
Christopher Faulet46230362019-10-17 16:04:20 +02002452 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002454 if (h1c->flags & H1C_F_CO_STREAMER)
2455 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002456
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002457 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002458 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002459 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002460 if (h1c->flags & H1C_F_OUT_FULL) {
2461 h1c->flags &= ~H1C_F_OUT_FULL;
2462 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2463 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002464 b_del(&h1c->obuf, ret);
2465 sent = 1;
2466 }
2467
Christopher Faulet145aa472018-12-06 10:56:20 +01002468 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002469 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002470 /* error or output closed, nothing to send, clear the buffer to release it */
2471 b_reset(&h1c->obuf);
2472 }
2473
Christopher Faulet51dbc942018-09-13 09:05:15 +02002474 end:
Christopher Fauletb385b502021-01-13 18:47:57 +01002475 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_WAIT_INPUT)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002476 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002477
Christopher Faulet51dbc942018-09-13 09:05:15 +02002478 /* We're done, no more to send */
2479 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002480 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002481 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002482 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002483 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002484 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002485 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002486 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002487 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2488 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002489 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002490 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002491
Christopher Faulet6b81df72019-10-01 22:08:43 +02002492 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002493 return sent;
2494}
2495
Christopher Faulet51dbc942018-09-13 09:05:15 +02002496/* callback called on any event by the connection handler.
2497 * It applies changes and returns zero, or < 0 if it wants immediate
2498 * destruction of the connection.
2499 */
2500static int h1_process(struct h1c * h1c)
2501{
2502 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002503 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002504
Christopher Faulet6b81df72019-10-01 22:08:43 +02002505 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2506
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002507 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002508 if (b_data(&h1c->ibuf) && /* Input data to be processed */
2509 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2510 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002511 struct buffer *buf;
2512 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002513
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002514 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2515 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002516 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002517
2518 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002519 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
2520 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE)) { /* H2 upgrade supported by the proxy */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002521 /* Try to match H2 preface before parsing the request headers. */
2522 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2523 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002524 if (h1c->flags & H1C_F_ST_ATTACHED) {
2525 /* Force the REOS here to be sure to release the CS.
2526 Here ATTACHED implies !READY, and h1s defined
2527 */
2528 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2529 h1s->flags |= H1S_F_REOS;
2530 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002531 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002532 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002533 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002534 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002535
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002536 /* Create the H1 stream if not already there */
2537 if (!h1s) {
2538 h1s = h1c_frt_stream_new(h1c);
2539 if (!h1s) {
2540 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002541 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002542 goto no_parsing;
2543 }
2544 }
2545
2546 if (h1s->sess->t_idle == -1)
2547 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2548
2549 /* Get the stream rxbuf */
2550 buf = h1_get_buf(h1c, &h1s->rxbuf);
2551 if (!buf) {
2552 h1c->flags |= H1C_F_IN_SALLOC;
2553 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2554 return 0;
2555 }
2556
2557 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2558 h1_process_input(h1c, buf, count);
2559 h1_release_buf(h1c, &h1s->rxbuf);
2560 h1_set_idle_expiration(h1c);
2561
2562 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002563 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002564 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002565 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002566 }
2567 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2568 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002569 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002570 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002571 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2572 h1_handle_not_impl_err(h1c);
2573 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
2574 }
Christopher Fauletae635762020-09-21 11:47:16 +02002575 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002576 h1_send(h1c);
2577
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002578 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002579 if (!(h1c->flags & H1C_F_ST_READY)) {
2580 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002581 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002582 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002583 if (h1_handle_bad_req(h1c))
2584 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002585 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002586 }
2587
2588 /* Handle pending error, if any (only possible on frontend connection) */
2589 if (h1c->flags & H1C_F_ERR_PENDING) {
2590 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2591 if (h1_send_error(h1c))
2592 h1_send(h1c);
2593 }
Christopher Fauletae635762020-09-21 11:47:16 +02002594
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002595 /* If there is some pending outgoing data or error, just wait */
2596 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2597 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002598
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002599 /* Otherwise we can release the H1 connection */
2600 goto release;
2601 }
2602 else {
2603 /* Here there is still a H1 stream with a conn-stream.
2604 * Report the connection state at the stream level
2605 */
2606 if (conn_xprt_read0_pending(conn)) {
2607 h1s->flags |= H1S_F_REOS;
2608 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2609 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002610 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002611 h1s->cs->flags |= CS_FL_ERROR;
2612 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002613 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002614 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002615 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002616
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002617 if (!b_data(&h1c->ibuf))
2618 h1_release_buf(h1c, &h1c->ibuf);
2619
2620
2621 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2622 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2623 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002624 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002625
Christopher Faulet47365272018-10-31 17:40:50 +01002626 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002627 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002628 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002629 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002630
2631 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002632 if (h1c->flags & H1C_F_ST_ATTACHED) {
2633 /* Don't release the H1 connetion right now, we must destroy the
2634 * attached CS first. Here, the H1C must not be READY */
2635 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2636
2637 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2638 h1s->cs->flags |= CS_FL_EOS;
2639 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2640 h1s->cs->flags |= CS_FL_ERROR;
2641 h1_alert(h1s);
2642 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2643 }
2644 else {
2645 h1_release(h1c);
2646 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2647 }
Christopher Faulet539e0292018-11-19 10:40:09 +01002648 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002649}
2650
Willy Tarreau691d5032021-01-20 14:55:01 +01002651struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002652{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002653 struct connection *conn;
2654 struct tasklet *tl = (struct tasklet *)t;
2655 int conn_in_list;
2656 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002657 int ret = 0;
2658
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002659
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002660 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002661 if (tl->context == NULL) {
2662 /* The connection has been taken over by another thread,
2663 * we're no longer responsible for it, so just free the
2664 * tasklet, and do nothing.
2665 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002666 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002667 tasklet_free(tl);
2668 return NULL;
2669 }
2670 h1c = ctx;
2671 conn = h1c->conn;
2672
2673 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2674
2675 /* Remove the connection from the list, to be sure nobody attempts
2676 * to use it while we handle the I/O events
2677 */
2678 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2679 if (conn_in_list)
2680 MT_LIST_DEL(&conn->list);
2681
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002682 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002683
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002684 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002685 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002686 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002687 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002688 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002689 ret = h1_process(h1c);
2690 /* If we were in an idle list, we want to add it back into it,
2691 * unless h1_process() returned -1, which mean it has destroyed
2692 * the connection (testing !ret is enough, if h1_process() wasn't
2693 * called then ret will be 0 anyway.
2694 */
2695 if (!ret && conn_in_list) {
2696 struct server *srv = objt_server(conn->target);
2697
2698 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002699 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002700 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002701 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002702 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002703 return NULL;
2704}
2705
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002706static void h1_reset(struct connection *conn)
2707{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002708
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002709}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002710
2711static int h1_wake(struct connection *conn)
2712{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002713 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002714 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002715
Christopher Faulet6b81df72019-10-01 22:08:43 +02002716 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2717
Christopher Faulet539e0292018-11-19 10:40:09 +01002718 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002719 ret = h1_process(h1c);
2720 if (ret == 0) {
2721 struct h1s *h1s = h1c->h1s;
2722
Christopher Fauletad4daf62021-01-21 17:49:01 +01002723 if (h1c->flags & H1C_F_ST_ATTACHED)
2724 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002725 }
2726 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002727}
2728
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002729/* Connection timeout management. The principle is that if there's no receipt
2730 * nor sending for a certain amount of time, the connection is closed.
2731 */
2732static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2733{
2734 struct h1c *h1c = context;
2735 int expired = tick_is_expired(t->expire, now_ms);
2736
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002737 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002738
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002739 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002740 /* Make sure nobody stole the connection from us */
2741 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2742
2743 /* Somebody already stole the connection from us, so we should not
2744 * free it, we just have to free the task.
2745 */
2746 if (!t->context) {
2747 h1c = NULL;
2748 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2749 goto do_leave;
2750 }
2751
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002752 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002753 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2754 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002755 return t;
2756 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002757
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002758 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002759 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002760 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002761 if (h1c->flags & H1C_F_ST_READY) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002762 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2763 t->expire = TICK_ETERNITY;
2764 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2765 return t;
2766 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002767
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002768 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002769 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2770 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002771 if (h1_handle_req_tout(h1c))
2772 h1_send(h1c);
2773 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2774 h1_refresh_timeout(h1c);
Christopher Fauletcc043f62020-12-14 10:06:12 +01002775 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002776 return t;
2777 }
2778 }
2779
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002780 if (h1c->flags & H1C_F_ST_ATTACHED) {
2781 /* Don't release the H1 connetion right now, we must destroy the
2782 * attached CS first. Here, the H1C must not be READY */
2783 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
2784 h1_alert(h1c->h1s);
2785 h1_refresh_timeout(h1c);
2786 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2787 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2788 return t;
2789 }
2790
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002791 /* We're about to destroy the connection, so make sure nobody attempts
2792 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002793 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002794 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002795 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002796
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002797 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002798 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002799
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002800 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002801 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002802
2803 if (!h1c) {
2804 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002805 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002806 return NULL;
2807 }
2808
2809 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002810 h1_release(h1c);
2811 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002812 return NULL;
2813}
2814
Christopher Faulet51dbc942018-09-13 09:05:15 +02002815/*******************************************/
2816/* functions below are used by the streams */
2817/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002818
Christopher Faulet51dbc942018-09-13 09:05:15 +02002819/*
2820 * Attach a new stream to a connection
2821 * (Used for outgoing connections)
2822 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002823static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002824{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002825 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002826 struct conn_stream *cs = NULL;
2827 struct h1s *h1s;
2828
Christopher Faulet6b81df72019-10-01 22:08:43 +02002829 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002830 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002831 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 +02002832 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002833 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002834
Christopher Faulet236c93b2020-07-02 09:19:54 +02002835 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002836 if (!cs) {
2837 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 +02002838 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002839 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002840
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002841 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002842 if (h1s == NULL) {
2843 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 +02002844 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002845 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002846
Christopher Faulet6b81df72019-10-01 22:08:43 +02002847 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002848 return cs;
2849 end:
2850 cs_free(cs);
2851 return NULL;
2852}
2853
2854/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2855 * this mux, it's easy as we can only store a single conn_stream.
2856 */
2857static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2858{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002859 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002860 struct h1s *h1s = h1c->h1s;
2861
2862 if (h1s)
2863 return h1s->cs;
2864
2865 return NULL;
2866}
2867
Christopher Faulet73c12072019-04-08 11:23:22 +02002868static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002869{
Christopher Faulet73c12072019-04-08 11:23:22 +02002870 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002871
Christopher Faulet6b81df72019-10-01 22:08:43 +02002872 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002873 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002874 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002875}
2876
2877/*
2878 * Detach the stream from the connection and possibly release the connection.
2879 */
2880static void h1_detach(struct conn_stream *cs)
2881{
2882 struct h1s *h1s = cs->ctx;
2883 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002884 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002885 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002886
Christopher Faulet6b81df72019-10-01 22:08:43 +02002887 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2888
Christopher Faulet51dbc942018-09-13 09:05:15 +02002889 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002890 if (!h1s) {
2891 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002892 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002893 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002894
Olivier Houchardf502aca2018-12-14 19:42:40 +01002895 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002896 h1c = h1s->h1c;
2897 h1s->cs = NULL;
2898
Christopher Faulet42849b02020-10-05 11:35:17 +02002899 sess->accept_date = date;
2900 sess->tv_accept = now;
2901 sess->t_handshake = 0;
2902 sess->t_idle = -1;
2903
Olivier Houchard8a786902018-12-15 16:05:40 +01002904 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2905 h1s_destroy(h1s);
2906
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002907 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 +02002908 /* If there are any excess server data in the input buffer,
2909 * release it and close the connection ASAP (some data may
2910 * remain in the output buffer). This happens if a server sends
2911 * invalid responses. So in such case, we don't want to reuse
2912 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002913 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002914 if (b_data(&h1c->ibuf)) {
2915 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002916 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002917 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002918 goto release;
2919 }
Christopher Faulet03627242019-07-19 11:34:08 +02002920
Christopher Faulet08016ab2020-07-01 16:10:06 +02002921 if (h1c->conn->flags & CO_FL_PRIVATE) {
2922 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002923 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2924 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002925 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002926 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002927 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002928 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002929 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002930 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002931 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2932 goto end;
2933 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002934 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002935 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002936 if (h1c->conn->owner == sess)
2937 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002938 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002939 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002940 /* The server doesn't want it, let's kill the connection right away */
2941 h1c->conn->mux->destroy(h1c);
2942 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2943 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002944 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002945 /* At this point, the connection has been added to the
2946 * server idle list, so another thread may already have
2947 * hijacked it, so we can't do anything with it.
2948 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002949 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002950 }
2951 }
2952
Christopher Fauletf1204b82019-07-19 14:51:06 +02002953 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002954 /* 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 +01002955 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002956 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002957 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002958 !h1c->conn->owner) {
2959 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002960 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002961 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002962 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002963 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002964 /* If we have a new request, process it immediately or
2965 * subscribe for reads waiting for new data
2966 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01002967 if (unlikely(b_data(&h1c->ibuf))) {
2968 if (h1_process(h1c) == -1)
2969 goto end;
2970 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002971 else
2972 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2973 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002974 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002975 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002976 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002977 end:
2978 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002979}
2980
2981
2982static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2983{
2984 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002985 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002986
2987 if (!h1s)
2988 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002989 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002990
Christopher Faulet6b81df72019-10-01 22:08:43 +02002991 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2992
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002993 if (cs->flags & CS_FL_SHR)
2994 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002995 if (cs->flags & CS_FL_KILL_CONN) {
2996 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2997 goto do_shutr;
2998 }
2999 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3000 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003001 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003002 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003003
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003004 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3005 /* Here attached is implicit because there is CS */
3006 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3007 goto end;
3008 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003009 if (h1s->flags & H1S_F_WANT_KAL) {
3010 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003011 goto end;
3012 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003013
Christopher Faulet7f366362019-04-08 10:51:20 +02003014 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003015 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
3016 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003017 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003018 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003019 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02003020 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003021 end:
3022 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003023}
3024
3025static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3026{
3027 struct h1s *h1s = cs->ctx;
3028 struct h1c *h1c;
3029
3030 if (!h1s)
3031 return;
3032 h1c = h1s->h1c;
3033
Christopher Faulet6b81df72019-10-01 22:08:43 +02003034 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3035
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003036 if (cs->flags & CS_FL_SHW)
3037 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003038 if (cs->flags & CS_FL_KILL_CONN) {
3039 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003040 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003041 }
3042 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3043 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3044 goto do_shutw;
3045 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003046
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003047 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3048 /* Here attached is implicit because there is CS */
3049 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3050 goto end;
3051 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003052 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3053 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003054 goto end;
3055 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003056
Christopher Faulet7f366362019-04-08 10:51:20 +02003057 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003058 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003059 if (!b_data(&h1c->obuf))
3060 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003061 end:
3062 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003063}
3064
Christopher Faulet666a0c42019-01-08 11:12:04 +01003065static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003066{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003067 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003068
Christopher Faulet6b81df72019-10-01 22:08:43 +02003069 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003070 conn_xprt_shutw(conn);
3071 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003072 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003073}
3074
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003075/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3076 * The <es> pointer is not allowed to differ from the one passed to the
3077 * subscribe() call. It always returns zero.
3078 */
3079static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003080{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003081 struct h1s *h1s = cs->ctx;
3082
3083 if (!h1s)
3084 return 0;
3085
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003086 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003087 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003088
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003089 es->events &= ~event_type;
3090 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003091 h1s->subs = NULL;
3092
3093 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003094 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003095
3096 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003097 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003098
Christopher Faulet51dbc942018-09-13 09:05:15 +02003099 return 0;
3100}
3101
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003102/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3103 * event subscriber <es> is not allowed to change from a previous call as long
3104 * as at least one event is still subscribed. The <event_type> must only be a
3105 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3106 * the conn_stream <cs> was already detached, in which case it will return -1.
3107 */
3108static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003109{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003110 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003111 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003112
3113 if (!h1s)
3114 return -1;
3115
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003116 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003117 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003118
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003119 es->events |= event_type;
3120 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003121
3122 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003123 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003124
3125
Christopher Faulet6b81df72019-10-01 22:08:43 +02003126 if (event_type & SUB_RETRY_SEND) {
3127 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003128 /*
3129 * If the conn_stream attempt to subscribe, and the
3130 * mux isn't subscribed to the connection, then it
3131 * probably means the connection wasn't established
3132 * yet, so we have to subscribe.
3133 */
3134 h1c = h1s->h1c;
3135 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3136 h1c->conn->xprt->subscribe(h1c->conn,
3137 h1c->conn->xprt_ctx,
3138 SUB_RETRY_SEND,
3139 &h1c->wait_event);
3140 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003141 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003142}
3143
3144/* Called from the upper layer, to receive data */
3145static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3146{
3147 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003148 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003149 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003150 size_t ret = 0;
3151
Willy Tarreau022e5e52020-09-10 09:33:15 +02003152 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003153
3154 /* Do nothing for now if not READY */
3155 if (!(h1c->flags & H1C_F_ST_READY)) {
3156 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3157 goto end;
3158 }
3159
Christopher Faulet539e0292018-11-19 10:40:09 +01003160 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02003161 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003162 else
3163 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003164
Christopher Faulete18777b2019-04-16 16:46:36 +02003165 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02003166 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02003167 h1c->flags |= H1C_F_WANT_SPLICE;
3168 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 +02003169 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003170 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003171 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003172 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003173 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003174 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003175
3176 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003177 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003178 return ret;
3179}
3180
3181
3182/* Called from the upper layer, to send data */
3183static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3184{
3185 struct h1s *h1s = cs->ctx;
3186 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003187 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003188
3189 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003190 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003191 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003192
Willy Tarreau022e5e52020-09-10 09:33:15 +02003193 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003194
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003195 /* If we're not connected yet, or we're waiting for a handshake, stop
3196 * now, as we don't want to remove everything from the channel buffer
3197 * before we're sure we can send it.
3198 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003199 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003200 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003201 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003202 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003203
Christopher Fauletdea24742021-01-22 15:12:30 +01003204 if (h1c->flags & H1C_F_ST_ERROR) {
3205 cs->flags |= CS_FL_ERROR;
3206 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);
3207 return 0;
3208 }
3209
Christopher Faulet46230362019-10-17 16:04:20 +02003210 /* Inherit some flags from the upper layer */
3211 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3212 if (flags & CO_SFL_MSG_MORE)
3213 h1c->flags |= H1C_F_CO_MSG_MORE;
3214 if (flags & CO_SFL_STREAMER)
3215 h1c->flags |= H1C_F_CO_STREAMER;
3216
Christopher Fauletc31872f2019-06-04 22:09:36 +02003217 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003218 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003219
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003220 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3221 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003222 else
3223 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003224
3225 if ((count - ret) > 0)
3226 h1c->flags |= H1C_F_CO_MSG_MORE;
3227
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003228 if (!ret)
3229 break;
3230 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003231 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003232 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003233 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003234 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003235
3236 if (h1c->flags & H1C_F_ST_ERROR) {
3237 TRACE_DEVEL("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
3238 cs->flags |= CS_FL_ERROR;
3239 }
3240
Christopher Faulet7a145d62020-08-05 11:31:16 +02003241 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003242 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003243 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003244}
3245
Willy Tarreaue5733232019-05-22 19:24:06 +02003246#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003247/* Send and get, using splicing */
3248static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3249{
3250 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003251 struct h1c *h1c = h1s->h1c;
3252 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003253 int ret = 0;
3254
Willy Tarreau022e5e52020-09-10 09:33:15 +02003255 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003256
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003257 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003258 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003259 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 +02003260 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003261 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003262 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003263 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003264 goto end;
3265 }
3266
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003267 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3268 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003269 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3270 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003271 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003272 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003273 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003274 }
3275
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003276 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003277 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3278 goto end;
3279 }
3280
Christopher Faulet1be55f92018-10-02 15:59:23 +02003281 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3282 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003283 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003284 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003285 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003286 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003287 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003288 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003289 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003290 }
3291
Christopher Faulet1be55f92018-10-02 15:59:23 +02003292 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003293 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003294 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003295 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003296 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003297 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003298
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003299 if ((h1s->flags & H1S_F_REOS) ||
3300 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003301 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003302 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003303 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003304 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003305
Willy Tarreau022e5e52020-09-10 09:33:15 +02003306 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003307 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003308}
3309
3310static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3311{
3312 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003313 int ret = 0;
3314
Willy Tarreau022e5e52020-09-10 09:33:15 +02003315 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003316
Christopher Faulet1be55f92018-10-02 15:59:23 +02003317 if (b_data(&h1s->h1c->obuf))
3318 goto end;
3319
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003320 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003321 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003322 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003323 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3324 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003325 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003326 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003327 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003328
Willy Tarreau022e5e52020-09-10 09:33:15 +02003329 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003330 return ret;
3331}
3332#endif
3333
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003334static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3335{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003336 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003337 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003338
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003339 switch (mux_ctl) {
3340 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003341 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003342 ret |= MUX_STATUS_READY;
3343 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003344 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003345 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3346 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003347 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3348 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3349 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003350 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003351 default:
3352 return -1;
3353 }
3354}
3355
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003356/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003357static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003358{
3359 struct h1c *h1c = conn->ctx;
3360 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003361 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003362
Christopher Fauletf376a312019-01-04 15:16:06 +01003363 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3364 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003365 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3366 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3367 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3368 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3369
3370 if (h1s) {
3371 char *method;
3372
3373 if (h1s->meth < HTTP_METH_OTHER)
3374 method = http_known_methods[h1s->meth].ptr;
3375 else
3376 method = "UNKNOWN";
3377 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3378 " .meth=%s status=%d",
3379 h1s, h1s->flags,
3380 h1m_state_str(h1s->req.state),
3381 h1m_state_str(h1s->res.state), method, h1s->status);
3382 if (h1s->cs)
3383 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3384 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003385
3386 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3387 if (h1s->subs) {
3388 if (h1s->subs) {
3389 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3390 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3391 h1s->subs->tasklet->calls,
3392 h1s->subs->tasklet->context);
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003393 if (h1s->subs->tasklet->calls >= 1000000)
3394 ret = 1;
Willy Tarreau150c4f82021-01-20 17:05:58 +01003395 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3396 chunk_appendf(&trash, ")");
3397 }
3398 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003399 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003400 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003401}
3402
3403
3404/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3405static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3406{
3407 struct h1_hdr_entry *entry;
3408
3409 /* Be sure there is a non-empty <to> */
3410 if (!strlen(to)) {
3411 memprintf(err, "expect <to>");
3412 return -1;
3413 }
3414
3415 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003416 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003417 memprintf(err, "<from> and <to> must not differ execpt the case");
3418 return -1;
3419 }
3420
3421 /* Be sure <from> does not already existsin the tree */
3422 if (ebis_lookup(&hdrs_map.map, from)) {
3423 memprintf(err, "duplicate entry '%s'", from);
3424 return -1;
3425 }
3426
3427 /* Create the entry and insert it in the tree */
3428 entry = malloc(sizeof(*entry));
3429 if (!entry) {
3430 memprintf(err, "out of memory");
3431 return -1;
3432 }
3433
3434 entry->node.key = strdup(from);
3435 entry->name.ptr = strdup(to);
3436 entry->name.len = strlen(to);
3437 if (!entry->node.key || !entry->name.ptr) {
3438 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003439 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003440 free(entry);
3441 memprintf(err, "out of memory");
3442 return -1;
3443 }
3444 ebis_insert(&hdrs_map.map, &entry->node);
3445 return 0;
3446}
3447
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003448/* Migrate the the connection to the current thread.
3449 * Return 0 if successful, non-zero otherwise.
3450 * Expected to be called with the old thread lock held.
3451 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003452static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003453{
3454 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003455 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003456
3457 if (fd_takeover(conn->handle.fd, conn) != 0)
3458 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003459
3460 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3461 /* We failed to takeover the xprt, even if the connection may
3462 * still be valid, flag it as error'd, as we have already
3463 * taken over the fd, and wake the tasklet, so that it will
3464 * destroy it.
3465 */
3466 conn->flags |= CO_FL_ERROR;
3467 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3468 return -1;
3469 }
3470
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003471 if (h1c->wait_event.events)
3472 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3473 h1c->wait_event.events, &h1c->wait_event);
3474 /* To let the tasklet know it should free itself, and do nothing else,
3475 * set its context to NULL.
3476 */
3477 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003478 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003479
3480 task = h1c->task;
3481 if (task) {
3482 task->context = NULL;
3483 h1c->task = NULL;
3484 __ha_barrier_store();
3485 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003486
3487 h1c->task = task_new(tid_bit);
3488 if (!h1c->task) {
3489 h1_release(h1c);
3490 return -1;
3491 }
3492 h1c->task->process = h1_timeout_task;
3493 h1c->task->context = h1c;
3494 }
3495 h1c->wait_event.tasklet = tasklet_new();
3496 if (!h1c->wait_event.tasklet) {
3497 h1_release(h1c);
3498 return -1;
3499 }
3500 h1c->wait_event.tasklet->process = h1_io_cb;
3501 h1c->wait_event.tasklet->context = h1c;
3502 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3503 SUB_RETRY_RECV, &h1c->wait_event);
3504
3505 return 0;
3506}
3507
3508
Christopher Faulet98fbe952019-07-22 16:18:24 +02003509static void h1_hdeaders_case_adjust_deinit()
3510{
3511 struct ebpt_node *node, *next;
3512 struct h1_hdr_entry *entry;
3513
3514 node = ebpt_first(&hdrs_map.map);
3515 while (node) {
3516 next = ebpt_next(node);
3517 ebpt_delete(node);
3518 entry = container_of(node, struct h1_hdr_entry, node);
3519 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003520 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003521 free(entry);
3522 node = next;
3523 }
3524 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003525}
3526
Christopher Faulet98fbe952019-07-22 16:18:24 +02003527static int cfg_h1_headers_case_adjust_postparser()
3528{
3529 FILE *file = NULL;
3530 char *c, *key_beg, *key_end, *value_beg, *value_end;
3531 char *err;
3532 int rc, line = 0, err_code = 0;
3533
3534 if (!hdrs_map.name)
3535 goto end;
3536
3537 file = fopen(hdrs_map.name, "r");
3538 if (!file) {
3539 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3540 hdrs_map.name);
3541 err_code |= ERR_ALERT | ERR_FATAL;
3542 goto end;
3543 }
3544
3545 /* now parse all lines. The file may contain only two header name per
3546 * line, separated by spaces. All heading and trailing spaces will be
3547 * ignored. Lines starting with a # are ignored.
3548 */
3549 while (fgets(trash.area, trash.size, file) != NULL) {
3550 line++;
3551 c = trash.area;
3552
3553 /* strip leading spaces and tabs */
3554 while (*c == ' ' || *c == '\t')
3555 c++;
3556
3557 /* ignore emptu lines, or lines beginning with a dash */
3558 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3559 continue;
3560
3561 /* look for the end of the key */
3562 key_beg = c;
3563 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3564 c++;
3565 key_end = c;
3566
3567 /* strip middle spaces and tabs */
3568 while (*c == ' ' || *c == '\t')
3569 c++;
3570
3571 /* look for the end of the value, it is the end of the line */
3572 value_beg = c;
3573 while (*c && *c != '\n' && *c != '\r')
3574 c++;
3575 value_end = c;
3576
3577 /* trim possibly trailing spaces and tabs */
3578 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3579 value_end--;
3580
3581 /* set final \0 and check entries */
3582 *key_end = '\0';
3583 *value_end = '\0';
3584
3585 err = NULL;
3586 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3587 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003588 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3589 hdrs_map.name, err, line);
3590 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003591 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003592 goto end;
3593 }
3594 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003595 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3596 hdrs_map.name, err, line);
3597 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003598 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003599 }
3600 }
3601
3602 end:
3603 if (file)
3604 fclose(file);
3605 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3606 return err_code;
3607}
3608
3609
3610/* config parser for global "h1-outgoing-header-case-adjust" */
3611static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3612 struct proxy *defpx, const char *file, int line,
3613 char **err)
3614{
3615 if (too_many_args(2, args, err, NULL))
3616 return -1;
3617 if (!*(args[1]) || !*(args[2])) {
3618 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3619 return -1;
3620 }
3621 return add_hdr_case_adjust(args[1], args[2], err);
3622}
3623
3624/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3625static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3626 struct proxy *defpx, const char *file, int line,
3627 char **err)
3628{
3629 if (too_many_args(1, args, err, NULL))
3630 return -1;
3631 if (!*(args[1])) {
3632 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3633 return -1;
3634 }
3635 free(hdrs_map.name);
3636 hdrs_map.name = strdup(args[1]);
3637 return 0;
3638}
3639
3640
3641/* config keyword parsers */
3642static struct cfg_kw_list cfg_kws = {{ }, {
3643 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3644 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3645 { 0, NULL, NULL },
3646 }
3647};
3648
3649INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3650REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3651
3652
Christopher Faulet51dbc942018-09-13 09:05:15 +02003653/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003654/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003655/****************************************/
3656
3657/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003658static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003659 .init = h1_init,
3660 .wake = h1_wake,
3661 .attach = h1_attach,
3662 .get_first_cs = h1_get_first_cs,
3663 .detach = h1_detach,
3664 .destroy = h1_destroy,
3665 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003666 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003667 .rcv_buf = h1_rcv_buf,
3668 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003669#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003670 .rcv_pipe = h1_rcv_pipe,
3671 .snd_pipe = h1_snd_pipe,
3672#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003673 .subscribe = h1_subscribe,
3674 .unsubscribe = h1_unsubscribe,
3675 .shutr = h1_shutr,
3676 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003677 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003678 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003679 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003680 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003681 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003682 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003683};
3684
3685
3686/* this mux registers default HTX proto */
3687static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003688{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003689
Willy Tarreau0108d902018-11-25 19:14:37 +01003690INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3691
Christopher Faulet51dbc942018-09-13 09:05:15 +02003692/*
3693 * Local variables:
3694 * c-indent-level: 8
3695 * c-basic-offset: 8
3696 * End:
3697 */