blob: 113cb8f745bc9dc57c1a045928b6a7816efd59b6 [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 Faulet60ef12c2020-09-24 10:05:44 +020082
Christopher Faulet2eed8002020-12-07 11:26:13 +010083#define H1S_F_PARSING_DONE 0x00000200 /* Set when incoming message parsing is finished (EOM added) */
84
85#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);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200566 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100567 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200568
569 return b_data(&h1s->h1c->ibuf);
570}
571
Christopher Faulet16df1782020-12-04 16:47:41 +0100572/* Creates a new conn-stream and the associate stream. <input> is used as input
573 * buffer for the stream. On success, it is transferred to the stream and the
574 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
575 * mux must still take care of it. However, there is nothing special to do
576 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
577 * b_free() on it is always safe. This function returns the conn-stream on
578 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200579static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100580{
581 struct conn_stream *cs;
582
Christopher Faulet6b81df72019-10-01 22:08:43 +0200583 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200584 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200585 if (!cs) {
586 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 +0100587 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200588 }
Christopher Faulet47365272018-10-31 17:40:50 +0100589 h1s->cs = cs;
590 cs->ctx = h1s;
591
592 if (h1s->flags & H1S_F_NOT_FIRST)
593 cs->flags |= CS_FL_NOT_FIRST;
594
Christopher Faulet27182292020-07-03 15:08:49 +0200595 if (global.tune.options & GTUNE_USE_SPLICE) {
596 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100597 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200598 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100599
Christopher Faulet26256f82020-09-14 11:40:13 +0200600 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200601 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 +0100602 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200603 }
604
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100605 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 +0200606 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100607 return cs;
608
609 err:
610 cs_free(cs);
611 h1s->cs = NULL;
612 return NULL;
613}
614
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100615static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
616{
617 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
618
619 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
620 TRACE_DEVEL("leaving on stream upgrade failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
621 goto err;
622 }
623
624 if (global.tune.options & GTUNE_USE_SPLICE) {
625 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
626 h1s->cs->flags |= CS_FL_MAY_SPLICE;
627 }
628
629 h1s->h1c->flags |= H1C_F_ST_READY;
630 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
631 return h1s->cs;
632
633 err:
634 return NULL;
635}
636
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200637static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200638{
639 struct h1s *h1s;
640
Christopher Faulet6b81df72019-10-01 22:08:43 +0200641 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
642
Christopher Faulet51dbc942018-09-13 09:05:15 +0200643 h1s = pool_alloc(pool_head_h1s);
644 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100645 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200646 h1s->h1c = h1c;
647 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200648 h1s->sess = NULL;
649 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100650 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100651 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200652 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200653
654 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100655 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200656
Christopher Faulet129817b2018-09-20 16:14:40 +0200657 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100658 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200659
660 h1s->status = 0;
661 h1s->meth = HTTP_METH_OTHER;
662
Christopher Faulet47365272018-10-31 17:40:50 +0100663 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
664 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100665 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100666
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200667 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
668 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200669
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200670 fail:
671 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
672 return NULL;
673}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100674
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200675static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
676{
677 struct session *sess = h1c->conn->owner;
678 struct h1s *h1s;
679
680 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
681
682 h1s = h1s_new(h1c);
683 if (!h1s)
684 goto fail;
685
686 h1s->sess = sess;
687
688 if (h1c->px->options2 & PR_O2_REQBUG_OK)
689 h1s->req.err_pos = -1;
690
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200691 h1c->idle_exp = TICK_ETERNITY;
692 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200693 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200694 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100695
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200696 fail:
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200697 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
698 return NULL;
699}
700
701static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
702{
703 struct h1s *h1s;
704
705 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
706
707 h1s = h1s_new(h1c);
708 if (!h1s)
709 goto fail;
710
711 h1s->cs = cs;
712 h1s->sess = sess;
713 cs->ctx = h1s;
714
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100715 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200716
717 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
718 h1s->res.err_pos = -1;
719
720 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
721 return h1s;
722
723 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200724 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 +0100725 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200726}
727
728static void h1s_destroy(struct h1s *h1s)
729{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200730 if (h1s) {
731 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200732
Christopher Faulet6b81df72019-10-01 22:08:43 +0200733 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200734 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200735
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100736 if (h1s->subs)
737 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200738
Christopher Fauletd17ad822020-09-24 15:14:29 +0200739 h1_release_buf(h1c, &h1s->rxbuf);
740
Christopher Fauletb385b502021-01-13 18:47:57 +0100741 h1c->flags &= ~(H1C_F_WAIT_INPUT|H1C_F_WAIT_OUTPUT|H1C_F_WANT_SPLICE|
742 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200743 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
744 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200745 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100746 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200747 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
748 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100749
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100750 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100751 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100752 (h1s->flags & (H1S_F_WANT_KAL|H1S_F_PARSING_DONE)) == (H1S_F_WANT_KAL|H1S_F_PARSING_DONE) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100753 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100754 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100755 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
756 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200757 else {
758 TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100759 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200760 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200761 pool_free(pool_head_h1s, h1s);
762 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200763}
764
765/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200766 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500767 * to the existing conn_stream (for outgoing connections or for incoming ones
768 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200769 * establishment). <input> is always used as Input buffer and may contain
770 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
771 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200772 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200773static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
774 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200775{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200776 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100777 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200778 void *conn_ctx = conn->ctx;
779
780 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200781
782 h1c = pool_alloc(pool_head_h1c);
783 if (!h1c)
784 goto fail_h1c;
785 h1c->conn = conn;
786 h1c->px = proxy;
787
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100788 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200789 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200790 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200791 h1c->obuf = BUF_NULL;
792 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200793 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200794
Willy Tarreau21046592020-02-26 10:39:36 +0100795 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200796 h1c->wait_event.tasklet = tasklet_new();
797 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200798 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200799 h1c->wait_event.tasklet->process = h1_io_cb;
800 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100801 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200802 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200803
Christopher Faulete9b70722019-04-08 10:46:02 +0200804 if (conn_is_back(conn)) {
Christopher Fauletb385b502021-01-13 18:47:57 +0100805 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OUTPUT);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100806 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
807 if (tick_isset(proxy->timeout.serverfin))
808 h1c->shut_timeout = proxy->timeout.serverfin;
809 } else {
810 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
811 if (tick_isset(proxy->timeout.clientfin))
812 h1c->shut_timeout = proxy->timeout.clientfin;
813 }
814 if (tick_isset(h1c->timeout)) {
815 t = task_new(tid_bit);
816 if (!t)
817 goto fail;
818
819 h1c->task = t;
820 t->process = h1_timeout_task;
821 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200822
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100823 t->expire = tick_add(now_ms, h1c->timeout);
824 }
825
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100826 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200827
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200828 if (h1c->flags & H1C_F_IS_BACK) {
829 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200830 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
831 goto fail;
832 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100833 else if (conn_ctx) {
834 /* Upgraded frontend connection (from TCP) */
835 struct conn_stream *cs = conn_ctx;
836
837 if (!h1c_frt_stream_new(h1c))
838 goto fail;
839
840 h1c->h1s->cs = cs;
841 cs->ctx = h1c->h1s;
842
843 /* Attach the CS but Not ready yet */
844 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
845 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
846 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
847 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100848
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200849 if (t) {
850 h1_set_idle_expiration(h1c);
851 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100852 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200853 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100854
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200855 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100856 if (b_data(&h1c->ibuf))
857 tasklet_wakeup(h1c->wait_event.tasklet);
858 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200859 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200860
861 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200862 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200863 return 0;
864
865 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200866 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200867 if (h1c->wait_event.tasklet)
868 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200869 pool_free(pool_head_h1c, h1c);
870 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200871 conn->ctx = conn_ctx; // restore saved context
872 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200873 return -1;
874}
875
Christopher Faulet73c12072019-04-08 11:23:22 +0200876/* release function. This one should be called to free all resources allocated
877 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200878 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200879static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200880{
Christopher Faulet61840e72019-04-15 09:33:32 +0200881 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200882
Christopher Faulet6b81df72019-10-01 22:08:43 +0200883 TRACE_POINT(H1_EV_H1C_END);
884
Christopher Faulet51dbc942018-09-13 09:05:15 +0200885 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200886 /* The connection must be aattached to this mux to be released */
887 if (h1c->conn && h1c->conn->ctx == h1c)
888 conn = h1c->conn;
889
Christopher Faulet6b81df72019-10-01 22:08:43 +0200890 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
891
Christopher Faulet61840e72019-04-15 09:33:32 +0200892 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200893 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200894 /* Make sure we're no longer subscribed to anything */
895 if (h1c->wait_event.events)
896 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
897 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200898 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200899 /* connection successfully upgraded to H2, this
900 * mux was already released */
901 return;
902 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200903 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200904 sess_log(conn->owner); /* Log if the upgrade failed */
905 }
906
Olivier Houchard45c44372019-06-11 14:06:23 +0200907
Willy Tarreau21046592020-02-26 10:39:36 +0100908 if (MT_LIST_ADDED(&h1c->buf_wait.list))
909 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200910
911 h1_release_buf(h1c, &h1c->ibuf);
912 h1_release_buf(h1c, &h1c->obuf);
913
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100914 if (h1c->task) {
915 h1c->task->context = NULL;
916 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
917 h1c->task = NULL;
918 }
919
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200920 if (h1c->wait_event.tasklet)
921 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200922
Christopher Fauletf2824e62018-10-01 12:12:37 +0200923 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200924 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100925 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200926 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200927 pool_free(pool_head_h1c, h1c);
928 }
929
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200930 if (conn) {
931 conn->mux = NULL;
932 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200933 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200934
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200935 conn_stop_tracking(conn);
936 conn_full_close(conn);
937 if (conn->destroy_cb)
938 conn->destroy_cb(conn);
939 conn_free(conn);
940 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200941}
942
943/******************************************************/
944/* functions below are for the H1 protocol processing */
945/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200946/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
947 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200948 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100949static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200950{
Christopher Faulet570d1612018-11-26 11:13:57 +0100951 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200952
Christopher Faulet570d1612018-11-26 11:13:57 +0100953 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200954 (*(p + 5) > '1' ||
955 (*(p + 5) == '1' && *(p + 7) >= '1')))
956 h1m->flags |= H1_MF_VER_11;
957}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200958
Christopher Faulet9768c262018-10-22 09:34:31 +0200959/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
960 * greater or equal to 1.1
961 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100962static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200963{
Christopher Faulet570d1612018-11-26 11:13:57 +0100964 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200965
Christopher Faulet570d1612018-11-26 11:13:57 +0100966 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200967 (*(p + 5) > '1' ||
968 (*(p + 5) == '1' && *(p + 7) >= '1')))
969 h1m->flags |= H1_MF_VER_11;
970}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200971
Christopher Fauletf2824e62018-10-01 12:12:37 +0200972/* Deduce the connection mode of the client connection, depending on the
973 * configuration and the H1 message flags. This function is called twice, the
974 * first time when the request is parsed and the second time when the response
975 * is parsed.
976 */
977static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
978{
979 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200980
981 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100982 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100983 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100984 h1s->status == 101) {
985 /* Either we've established an explicit tunnel, or we're
986 * switching the protocol. In both cases, we're very unlikely to
987 * understand the next protocols. We have to switch to tunnel
988 * mode, so that we transfer the request and responses then let
989 * this protocol pass unmodified. When we later implement
990 * specific parsers for such protocols, we'll want to check the
991 * Upgrade header which contains information about that protocol
992 * for responses with status 101 (eg: see RFC2817 about TLS).
993 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200994 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200995 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 +0100996 }
997 else if (h1s->flags & H1S_F_WANT_KAL) {
998 /* By default the client is in KAL mode. CLOSE mode mean
999 * it is imposed by the client itself. So only change
1000 * KAL mode here. */
1001 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1002 /* no length known or explicit close => close */
1003 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001004 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 +01001005 }
1006 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1007 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001008 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001009 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001010 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 +01001011 }
1012 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001013 }
1014 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001015 /* Input direction: first pass */
1016 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1017 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001018 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001019 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 +01001020 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001021 }
1022
1023 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001024 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001025 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001026 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);
1027 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001028}
1029
1030/* Deduce the connection mode of the client connection, depending on the
1031 * configuration and the H1 message flags. This function is called twice, the
1032 * first time when the request is parsed and the second time when the response
1033 * is parsed.
1034 */
1035static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1036{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001037 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001038 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001039 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001040
Christopher Fauletf2824e62018-10-01 12:12:37 +02001041 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001042 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001043 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001044 h1s->status == 101) {
1045 /* Either we've established an explicit tunnel, or we're
1046 * switching the protocol. In both cases, we're very unlikely to
1047 * understand the next protocols. We have to switch to tunnel
1048 * mode, so that we transfer the request and responses then let
1049 * this protocol pass unmodified. When we later implement
1050 * specific parsers for such protocols, we'll want to check the
1051 * Upgrade header which contains information about that protocol
1052 * for responses with status 101 (eg: see RFC2817 about TLS).
1053 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001054 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001055 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 +01001056 }
1057 else if (h1s->flags & H1S_F_WANT_KAL) {
1058 /* By default the server is in KAL mode. CLOSE mode mean
1059 * it is imposed by haproxy itself. So only change KAL
1060 * mode here. */
1061 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1062 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1063 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1064 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001065 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 +01001066 }
1067 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001068 }
Christopher Faulet70033782018-12-05 13:50:11 +01001069 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001070 /* Output direction: first pass */
1071 if (h1m->flags & H1_MF_CONN_CLO) {
1072 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001073 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001074 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 +01001075 }
1076 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1077 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1078 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1079 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1080 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1081 /* no explicit keep-alive option httpclose/server-close => close */
1082 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001083 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 +01001084 }
Christopher Faulet70033782018-12-05 13:50:11 +01001085 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001086
1087 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001088 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001089 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001090 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);
1091 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001092}
1093
Christopher Fauletb992af02019-03-28 15:42:24 +01001094static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001095{
1096 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001097
1098 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1099 * token is found
1100 */
1101 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001102 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001103
1104 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001105 if (!(h1m->flags & H1_MF_VER_11)) {
1106 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 +01001107 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001108 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001109 }
1110 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001111 if (h1m->flags & H1_MF_VER_11) {
1112 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001113 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001114 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001115 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116}
1117
Christopher Fauletb992af02019-03-28 15:42:24 +01001118static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001119{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001120 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1121 * token is found
1122 */
1123 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001124 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001125
1126 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001127 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001128 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1129 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 +01001130 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001131 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001132 }
1133 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001134 if (h1m->flags & H1_MF_VER_11) {
1135 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001136 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001137 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001138 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001139}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001140
Christopher Fauletb992af02019-03-28 15:42:24 +01001141static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001142{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001143 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001144 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001145 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001146 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001147}
1148
Christopher Fauletb992af02019-03-28 15:42:24 +01001149static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1150{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001151 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001152 h1_set_cli_conn_mode(h1s, h1m);
1153 else
1154 h1_set_srv_conn_mode(h1s, h1m);
1155
1156 if (!(h1m->flags & H1_MF_RESP))
1157 h1_update_req_conn_value(h1s, h1m, conn_val);
1158 else
1159 h1_update_res_conn_value(h1s, h1m, conn_val);
1160}
Christopher Faulete44769b2018-11-29 23:01:45 +01001161
Christopher Faulet98fbe952019-07-22 16:18:24 +02001162/* Try to adjust the case of the message header name using the global map
1163 * <hdrs_map>.
1164 */
1165static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1166{
1167 struct ebpt_node *node;
1168 struct h1_hdr_entry *entry;
1169
1170 /* No entry in the map, do nothing */
1171 if (eb_is_empty(&hdrs_map.map))
1172 return;
1173
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001174 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001175 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1176 return;
1177
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001178 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001179 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1180 return;
1181
1182 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1183 if (!node)
1184 return;
1185 entry = container_of(node, struct h1_hdr_entry, node);
1186 name->ptr = entry->name.ptr;
1187 name->len = entry->name.len;
1188}
1189
Christopher Faulete44769b2018-11-29 23:01:45 +01001190/* Append the description of what is present in error snapshot <es> into <out>.
1191 * The description must be small enough to always fit in a buffer. The output
1192 * buffer may be the trash so the trash must not be used inside this function.
1193 */
1194static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1195{
1196 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001197 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1198 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1199 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1200 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1201 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1202 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001203}
1204/*
1205 * Capture a bad request or response and archive it in the proxy's structure.
1206 * By default it tries to report the error position as h1m->err_pos. However if
1207 * this one is not set, it will then report h1m->next, which is the last known
1208 * parsing point. The function is able to deal with wrapping buffers. It always
1209 * displays buffers as a contiguous area starting at buf->p. The direction is
1210 * determined thanks to the h1m's flags.
1211 */
1212static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1213 struct h1m *h1m, struct buffer *buf)
1214{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001215 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001216 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001217 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001218 union error_snapshot_ctx ctx;
1219
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001220 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001221 if (sess == NULL)
1222 sess = si_strm(h1s->cs->data)->sess;
1223 if (!(h1m->flags & H1_MF_RESP))
1224 other_end = si_strm(h1s->cs->data)->be;
1225 else
1226 other_end = sess->fe;
1227 } else
1228 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001229
1230 /* http-specific part now */
1231 ctx.h1.state = h1m->state;
1232 ctx.h1.c_flags = h1c->flags;
1233 ctx.h1.s_flags = h1s->flags;
1234 ctx.h1.m_flags = h1m->flags;
1235 ctx.h1.m_clen = h1m->curr_len;
1236 ctx.h1.m_blen = h1m->body_len;
1237
1238 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1239 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001240 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1241 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001242}
1243
Christopher Fauletadb22202018-12-12 10:32:09 +01001244/* Emit the chunksize followed by a CRLF in front of data of the buffer
1245 * <buf>. It goes backwards and starts with the byte before the buffer's
1246 * head. The caller is responsible for ensuring there is enough room left before
1247 * the buffer's head for the string.
1248 */
1249static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1250{
1251 char *beg, *end;
1252
1253 beg = end = b_head(buf);
1254 *--beg = '\n';
1255 *--beg = '\r';
1256 do {
1257 *--beg = hextab[chksz & 0xF];
1258 } while (chksz >>= 4);
1259 buf->head -= (end - beg);
1260 b_add(buf, end - beg);
1261}
1262
1263/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1264 * ensuring there is enough room left in the buffer for the string. */
1265static void h1_emit_chunk_crlf(struct buffer *buf)
1266{
1267 *(b_peek(buf, b_data(buf))) = '\r';
1268 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1269 b_add(buf, 2);
1270}
1271
Christopher Faulet129817b2018-09-20 16:14:40 +02001272/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001273 * Switch the stream to tunnel mode. This function must only be called on 2xx
1274 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001275 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001276static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001277{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001278 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001279
Christopher Faulet5be651d2021-01-22 15:28:03 +01001280 h1s->req.state = H1_MSG_TUNNEL;
1281 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001282
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001283 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001284 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001285
Christopher Faulet5be651d2021-01-22 15:28:03 +01001286 TRACE_STATE("switch H1 stream in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1287 if (h1c->flags & H1C_F_IS_BACK)
1288 h1s->flags &= ~H1S_F_PARSING_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01001289
Christopher Faulet5be651d2021-01-22 15:28:03 +01001290 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1291 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1292 if (b_data(&h1c->ibuf))
1293 h1_wake_stream_for_recv(h1s);
1294 tasklet_wakeup(h1c->wait_event.tasklet);
1295 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 +02001296 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01001297 if (h1c->flags & H1C_F_WAIT_INPUT) {
1298 h1c->flags &= ~H1C_F_WAIT_INPUT;
1299 h1_wake_stream_for_send(h1s);
1300 if (b_data(&h1c->obuf))
1301 tasklet_wakeup(h1c->wait_event.tasklet);
1302 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 +01001303 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001304}
1305
1306/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001307 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001308 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001309 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001310 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001311static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001312 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001313{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001314 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001315 int ret = 0;
1316
Willy Tarreau022e5e52020-09-10 09:33:15 +02001317 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 +02001318
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001319 if (h1s->meth == HTTP_METH_CONNECT)
1320 h1m->flags |= H1_MF_METH_CONNECT;
1321 if (h1s->meth == HTTP_METH_HEAD)
1322 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001323
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001324 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1325 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001326 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 +02001327 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001328 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001329 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 +02001330 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1331 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001332 goto end;
1333 }
1334
Christopher Faulet486498c2019-10-11 14:22:00 +02001335 if (h1m->err_pos >= 0) {
1336 /* Maybe we found an error during the parsing while we were
1337 * configured not to block on that, so we have to capture it
1338 * now.
1339 */
1340 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1341 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1342 }
1343
Christopher Faulet5be651d2021-01-22 15:28:03 +01001344 if (!(h1m->flags & H1_MF_RESP))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001345 h1s->meth = h1sl.rq.meth;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001346 else
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001347 h1s->status = h1sl.st.status;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001348
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 Faulet76014fd2019-12-10 11:47:22 +01001418 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
Christopher Fauleta583af62020-09-24 15:35:37 +02001419 * proceed. This functions is responsible to update the parser state <h1m>.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001420 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001421static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1422 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001423{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001424 int ret;
1425
Willy Tarreau022e5e52020-09-10 09:33:15 +02001426 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet76014fd2019-12-10 11:47:22 +01001427 ret = h1_parse_msg_eom(h1m, htx, max);
1428 if (!ret) {
1429 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1430 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001431 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001432 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001433 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1434 }
1435 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001436 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001437
Christopher Faulet76014fd2019-12-10 11:47:22 +01001438 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001439 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001440 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet76014fd2019-12-10 11:47:22 +01001441 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001442}
1443
1444/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001445 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001446 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1447 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001448 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001449static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001450{
Christopher Faulet539e0292018-11-19 10:40:09 +01001451 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001452 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001453 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001454 size_t data;
1455 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001456 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001457
Christopher Faulet539e0292018-11-19 10:40:09 +01001458 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001459 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001460
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001461 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001462 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001463
Christopher Faulet2eed8002020-12-07 11:26:13 +01001464 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001465 goto end;
1466
Christopher Faulet5be651d2021-01-22 15:28:03 +01001467 if (h1c->flags & H1C_F_WAIT_OUTPUT)
1468 goto end;
1469
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001470 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001471 size_t used = htx_used_space(htx);
1472
Christopher Faulet129817b2018-09-20 16:14:40 +02001473 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001474 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001475 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001476 if (!ret)
1477 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001478
1479 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1480 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1481
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001482 /* Reject Protocol upgrade request with payload */
1483 if ((h1m->flags & (H1_MF_RESP|H1_MF_CONN_UPG)) == H1_MF_CONN_UPG && h1m->state != H1_MSG_DONE) {
1484 h1s->flags |= H1S_F_NOT_IMPL_ERROR;
1485 TRACE_USER("Upgrade with body not implemented, reject H1 message",
1486 H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1487 break;
1488 }
1489
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001490 if ((h1m->flags & H1_MF_RESP) &&
1491 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1492 h1m_init_res(&h1s->res);
1493 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001494 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001495 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001496 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001497 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001498 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001499 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001500 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001501 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001502
1503 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1504 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001505 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001506 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001507 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1508 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1509 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001510 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001511
Christopher Faulet76014fd2019-12-10 11:47:22 +01001512 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1513 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001514 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001515 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001516 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1517 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1518 break;
1519
1520 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1521 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1522 }
1523
Christopher Faulet5be651d2021-01-22 15:28:03 +01001524 if ((h1m->flags & H1_MF_RESP) &&
1525 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1526 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001527 else {
1528 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1529 /* Unfinished transaction: block this input side waiting the end of the output side */
1530 h1c->flags |= H1C_F_WAIT_OUTPUT;
1531 TRACE_STATE("Disable read on h1c (wait_output)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1532 }
1533 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1534 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1535 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1536 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1537 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001538 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001539 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001540 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001541 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001542 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001543 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001544 if (!ret)
1545 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001546
1547 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1548 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001549 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001550 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001551 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001552 break;
1553 }
1554
Christopher Faulet30db3d72019-05-17 15:35:33 +02001555 count -= htx_used_space(htx) - used;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001556 } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) && !(h1c->flags & H1C_F_WAIT_OUTPUT));
1557
Christopher Faulet129817b2018-09-20 16:14:40 +02001558
Christopher Faulet2eed8002020-12-07 11:26:13 +01001559 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
1560 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 +02001561 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001562 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001563
Christopher Faulet539e0292018-11-19 10:40:09 +01001564 b_del(&h1c->ibuf, total);
1565
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001566 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001567 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1568
Christopher Faulet30db3d72019-05-17 15:35:33 +02001569 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001570 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001571 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001572 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 +01001573 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001574 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001575
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001576 if (!b_data(&h1c->ibuf))
1577 h1_release_buf(h1c, &h1c->ibuf);
1578
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001579 if (!(h1c->flags & H1C_F_ST_READY)) {
1580 /* The H1 connection is not ready. Most of time, there is no CS
1581 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1582 * cases, it is only possible on the client side.
1583 */
1584 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1585
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001586 if (h1m->state <= H1_MSG_LAST_LF) {
1587 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1588 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1589 goto end;
1590 }
1591
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001592 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1593 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1594 BUG_ON(h1s->cs);
1595 if (!h1s_new_cs(h1s, buf)) {
1596 h1c->flags |= H1C_F_ST_ERROR;
1597 goto err;
1598 }
1599 }
1600 else {
1601 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1602 BUG_ON(h1s->cs == NULL);
1603 if (!h1s_upgrade_cs(h1s, buf)) {
1604 h1c->flags |= H1C_F_ST_ERROR;
1605 goto err;
1606 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001607 }
1608 }
1609
1610 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001611 if (!(h1m->flags & H1_MF_CHNK) &&
1612 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1613 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1614 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1615 }
1616 else {
1617 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1618 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1619 }
1620
Christopher Faulet5be651d2021-01-22 15:28:03 +01001621 /* Don't set EOI on the conn-stream for protocol upgrade or connect
1622 * requests, wait the response to do so or not depending on the status
1623 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001624 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001625 if ((h1s->flags & H1S_F_PARSING_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG))
Christopher Fauleta583af62020-09-24 15:35:37 +02001626 h1s->cs->flags |= CS_FL_EOI;
1627
Christopher Faulet6716cc22019-12-20 17:33:24 +01001628 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001629 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001630 else {
1631 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1632 if (h1s->flags & H1S_F_REOS) {
1633 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001634 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1635 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1636 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001637 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001638 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001639 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
1640 h1s->cs->flags |= CS_FL_ERROR;
Christopher Fauletb385b502021-01-13 18:47:57 +01001641
1642 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1643 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1644 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1645 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1646 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001647 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001648 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001649
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001650 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001651 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001652 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001653
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001654 err:
Christopher Faulet47365272018-10-31 17:40:50 +01001655 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001656 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001657 if (h1s->cs)
1658 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001659 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001661}
1662
Christopher Faulet129817b2018-09-20 16:14:40 +02001663/*
1664 * Process outgoing data. It parses data and transfer them from the channel buffer into
1665 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1666 * 0 if it couldn't proceed.
1667 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001668static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1669{
Christopher Faulet129817b2018-09-20 16:14:40 +02001670 struct h1s *h1s = h1c->h1s;
1671 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001672 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001673 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001674 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001675 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001676
Christopher Faulet47365272018-10-31 17:40:50 +01001677 if (!count)
1678 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001679
Christopher Faulet94b2c762019-05-24 15:28:57 +02001680 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001681 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1682
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001683 if (htx_is_empty(chn_htx))
1684 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001685
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001686 if (h1s->flags & H1S_F_PROCESSING_ERROR)
1687 goto end;
1688
Christopher Fauletdea24742021-01-22 15:12:30 +01001689 if (h1c->flags & H1C_F_WAIT_INPUT)
1690 goto end;
1691
Christopher Faulet51dbc942018-09-13 09:05:15 +02001692 if (!h1_get_buf(h1c, &h1c->obuf)) {
1693 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001694 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 +02001695 goto end;
1696 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001697
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001698 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001699
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001700 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001701 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001702
Willy Tarreau3815b222018-12-11 19:50:43 +01001703 /* Perform some optimizations to reduce the number of buffer copies.
1704 * First, if the mux's buffer is empty and the htx area contains
1705 * exactly one data block of the same size as the requested count,
1706 * then it's possible to simply swap the caller's buffer with the
1707 * mux's output buffer and adjust offsets and length to match the
1708 * entire DATA HTX block in the middle. In this case we perform a
1709 * true zero-copy operation from end-to-end. This is the situation
1710 * that happens all the time with large files. Second, if this is not
1711 * possible, but the mux's output buffer is empty, we still have an
1712 * opportunity to avoid the copy to the intermediary buffer, by making
1713 * the intermediary buffer's area point to the output buffer's area.
1714 * In this case we want to skip the HTX header to make sure that copies
1715 * remain aligned and that this operation remains possible all the
1716 * time. This goes for headers, data blocks and any data extracted from
1717 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001718 */
1719 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001720 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
1721 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001722 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001723 htx_get_blk_value(chn_htx, blk).len == count) {
1724 void *old_area = h1c->obuf.area;
1725
Christopher Faulet6b81df72019-10-01 22:08:43 +02001726 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001727 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001728 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001729 h1c->obuf.data = count;
1730
1731 buf->area = old_area;
1732 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001733
Christopher Faulet6b81df72019-10-01 22:08:43 +02001734 chn_htx = (struct htx *)buf->area;
1735 htx_reset(chn_htx);
1736
Christopher Fauletadb22202018-12-12 10:32:09 +01001737 /* The message is chunked. We need to emit the chunk
1738 * size. We have at least the size of the struct htx to
1739 * write the chunk envelope. It should be enough.
1740 */
1741 if (h1m->flags & H1_MF_CHNK) {
1742 h1_emit_chunk_size(&h1c->obuf, count);
1743 h1_emit_chunk_crlf(&h1c->obuf);
1744 }
1745
Willy Tarreau3815b222018-12-11 19:50:43 +01001746 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001747 if (h1m->state == H1_MSG_DATA)
1748 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001749 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001750 else
1751 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001752 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001753 goto out;
1754 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001755 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001756 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001757 else
1758 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001759
Christopher Fauletc2518a52019-06-25 21:41:02 +02001760 tmp.data = 0;
1761 tmp.size = b_room(&h1c->obuf);
Christopher Fauletdea24742021-01-22 15:12:30 +01001762 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && !(h1c->flags & H1C_F_WAIT_INPUT) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001763 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001764 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001765 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001766 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001767 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001768
Christopher Fauletb2e84162018-12-06 11:39:49 +01001769 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001770 if (type != HTX_BLK_DATA && vlen > count)
1771 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001772
Christopher Faulet94b2c762019-05-24 15:28:57 +02001773 if (type == HTX_BLK_UNUSED)
1774 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001775
Christopher Faulet94b2c762019-05-24 15:28:57 +02001776 switch (h1m->state) {
1777 case H1_MSG_RQBEFORE:
1778 if (type != HTX_BLK_REQ_SL)
1779 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001780 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 +02001781 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001782 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001783 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001784 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001785 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001786 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001787 if (sl->flags & HTX_SL_F_BODYLESS)
1788 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001789 h1m->state = H1_MSG_HDR_FIRST;
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 Fauletd1ebb1e2018-11-28 16:32:50 +01001808 if (sl->info.res.status < 200 &&
1809 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001810 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001811 h1m->state = H1_MSG_HDR_FIRST;
1812 break;
1813
Christopher Faulet94b2c762019-05-24 15:28:57 +02001814 case H1_MSG_HDR_FIRST:
1815 case H1_MSG_HDR_NAME:
1816 case H1_MSG_HDR_L2_LWS:
1817 if (type == HTX_BLK_EOH)
1818 goto last_lf;
1819 if (type != HTX_BLK_HDR)
1820 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001821
Christopher Faulet9768c262018-10-22 09:34:31 +02001822 h1m->state = H1_MSG_HDR_NAME;
1823 n = htx_get_blk_name(chn_htx, blk);
1824 v = htx_get_blk_value(chn_htx, blk);
1825
Christopher Faulet86d144c2019-08-14 16:32:25 +02001826 /* Skip all pseudo-headers */
1827 if (*(n.ptr) == ':')
1828 goto skip_hdr;
1829
Christopher Fauletb045bb22020-02-28 10:42:20 +01001830 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001831 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001832 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001833 /* Only skip C-L header with invalid value. */
1834 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001835 goto skip_hdr;
1836 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001837 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001838 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001839 if (!v.len)
1840 goto skip_hdr;
1841 }
1842
Christopher Faulet67d58092019-10-02 10:51:38 +02001843 /* Skip header if same name is used to add the server name */
1844 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1845 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1846 goto skip_hdr;
1847
Christopher Faulet98fbe952019-07-22 16:18:24 +02001848 /* Try to adjust the case of the header name */
1849 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1850 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001851 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001852 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001853 skip_hdr:
1854 h1m->state = H1_MSG_HDR_L2_LWS;
1855 break;
1856
Christopher Faulet94b2c762019-05-24 15:28:57 +02001857 case H1_MSG_LAST_LF:
1858 if (type != HTX_BLK_EOH)
1859 goto error;
1860 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001861 h1m->state = H1_MSG_LAST_LF;
1862 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001863 /* If the reply comes from haproxy while the request is
1864 * not finished, we force the connection close. */
1865 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1866 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1867 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1868 }
1869
1870 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001871 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001872 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001873 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001874 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001875 /* Try to adjust the case of the header name */
1876 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1877 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001878 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001879 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001880 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001881 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001882 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001883
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001884 if ((h1s->meth != HTTP_METH_CONNECT &&
1885 (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 +01001886 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Fauletc75668e2020-12-07 18:10:32 +01001887 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && h1s->meth != HTTP_METH_HEAD &&
1888 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001889 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1890 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001891 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001892 n = ist("transfer-encoding");
1893 v = ist("chunked");
1894 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1895 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001896 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001897 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001898 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001899 h1m->flags |= H1_MF_CHNK;
1900 }
1901
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001902 /* Now add the server name to a header (if requested) */
1903 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1904 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1905 struct server *srv = objt_server(h1c->conn->target);
1906
1907 if (srv) {
1908 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1909 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001910
1911 /* Try to adjust the case of the header name */
1912 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1913 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001914 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001915 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001916 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001917 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001918 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1919 }
1920
Christopher Fauletc2518a52019-06-25 21:41:02 +02001921 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001922 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001923
Christopher Faulet6b81df72019-10-01 22:08:43 +02001924 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1925 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1926
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001927 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet5be651d2021-01-22 15:28:03 +01001928 /* Must have a EOM before tunnel data */
1929 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001930 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001931 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001932 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Faulet5be651d2021-01-22 15:28:03 +01001933 /* Must have a EOM before tunnel data */
1934 h1m->state = H1_MSG_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)) {
1938 h1m_init_res(&h1s->res);
1939 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001940 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001941 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001942 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001943 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001944 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001945 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1946 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001947 else
1948 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001949 break;
1950
Christopher Faulet94b2c762019-05-24 15:28:57 +02001951 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001952 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001953 if (type == HTX_BLK_EOM) {
1954 /* Chunked message without explicit trailers */
1955 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001956 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001957 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001958 }
1959 goto done;
1960 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001961 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001962 /* If the message is not chunked, never
1963 * add the last chunk. */
1964 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001965 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001966 TRACE_PROTO("sending message trailers", H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s, chn_htx);
Christopher Faulet94b2c762019-05-24 15:28:57 +02001967 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001968 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001969 else if (type != HTX_BLK_DATA)
1970 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001971
1972 TRACE_PROTO("sending message data", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){sz});
Christopher Fauletd9233f02019-10-14 14:01:24 +02001973
1974
1975 if (vlen > count) {
1976 /* Get the maximum amount of data we can xferred */
1977 vlen = count;
1978 }
1979
1980 chklen = 0;
1981 if (h1m->flags & H1_MF_CHNK) {
1982 chklen = b_room(&tmp);
1983 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1984 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1985 (chklen < 1048576) ? 5 : 8);
1986 chklen += 4; /* 2 x CRLF */
1987 }
1988
1989 if (vlen + chklen > b_room(&tmp)) {
1990 /* too large for the buffer */
1991 if (chklen >= b_room(&tmp))
1992 goto full;
1993 vlen = b_room(&tmp) - chklen;
1994 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001995 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001996 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001997 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001998 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001999
2000 if (h1m->state == H1_MSG_DATA)
2001 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002002 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002003 else
2004 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002005 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02002006 break;
2007
Christopher Faulet94b2c762019-05-24 15:28:57 +02002008 case H1_MSG_TRAILERS:
2009 if (type == HTX_BLK_EOM)
2010 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002011 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002012 goto error;
2013 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002014 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002015 /* If the message is not chunked, ignore
2016 * trailers. It may happen with H2 messages. */
2017 if (!(h1m->flags & H1_MF_CHNK))
2018 break;
2019
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002020 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002021 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002022 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002023 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2024 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01002025 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002026 else { // HTX_BLK_TLR
2027 n = htx_get_blk_name(chn_htx, blk);
2028 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002029
2030 /* Try to adjust the case of the header name */
2031 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2032 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002033 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002034 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002035 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002036 break;
2037
Christopher Faulet94b2c762019-05-24 15:28:57 +02002038 case H1_MSG_DONE:
2039 if (type != HTX_BLK_EOM)
2040 goto error;
2041 done:
2042 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002043 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
2044 h1c->flags |= H1C_F_WAIT_INPUT;
2045 TRACE_STATE("Disable send on h1c (wait_input)", H1_EV_TX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
2046 }
2047 else if ((h1m->flags & H1_MF_RESP) &&
2048 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2049 /* a successful reply to a CONNECT or a protocol switching is sent
2050 * to the client. Switch the response to tunnel mode.
2051 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002052 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002053 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 +01002054 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002055
2056 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
Christopher Fauletb385b502021-01-13 18:47:57 +01002057 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002058 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02002059 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 +02002060 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002061
2062 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2063 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002064 break;
2065
Christopher Faulet9768c262018-10-22 09:34:31 +02002066 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002067 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002068 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002069 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002070 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002071 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002072 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002073 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2074 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002075 break;
2076 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002077
2078 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002079 total += vlen;
2080 count -= vlen;
2081 if (sz == vlen)
2082 blk = htx_remove_blk(chn_htx, blk);
2083 else {
2084 htx_cut_data_blk(chn_htx, blk, vlen);
2085 break;
2086 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002087 }
2088
Christopher Faulet9768c262018-10-22 09:34:31 +02002089 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002090 /* when the output buffer is empty, tmp shares the same area so that we
2091 * only have to update pointers and lengths.
2092 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002093 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2094 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002095 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002096 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002097
Willy Tarreau3815b222018-12-11 19:50:43 +01002098 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002099 out:
2100 if (!buf_room_for_htx_data(&h1c->obuf)) {
2101 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002102 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002103 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002104 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002105 /* Both the request and the response reached the DONE state. So set EOI
2106 * flag on the conn-stream. Most of time, the flag will already be set,
2107 * except for protocol upgrades. Report an error if data remains blocked
2108 * in the output buffer.
2109 */
2110 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2111 if (!htx_is_empty(chn_htx)) {
2112 h1c->flags |= H1C_F_ST_ERROR;
2113 TRACE_STATE("txn done but data waiting to be sent, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
2114 }
2115 h1s->cs->flags |= CS_FL_EOI;
2116 }
2117
Christopher Faulet6b81df72019-10-01 22:08:43 +02002118 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002119 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002120
2121 full:
2122 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2123 h1c->flags |= H1C_F_OUT_FULL;
2124 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002125}
2126
Christopher Faulet51dbc942018-09-13 09:05:15 +02002127/*********************************************************/
2128/* functions below are I/O callbacks from the connection */
2129/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002130static void h1_wake_stream_for_recv(struct h1s *h1s)
2131{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002132 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002133 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002134 tasklet_wakeup(h1s->subs->tasklet);
2135 h1s->subs->events &= ~SUB_RETRY_RECV;
2136 if (!h1s->subs->events)
2137 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002138 }
2139}
2140static void h1_wake_stream_for_send(struct h1s *h1s)
2141{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002142 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002143 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002144 tasklet_wakeup(h1s->subs->tasklet);
2145 h1s->subs->events &= ~SUB_RETRY_SEND;
2146 if (!h1s->subs->events)
2147 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002148 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002149}
2150
Christopher Fauletad4daf62021-01-21 17:49:01 +01002151/* alerts the data layer following this sequence :
2152 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2153 * - if its subscribed to send, then it's woken up for send
2154 * - if it was subscribed to neither, its ->wake() callback is called
2155 */
2156static void h1_alert(struct h1s *h1s)
2157{
2158 if (h1s->subs) {
2159 h1_wake_stream_for_recv(h1s);
2160 h1_wake_stream_for_send(h1s);
2161 }
2162 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2163 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2164 h1s->cs->data_cb->wake(h1s->cs);
2165 }
2166}
2167
Christopher Fauletc18fc232020-10-06 17:41:36 +02002168/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2169 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2170 * retryable errors (allocation error or buffer full). On success, the error is
2171 * copied in the output buffer.
2172*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002173static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002174{
2175 int rc = http_get_status_idx(h1c->errcode);
2176 int ret = 0;
2177
2178 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2179
2180 /* Verify if the error is mapped on /dev/null or any empty file */
2181 /// XXX: do a function !
2182 if (h1c->px->replies[rc] &&
2183 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2184 h1c->px->replies[rc]->body.errmsg &&
2185 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2186 /* Empty error, so claim a success */
2187 ret = 1;
2188 goto out;
2189 }
2190
2191 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2192 h1c->flags |= H1C_F_ERR_PENDING;
2193 goto out;
2194 }
2195
2196 if (!h1_get_buf(h1c, &h1c->obuf)) {
2197 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2198 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2199 goto out;
2200 }
2201 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2202 if (unlikely(ret <= 0)) {
2203 if (!ret) {
2204 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2205 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2206 goto out;
2207 }
2208 else {
2209 /* we cannot report this error, so claim a success */
2210 ret = 1;
2211 }
2212 }
2213 h1c->flags &= ~H1C_F_ERR_PENDING;
2214 out:
2215 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2216 return ret;
2217}
2218
2219/* Try to send a 500 internal error. It relies on h1_send_error to send the
2220 * error. This function takes care of incrementing stats and tracked counters.
2221 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002222static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002223{
2224 struct session *sess = h1c->conn->owner;
2225 int ret = 1;
2226
2227 session_inc_http_req_ctr(sess);
2228 session_inc_http_err_ctr(sess);
2229 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2230 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[5], 1);
2231 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
2232 if (sess->listener->counters)
2233 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
2234
2235 h1c->errcode = 500;
2236 ret = h1_send_error(h1c);
2237 sess_log(sess);
2238 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002239}
2240
Christopher Fauletc18fc232020-10-06 17:41:36 +02002241/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2242 * error. This function takes care of incrementing stats and tracked counters.
2243 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002244static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002245{
2246 struct session *sess = h1c->conn->owner;
2247 int ret = 1;
2248
2249 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2250 goto end;
2251
2252 session_inc_http_req_ctr(sess);
2253 session_inc_http_err_ctr(sess);
2254 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2255 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2256 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2257 if (sess->listener->counters)
2258 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2259
2260 h1c->errcode = 400;
2261 ret = h1_send_error(h1c);
2262 sess_log(sess);
2263
2264 end:
2265 return ret;
2266}
2267
Christopher Faulet2eed8002020-12-07 11:26:13 +01002268/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2269 * the error. This function takes care of incrementing stats and tracked
2270 * counters.
2271 */
2272static int h1_handle_not_impl_err(struct h1c *h1c)
2273{
2274 struct session *sess = h1c->conn->owner;
2275 int ret = 1;
2276
2277 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2278 goto end;
2279
2280 session_inc_http_req_ctr(sess);
2281 session_inc_http_err_ctr(sess);
2282 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2283 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2284 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2285 if (sess->listener->counters)
2286 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2287
2288 h1c->errcode = 501;
2289 ret = h1_send_error(h1c);
2290 sess_log(sess);
2291
2292 end:
2293 return ret;
2294}
2295
Christopher Fauletc18fc232020-10-06 17:41:36 +02002296/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2297 * error. This function takes care of incrementing stats and tracked counters.
2298 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002299static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002300{
2301 struct session *sess = h1c->conn->owner;
2302 int ret = 1;
2303
2304 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2305 goto end;
2306
2307 session_inc_http_req_ctr(sess);
2308 session_inc_http_err_ctr(sess);
2309 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2310 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2311 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2312 if (sess->listener->counters)
2313 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2314
2315 h1c->errcode = 408;
2316 ret = h1_send_error(h1c);
2317 sess_log(sess);
2318 end:
2319 return ret;
2320}
2321
2322
Christopher Faulet51dbc942018-09-13 09:05:15 +02002323/*
2324 * Attempt to read data, and subscribe if none available
2325 */
2326static int h1_recv(struct h1c *h1c)
2327{
2328 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002329 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002330 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002331
Christopher Faulet6b81df72019-10-01 22:08:43 +02002332 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2333
2334 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2335 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002336 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002337 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002338
Christopher Fauletae635762020-09-21 11:47:16 +02002339 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2340 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002341 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002342 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002343
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002344 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2345 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002346 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002347 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002348 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002349
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002350 /*
2351 * If we only have a small amount of data, realign it,
2352 * it's probably cheaper than doing 2 recv() calls.
2353 */
2354 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2355 b_slow_realign(&h1c->ibuf, trash.area, 0);
2356
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002357 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002358 if (!h1c->h1s ||
2359 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2360 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002361 flags |= CO_RFL_READ_ONCE;
2362
Willy Tarreau45f2b892018-12-05 07:59:27 +01002363 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002364 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002365 if (h1c->flags & H1C_F_IN_FULL) {
2366 h1c->flags &= ~H1C_F_IN_FULL;
2367 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2368 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002369
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002370 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002371 if (!b_data(&h1c->ibuf)) {
2372 /* try to pre-align the buffer like the rxbufs will be
2373 * to optimize memory copies.
2374 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002375 h1c->ibuf.head = sizeof(struct htx);
2376 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002377 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002378 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002379 if (max && !ret && h1_recv_allowed(h1c)) {
2380 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2381 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002382 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002383 else {
2384 h1_wake_stream_for_recv(h1c->h1s);
2385 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002386 }
2387
Christopher Faulet51dbc942018-09-13 09:05:15 +02002388 if (!b_data(&h1c->ibuf))
2389 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002390 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002392 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2393 }
2394
2395 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002396 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002397}
2398
2399
2400/*
2401 * Try to send data if possible
2402 */
2403static int h1_send(struct h1c *h1c)
2404{
2405 struct connection *conn = h1c->conn;
2406 unsigned int flags = 0;
2407 size_t ret;
2408 int sent = 0;
2409
Christopher Faulet6b81df72019-10-01 22:08:43 +02002410 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2411
2412 if (conn->flags & CO_FL_ERROR) {
2413 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002414 b_reset(&h1c->obuf);
2415 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002416 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002417
2418 if (!b_data(&h1c->obuf))
2419 goto end;
2420
Christopher Faulet46230362019-10-17 16:04:20 +02002421 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002422 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002423 if (h1c->flags & H1C_F_CO_STREAMER)
2424 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002425
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002426 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002427 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002428 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002429 if (h1c->flags & H1C_F_OUT_FULL) {
2430 h1c->flags &= ~H1C_F_OUT_FULL;
2431 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2432 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002433 b_del(&h1c->obuf, ret);
2434 sent = 1;
2435 }
2436
Christopher Faulet145aa472018-12-06 10:56:20 +01002437 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002438 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002439 /* error or output closed, nothing to send, clear the buffer to release it */
2440 b_reset(&h1c->obuf);
2441 }
2442
Christopher Faulet51dbc942018-09-13 09:05:15 +02002443 end:
Christopher Fauletb385b502021-01-13 18:47:57 +01002444 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_WAIT_INPUT)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002445 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002446
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447 /* We're done, no more to send */
2448 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002449 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002450 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002451 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002452 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002453 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002454 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002455 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002456 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2457 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002458 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002459 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002460
Christopher Faulet6b81df72019-10-01 22:08:43 +02002461 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002462 return sent;
2463}
2464
Christopher Faulet51dbc942018-09-13 09:05:15 +02002465/* callback called on any event by the connection handler.
2466 * It applies changes and returns zero, or < 0 if it wants immediate
2467 * destruction of the connection.
2468 */
2469static int h1_process(struct h1c * h1c)
2470{
2471 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002472 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002473
Christopher Faulet6b81df72019-10-01 22:08:43 +02002474 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2475
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002476 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002477 if (b_data(&h1c->ibuf) && /* Input data to be processed */
2478 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2479 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002480 struct buffer *buf;
2481 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002482
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002483 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2484 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002485 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002486
2487 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002488 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
2489 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE)) { /* H2 upgrade supported by the proxy */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002490 /* Try to match H2 preface before parsing the request headers. */
2491 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2492 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002493 if (h1c->flags & H1C_F_ST_ATTACHED) {
2494 /* Force the REOS here to be sure to release the CS.
2495 Here ATTACHED implies !READY, and h1s defined
2496 */
2497 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2498 h1s->flags |= H1S_F_REOS;
2499 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002500 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002501 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002502 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002503 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002504
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002505 /* Create the H1 stream if not already there */
2506 if (!h1s) {
2507 h1s = h1c_frt_stream_new(h1c);
2508 if (!h1s) {
2509 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002510 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002511 goto no_parsing;
2512 }
2513 }
2514
2515 if (h1s->sess->t_idle == -1)
2516 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2517
2518 /* Get the stream rxbuf */
2519 buf = h1_get_buf(h1c, &h1s->rxbuf);
2520 if (!buf) {
2521 h1c->flags |= H1C_F_IN_SALLOC;
2522 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2523 return 0;
2524 }
2525
2526 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2527 h1_process_input(h1c, buf, count);
2528 h1_release_buf(h1c, &h1s->rxbuf);
2529 h1_set_idle_expiration(h1c);
2530
2531 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002532 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002533 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002534 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002535 }
2536 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2537 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002538 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002539 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002540 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2541 h1_handle_not_impl_err(h1c);
2542 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
2543 }
Christopher Fauletae635762020-09-21 11:47:16 +02002544 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002545 h1_send(h1c);
2546
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002547 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002548 if (!(h1c->flags & H1C_F_ST_READY)) {
2549 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002550 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002551 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002552 if (h1_handle_bad_req(h1c))
2553 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002554 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002555 }
2556
2557 /* Handle pending error, if any (only possible on frontend connection) */
2558 if (h1c->flags & H1C_F_ERR_PENDING) {
2559 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2560 if (h1_send_error(h1c))
2561 h1_send(h1c);
2562 }
Christopher Fauletae635762020-09-21 11:47:16 +02002563
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002564 /* If there is some pending outgoing data or error, just wait */
2565 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2566 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002567
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002568 /* Otherwise we can release the H1 connection */
2569 goto release;
2570 }
2571 else {
2572 /* Here there is still a H1 stream with a conn-stream.
2573 * Report the connection state at the stream level
2574 */
2575 if (conn_xprt_read0_pending(conn)) {
2576 h1s->flags |= H1S_F_REOS;
2577 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2578 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002579 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002580 h1s->cs->flags |= CS_FL_ERROR;
2581 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002582 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002583 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002584 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002585
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002586 if (!b_data(&h1c->ibuf))
2587 h1_release_buf(h1c, &h1c->ibuf);
2588
2589
2590 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2591 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2592 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002593 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002594
Christopher Faulet47365272018-10-31 17:40:50 +01002595 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002596 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002597 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002598 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002599
2600 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002601 if (h1c->flags & H1C_F_ST_ATTACHED) {
2602 /* Don't release the H1 connetion right now, we must destroy the
2603 * attached CS first. Here, the H1C must not be READY */
2604 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2605
2606 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2607 h1s->cs->flags |= CS_FL_EOS;
2608 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2609 h1s->cs->flags |= CS_FL_ERROR;
2610 h1_alert(h1s);
2611 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2612 }
2613 else {
2614 h1_release(h1c);
2615 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2616 }
Christopher Faulet539e0292018-11-19 10:40:09 +01002617 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002618}
2619
Willy Tarreau691d5032021-01-20 14:55:01 +01002620struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002621{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002622 struct connection *conn;
2623 struct tasklet *tl = (struct tasklet *)t;
2624 int conn_in_list;
2625 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002626 int ret = 0;
2627
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002628
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002629 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002630 if (tl->context == NULL) {
2631 /* The connection has been taken over by another thread,
2632 * we're no longer responsible for it, so just free the
2633 * tasklet, and do nothing.
2634 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002635 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002636 tasklet_free(tl);
2637 return NULL;
2638 }
2639 h1c = ctx;
2640 conn = h1c->conn;
2641
2642 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2643
2644 /* Remove the connection from the list, to be sure nobody attempts
2645 * to use it while we handle the I/O events
2646 */
2647 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2648 if (conn_in_list)
2649 MT_LIST_DEL(&conn->list);
2650
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002651 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002652
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002653 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002654 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002655 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002656 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002657 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002658 ret = h1_process(h1c);
2659 /* If we were in an idle list, we want to add it back into it,
2660 * unless h1_process() returned -1, which mean it has destroyed
2661 * the connection (testing !ret is enough, if h1_process() wasn't
2662 * called then ret will be 0 anyway.
2663 */
2664 if (!ret && conn_in_list) {
2665 struct server *srv = objt_server(conn->target);
2666
2667 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002668 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002669 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002670 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002671 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002672 return NULL;
2673}
2674
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002675static void h1_reset(struct connection *conn)
2676{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002677
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002678}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002679
2680static int h1_wake(struct connection *conn)
2681{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002682 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002683 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002684
Christopher Faulet6b81df72019-10-01 22:08:43 +02002685 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2686
Christopher Faulet539e0292018-11-19 10:40:09 +01002687 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002688 ret = h1_process(h1c);
2689 if (ret == 0) {
2690 struct h1s *h1s = h1c->h1s;
2691
Christopher Fauletad4daf62021-01-21 17:49:01 +01002692 if (h1c->flags & H1C_F_ST_ATTACHED)
2693 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002694 }
2695 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002696}
2697
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002698/* Connection timeout management. The principle is that if there's no receipt
2699 * nor sending for a certain amount of time, the connection is closed.
2700 */
2701static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2702{
2703 struct h1c *h1c = context;
2704 int expired = tick_is_expired(t->expire, now_ms);
2705
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002706 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002707
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002708 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002709 /* Make sure nobody stole the connection from us */
2710 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2711
2712 /* Somebody already stole the connection from us, so we should not
2713 * free it, we just have to free the task.
2714 */
2715 if (!t->context) {
2716 h1c = NULL;
2717 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2718 goto do_leave;
2719 }
2720
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002721 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002722 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2723 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002724 return t;
2725 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002726
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002727 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002728 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002729 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002730 if (h1c->flags & H1C_F_ST_READY) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002731 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2732 t->expire = TICK_ETERNITY;
2733 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2734 return t;
2735 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002736
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002737 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002738 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2739 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002740 if (h1_handle_req_tout(h1c))
2741 h1_send(h1c);
2742 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2743 h1_refresh_timeout(h1c);
Christopher Fauletcc043f62020-12-14 10:06:12 +01002744 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002745 return t;
2746 }
2747 }
2748
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002749 if (h1c->flags & H1C_F_ST_ATTACHED) {
2750 /* Don't release the H1 connetion right now, we must destroy the
2751 * attached CS first. Here, the H1C must not be READY */
2752 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
2753 h1_alert(h1c->h1s);
2754 h1_refresh_timeout(h1c);
2755 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2756 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2757 return t;
2758 }
2759
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002760 /* We're about to destroy the connection, so make sure nobody attempts
2761 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002762 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002763 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002764 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002765
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002766 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002767 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002768
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002769 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002770 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002771
2772 if (!h1c) {
2773 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002774 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002775 return NULL;
2776 }
2777
2778 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002779 h1_release(h1c);
2780 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002781 return NULL;
2782}
2783
Christopher Faulet51dbc942018-09-13 09:05:15 +02002784/*******************************************/
2785/* functions below are used by the streams */
2786/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002787
Christopher Faulet51dbc942018-09-13 09:05:15 +02002788/*
2789 * Attach a new stream to a connection
2790 * (Used for outgoing connections)
2791 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002792static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002793{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002794 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002795 struct conn_stream *cs = NULL;
2796 struct h1s *h1s;
2797
Christopher Faulet6b81df72019-10-01 22:08:43 +02002798 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002799 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002800 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 +02002801 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002802 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002803
Christopher Faulet236c93b2020-07-02 09:19:54 +02002804 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002805 if (!cs) {
2806 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 +02002807 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002808 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002809
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002810 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002811 if (h1s == NULL) {
2812 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 +02002813 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002814 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002815
Christopher Faulet6b81df72019-10-01 22:08:43 +02002816 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002817 return cs;
2818 end:
2819 cs_free(cs);
2820 return NULL;
2821}
2822
2823/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2824 * this mux, it's easy as we can only store a single conn_stream.
2825 */
2826static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2827{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002828 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002829 struct h1s *h1s = h1c->h1s;
2830
2831 if (h1s)
2832 return h1s->cs;
2833
2834 return NULL;
2835}
2836
Christopher Faulet73c12072019-04-08 11:23:22 +02002837static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002838{
Christopher Faulet73c12072019-04-08 11:23:22 +02002839 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002840
Christopher Faulet6b81df72019-10-01 22:08:43 +02002841 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002842 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002843 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002844}
2845
2846/*
2847 * Detach the stream from the connection and possibly release the connection.
2848 */
2849static void h1_detach(struct conn_stream *cs)
2850{
2851 struct h1s *h1s = cs->ctx;
2852 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002853 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002854 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002855
Christopher Faulet6b81df72019-10-01 22:08:43 +02002856 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2857
Christopher Faulet51dbc942018-09-13 09:05:15 +02002858 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002859 if (!h1s) {
2860 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002861 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002862 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002863
Olivier Houchardf502aca2018-12-14 19:42:40 +01002864 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002865 h1c = h1s->h1c;
2866 h1s->cs = NULL;
2867
Christopher Faulet42849b02020-10-05 11:35:17 +02002868 sess->accept_date = date;
2869 sess->tv_accept = now;
2870 sess->t_handshake = 0;
2871 sess->t_idle = -1;
2872
Olivier Houchard8a786902018-12-15 16:05:40 +01002873 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2874 h1s_destroy(h1s);
2875
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002876 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 +02002877 /* If there are any excess server data in the input buffer,
2878 * release it and close the connection ASAP (some data may
2879 * remain in the output buffer). This happens if a server sends
2880 * invalid responses. So in such case, we don't want to reuse
2881 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002882 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002883 if (b_data(&h1c->ibuf)) {
2884 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002885 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002886 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002887 goto release;
2888 }
Christopher Faulet03627242019-07-19 11:34:08 +02002889
Christopher Faulet08016ab2020-07-01 16:10:06 +02002890 if (h1c->conn->flags & CO_FL_PRIVATE) {
2891 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002892 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2893 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002894 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002895 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002896 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002897 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002898 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002899 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002900 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2901 goto end;
2902 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002903 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002904 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002905 if (h1c->conn->owner == sess)
2906 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002907 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002908 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002909 /* The server doesn't want it, let's kill the connection right away */
2910 h1c->conn->mux->destroy(h1c);
2911 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2912 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002913 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002914 /* At this point, the connection has been added to the
2915 * server idle list, so another thread may already have
2916 * hijacked it, so we can't do anything with it.
2917 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002918 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002919 }
2920 }
2921
Christopher Fauletf1204b82019-07-19 14:51:06 +02002922 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002923 /* 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 +01002924 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002925 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002926 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002927 !h1c->conn->owner) {
2928 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002929 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002930 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002931 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002932 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002933 /* If we have a new request, process it immediately or
2934 * subscribe for reads waiting for new data
2935 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01002936 if (unlikely(b_data(&h1c->ibuf))) {
2937 if (h1_process(h1c) == -1)
2938 goto end;
2939 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002940 else
2941 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2942 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002943 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002944 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002945 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002946 end:
2947 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002948}
2949
2950
2951static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2952{
2953 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002954 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002955
2956 if (!h1s)
2957 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002958 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002959
Christopher Faulet6b81df72019-10-01 22:08:43 +02002960 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2961
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002962 if (cs->flags & CS_FL_SHR)
2963 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002964 if (cs->flags & CS_FL_KILL_CONN) {
2965 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2966 goto do_shutr;
2967 }
2968 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2969 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002970 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002971 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002972
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002973 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
2974 /* Here attached is implicit because there is CS */
2975 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2976 goto end;
2977 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002978 if (h1s->flags & H1S_F_WANT_KAL) {
2979 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002980 goto end;
2981 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002982
Christopher Faulet7f366362019-04-08 10:51:20 +02002983 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002984 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2985 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002986 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002987 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002988 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002989 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002990 end:
2991 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002992}
2993
2994static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2995{
2996 struct h1s *h1s = cs->ctx;
2997 struct h1c *h1c;
2998
2999 if (!h1s)
3000 return;
3001 h1c = h1s->h1c;
3002
Christopher Faulet6b81df72019-10-01 22:08:43 +02003003 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3004
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003005 if (cs->flags & CS_FL_SHW)
3006 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003007 if (cs->flags & CS_FL_KILL_CONN) {
3008 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003009 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003010 }
3011 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3012 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3013 goto do_shutw;
3014 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003015
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003016 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3017 /* Here attached is implicit because there is CS */
3018 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3019 goto end;
3020 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003021 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3022 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003023 goto end;
3024 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003025
Christopher Faulet7f366362019-04-08 10:51:20 +02003026 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003027 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003028 if (!b_data(&h1c->obuf))
3029 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003030 end:
3031 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003032}
3033
Christopher Faulet666a0c42019-01-08 11:12:04 +01003034static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003035{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003036 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003037
Christopher Faulet6b81df72019-10-01 22:08:43 +02003038 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003039 conn_xprt_shutw(conn);
3040 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003041 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003042}
3043
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003044/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3045 * The <es> pointer is not allowed to differ from the one passed to the
3046 * subscribe() call. It always returns zero.
3047 */
3048static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003049{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003050 struct h1s *h1s = cs->ctx;
3051
3052 if (!h1s)
3053 return 0;
3054
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003055 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003056 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003057
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003058 es->events &= ~event_type;
3059 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003060 h1s->subs = NULL;
3061
3062 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003063 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003064
3065 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003066 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003067
Christopher Faulet51dbc942018-09-13 09:05:15 +02003068 return 0;
3069}
3070
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003071/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3072 * event subscriber <es> is not allowed to change from a previous call as long
3073 * as at least one event is still subscribed. The <event_type> must only be a
3074 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3075 * the conn_stream <cs> was already detached, in which case it will return -1.
3076 */
3077static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003078{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003079 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003080 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003081
3082 if (!h1s)
3083 return -1;
3084
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003085 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003086 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003087
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003088 es->events |= event_type;
3089 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003090
3091 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003092 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003093
3094
Christopher Faulet6b81df72019-10-01 22:08:43 +02003095 if (event_type & SUB_RETRY_SEND) {
3096 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003097 /*
3098 * If the conn_stream attempt to subscribe, and the
3099 * mux isn't subscribed to the connection, then it
3100 * probably means the connection wasn't established
3101 * yet, so we have to subscribe.
3102 */
3103 h1c = h1s->h1c;
3104 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3105 h1c->conn->xprt->subscribe(h1c->conn,
3106 h1c->conn->xprt_ctx,
3107 SUB_RETRY_SEND,
3108 &h1c->wait_event);
3109 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003110 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003111}
3112
3113/* Called from the upper layer, to receive data */
3114static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3115{
3116 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003117 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003118 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003119 size_t ret = 0;
3120
Willy Tarreau022e5e52020-09-10 09:33:15 +02003121 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003122
3123 /* Do nothing for now if not READY */
3124 if (!(h1c->flags & H1C_F_ST_READY)) {
3125 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3126 goto end;
3127 }
3128
Christopher Faulet539e0292018-11-19 10:40:09 +01003129 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02003130 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003131 else
3132 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003133
Christopher Faulete18777b2019-04-16 16:46:36 +02003134 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02003135 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02003136 h1c->flags |= H1C_F_WANT_SPLICE;
3137 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 +02003138 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003139 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003140 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003141 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003142 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003143 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003144
3145 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003146 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003147 return ret;
3148}
3149
3150
3151/* Called from the upper layer, to send data */
3152static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3153{
3154 struct h1s *h1s = cs->ctx;
3155 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003156 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003157
3158 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003159 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003160 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003161
Willy Tarreau022e5e52020-09-10 09:33:15 +02003162 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003163
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003164 /* If we're not connected yet, or we're waiting for a handshake, stop
3165 * now, as we don't want to remove everything from the channel buffer
3166 * before we're sure we can send it.
3167 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003168 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003169 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003170 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003171 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003172
Christopher Fauletdea24742021-01-22 15:12:30 +01003173 if (h1c->flags & H1C_F_ST_ERROR) {
3174 cs->flags |= CS_FL_ERROR;
3175 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);
3176 return 0;
3177 }
3178
Christopher Faulet46230362019-10-17 16:04:20 +02003179 /* Inherit some flags from the upper layer */
3180 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3181 if (flags & CO_SFL_MSG_MORE)
3182 h1c->flags |= H1C_F_CO_MSG_MORE;
3183 if (flags & CO_SFL_STREAMER)
3184 h1c->flags |= H1C_F_CO_STREAMER;
3185
Christopher Fauletc31872f2019-06-04 22:09:36 +02003186 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003187 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003188
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003189 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3190 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003191 else
3192 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003193
3194 if ((count - ret) > 0)
3195 h1c->flags |= H1C_F_CO_MSG_MORE;
3196
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003197 if (!ret)
3198 break;
3199 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003200 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003201 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003202 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003203 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003204
3205 if (h1c->flags & H1C_F_ST_ERROR) {
3206 TRACE_DEVEL("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
3207 cs->flags |= CS_FL_ERROR;
3208 }
3209
Christopher Faulet7a145d62020-08-05 11:31:16 +02003210 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003211 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003212 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003213}
3214
Willy Tarreaue5733232019-05-22 19:24:06 +02003215#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003216/* Send and get, using splicing */
3217static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3218{
3219 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003220 struct h1c *h1c = h1s->h1c;
3221 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003222 int ret = 0;
3223
Willy Tarreau022e5e52020-09-10 09:33:15 +02003224 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003225
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003226 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003227 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003228 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 +02003229 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003230 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003231 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003232 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003233 goto end;
3234 }
3235
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003236 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3237 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003238 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3239 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003240 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003241 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003242 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003243 }
3244
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003245 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003246 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3247 goto end;
3248 }
3249
Christopher Faulet1be55f92018-10-02 15:59:23 +02003250 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3251 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003252 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003253 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003254 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003255 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003256 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003257 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003258 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003259 }
3260
Christopher Faulet1be55f92018-10-02 15:59:23 +02003261 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003262 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003263 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003264 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003265 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003266 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003267
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003268 if ((h1s->flags & H1S_F_REOS) ||
3269 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003270 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003271 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003272 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003273 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003274
Willy Tarreau022e5e52020-09-10 09:33:15 +02003275 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003276 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003277}
3278
3279static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3280{
3281 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003282 int ret = 0;
3283
Willy Tarreau022e5e52020-09-10 09:33:15 +02003284 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003285
Christopher Faulet1be55f92018-10-02 15:59:23 +02003286 if (b_data(&h1s->h1c->obuf))
3287 goto end;
3288
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003289 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003290 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003291 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003292 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3293 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003294 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003295 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003296 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003297
Willy Tarreau022e5e52020-09-10 09:33:15 +02003298 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003299 return ret;
3300}
3301#endif
3302
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003303static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3304{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003305 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003306 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003307
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003308 switch (mux_ctl) {
3309 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003310 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003311 ret |= MUX_STATUS_READY;
3312 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003313 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003314 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3315 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003316 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3317 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3318 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003319 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003320 default:
3321 return -1;
3322 }
3323}
3324
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003325/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003326static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003327{
3328 struct h1c *h1c = conn->ctx;
3329 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003330 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003331
Christopher Fauletf376a312019-01-04 15:16:06 +01003332 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3333 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003334 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3335 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3336 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3337 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3338
3339 if (h1s) {
3340 char *method;
3341
3342 if (h1s->meth < HTTP_METH_OTHER)
3343 method = http_known_methods[h1s->meth].ptr;
3344 else
3345 method = "UNKNOWN";
3346 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3347 " .meth=%s status=%d",
3348 h1s, h1s->flags,
3349 h1m_state_str(h1s->req.state),
3350 h1m_state_str(h1s->res.state), method, h1s->status);
3351 if (h1s->cs)
3352 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3353 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003354
3355 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3356 if (h1s->subs) {
3357 if (h1s->subs) {
3358 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3359 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3360 h1s->subs->tasklet->calls,
3361 h1s->subs->tasklet->context);
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003362 if (h1s->subs->tasklet->calls >= 1000000)
3363 ret = 1;
Willy Tarreau150c4f82021-01-20 17:05:58 +01003364 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3365 chunk_appendf(&trash, ")");
3366 }
3367 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003368 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003369 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003370}
3371
3372
3373/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3374static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3375{
3376 struct h1_hdr_entry *entry;
3377
3378 /* Be sure there is a non-empty <to> */
3379 if (!strlen(to)) {
3380 memprintf(err, "expect <to>");
3381 return -1;
3382 }
3383
3384 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003385 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003386 memprintf(err, "<from> and <to> must not differ execpt the case");
3387 return -1;
3388 }
3389
3390 /* Be sure <from> does not already existsin the tree */
3391 if (ebis_lookup(&hdrs_map.map, from)) {
3392 memprintf(err, "duplicate entry '%s'", from);
3393 return -1;
3394 }
3395
3396 /* Create the entry and insert it in the tree */
3397 entry = malloc(sizeof(*entry));
3398 if (!entry) {
3399 memprintf(err, "out of memory");
3400 return -1;
3401 }
3402
3403 entry->node.key = strdup(from);
3404 entry->name.ptr = strdup(to);
3405 entry->name.len = strlen(to);
3406 if (!entry->node.key || !entry->name.ptr) {
3407 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003408 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003409 free(entry);
3410 memprintf(err, "out of memory");
3411 return -1;
3412 }
3413 ebis_insert(&hdrs_map.map, &entry->node);
3414 return 0;
3415}
3416
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003417/* Migrate the the connection to the current thread.
3418 * Return 0 if successful, non-zero otherwise.
3419 * Expected to be called with the old thread lock held.
3420 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003421static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003422{
3423 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003424 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003425
3426 if (fd_takeover(conn->handle.fd, conn) != 0)
3427 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003428
3429 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3430 /* We failed to takeover the xprt, even if the connection may
3431 * still be valid, flag it as error'd, as we have already
3432 * taken over the fd, and wake the tasklet, so that it will
3433 * destroy it.
3434 */
3435 conn->flags |= CO_FL_ERROR;
3436 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3437 return -1;
3438 }
3439
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003440 if (h1c->wait_event.events)
3441 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3442 h1c->wait_event.events, &h1c->wait_event);
3443 /* To let the tasklet know it should free itself, and do nothing else,
3444 * set its context to NULL.
3445 */
3446 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003447 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003448
3449 task = h1c->task;
3450 if (task) {
3451 task->context = NULL;
3452 h1c->task = NULL;
3453 __ha_barrier_store();
3454 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003455
3456 h1c->task = task_new(tid_bit);
3457 if (!h1c->task) {
3458 h1_release(h1c);
3459 return -1;
3460 }
3461 h1c->task->process = h1_timeout_task;
3462 h1c->task->context = h1c;
3463 }
3464 h1c->wait_event.tasklet = tasklet_new();
3465 if (!h1c->wait_event.tasklet) {
3466 h1_release(h1c);
3467 return -1;
3468 }
3469 h1c->wait_event.tasklet->process = h1_io_cb;
3470 h1c->wait_event.tasklet->context = h1c;
3471 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3472 SUB_RETRY_RECV, &h1c->wait_event);
3473
3474 return 0;
3475}
3476
3477
Christopher Faulet98fbe952019-07-22 16:18:24 +02003478static void h1_hdeaders_case_adjust_deinit()
3479{
3480 struct ebpt_node *node, *next;
3481 struct h1_hdr_entry *entry;
3482
3483 node = ebpt_first(&hdrs_map.map);
3484 while (node) {
3485 next = ebpt_next(node);
3486 ebpt_delete(node);
3487 entry = container_of(node, struct h1_hdr_entry, node);
3488 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003489 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003490 free(entry);
3491 node = next;
3492 }
3493 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003494}
3495
Christopher Faulet98fbe952019-07-22 16:18:24 +02003496static int cfg_h1_headers_case_adjust_postparser()
3497{
3498 FILE *file = NULL;
3499 char *c, *key_beg, *key_end, *value_beg, *value_end;
3500 char *err;
3501 int rc, line = 0, err_code = 0;
3502
3503 if (!hdrs_map.name)
3504 goto end;
3505
3506 file = fopen(hdrs_map.name, "r");
3507 if (!file) {
3508 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3509 hdrs_map.name);
3510 err_code |= ERR_ALERT | ERR_FATAL;
3511 goto end;
3512 }
3513
3514 /* now parse all lines. The file may contain only two header name per
3515 * line, separated by spaces. All heading and trailing spaces will be
3516 * ignored. Lines starting with a # are ignored.
3517 */
3518 while (fgets(trash.area, trash.size, file) != NULL) {
3519 line++;
3520 c = trash.area;
3521
3522 /* strip leading spaces and tabs */
3523 while (*c == ' ' || *c == '\t')
3524 c++;
3525
3526 /* ignore emptu lines, or lines beginning with a dash */
3527 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3528 continue;
3529
3530 /* look for the end of the key */
3531 key_beg = c;
3532 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3533 c++;
3534 key_end = c;
3535
3536 /* strip middle spaces and tabs */
3537 while (*c == ' ' || *c == '\t')
3538 c++;
3539
3540 /* look for the end of the value, it is the end of the line */
3541 value_beg = c;
3542 while (*c && *c != '\n' && *c != '\r')
3543 c++;
3544 value_end = c;
3545
3546 /* trim possibly trailing spaces and tabs */
3547 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3548 value_end--;
3549
3550 /* set final \0 and check entries */
3551 *key_end = '\0';
3552 *value_end = '\0';
3553
3554 err = NULL;
3555 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3556 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003557 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3558 hdrs_map.name, err, line);
3559 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003560 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003561 goto end;
3562 }
3563 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003564 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3565 hdrs_map.name, err, line);
3566 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003567 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003568 }
3569 }
3570
3571 end:
3572 if (file)
3573 fclose(file);
3574 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3575 return err_code;
3576}
3577
3578
3579/* config parser for global "h1-outgoing-header-case-adjust" */
3580static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3581 struct proxy *defpx, const char *file, int line,
3582 char **err)
3583{
3584 if (too_many_args(2, args, err, NULL))
3585 return -1;
3586 if (!*(args[1]) || !*(args[2])) {
3587 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3588 return -1;
3589 }
3590 return add_hdr_case_adjust(args[1], args[2], err);
3591}
3592
3593/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3594static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3595 struct proxy *defpx, const char *file, int line,
3596 char **err)
3597{
3598 if (too_many_args(1, args, err, NULL))
3599 return -1;
3600 if (!*(args[1])) {
3601 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3602 return -1;
3603 }
3604 free(hdrs_map.name);
3605 hdrs_map.name = strdup(args[1]);
3606 return 0;
3607}
3608
3609
3610/* config keyword parsers */
3611static struct cfg_kw_list cfg_kws = {{ }, {
3612 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3613 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3614 { 0, NULL, NULL },
3615 }
3616};
3617
3618INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3619REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3620
3621
Christopher Faulet51dbc942018-09-13 09:05:15 +02003622/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003623/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003624/****************************************/
3625
3626/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003627static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003628 .init = h1_init,
3629 .wake = h1_wake,
3630 .attach = h1_attach,
3631 .get_first_cs = h1_get_first_cs,
3632 .detach = h1_detach,
3633 .destroy = h1_destroy,
3634 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003635 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003636 .rcv_buf = h1_rcv_buf,
3637 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003638#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003639 .rcv_pipe = h1_rcv_pipe,
3640 .snd_pipe = h1_snd_pipe,
3641#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003642 .subscribe = h1_subscribe,
3643 .unsubscribe = h1_unsubscribe,
3644 .shutr = h1_shutr,
3645 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003646 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003647 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003648 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003649 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003650 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003651 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003652};
3653
3654
3655/* this mux registers default HTX proto */
3656static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003657{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003658
Willy Tarreau0108d902018-11-25 19:14:37 +01003659INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3660
Christopher Faulet51dbc942018-09-13 09:05:15 +02003661/*
3662 * Local variables:
3663 * c-indent-level: 8
3664 * c-basic-offset: 8
3665 * End:
3666 */