blob: 7c5451c7a4ac61ae60b1d866295ac98d57b56736 [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 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Christopher Faulet0ef372a2019-04-08 10:57:20 +020015#include <common/h2.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Christopher Faulet98fbe952019-07-22 16:18:24 +020019#include <ebistree.h>
20
Christopher Faulet1be55f92018-10-02 15:59:23 +020021#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020022#include <types/proxy.h>
23#include <types/session.h>
24
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/connection.h>
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020026#include <proto/h1_htx.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020027#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020028#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010029#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030#include <proto/stream.h>
31#include <proto/stream_interface.h>
Christopher Faulet6b81df72019-10-01 22:08:43 +020032#include <proto/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020033
34/*
35 * H1 Connection flags (32 bits)
36 */
37#define H1C_F_NONE 0x00000000
38
39/* Flags indicating why writing output data are blocked */
40#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
41#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
42/* 0x00000004 - 0x00000008 unused */
43
44/* Flags indicating why reading input data are blocked. */
45#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
46#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010047#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010048/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
50#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
51#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010052#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet51dbc942018-09-13 09:05:15 +020053
Christopher Fauletf2824e62018-10-01 12:12:37 +020054#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020055#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020056
Christopher Faulet51dbc942018-09-13 09:05:15 +020057/*
58 * H1 Stream flags (32 bits)
59 */
Christopher Faulet129817b2018-09-20 16:14:40 +020060#define H1S_F_NONE 0x00000000
61#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020062#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
63#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020064#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020065#define H1S_F_WANT_KAL 0x00000010
66#define H1S_F_WANT_TUN 0x00000020
67#define H1S_F_WANT_CLO 0x00000040
68#define H1S_F_WANT_MSK 0x00000070
69#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010070#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010071#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010072#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +020073#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020074/* 0x00002000 .. 0x00001000 unused */
75#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 +020076#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020077
78/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020079struct h1c {
80 struct connection *conn;
81 struct proxy *px;
82 uint32_t flags; /* Connection flags: H1C_F_* */
83
84 struct buffer ibuf; /* Input buffer to store data before parsing */
85 struct buffer obuf; /* Output buffer to store data after reformatting */
86
87 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
88 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
89
90 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010091 struct task *task; /* timeout management task */
92 int timeout; /* idle timeout duration in ticks */
93 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020094};
95
96/* H1 stream descriptor */
97struct h1s {
98 struct h1c *h1c;
99 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100100 struct cs_info csinfo; /* CS info, only used for client connections */
101 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200102
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
104 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200105
Olivier Houchardf502aca2018-12-14 19:42:40 +0100106 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200107 struct h1m req;
108 struct h1m res;
109
110 enum http_meth_t meth; /* HTTP resquest method */
111 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200112};
113
Christopher Faulet98fbe952019-07-22 16:18:24 +0200114/* Map of headers used to convert outgoing headers */
115struct h1_hdrs_map {
116 char *name;
117 struct eb_root map;
118};
119
120/* An entry in a headers map */
121struct h1_hdr_entry {
122 struct ist name;
123 struct ebpt_node node;
124};
125
126/* Declare the headers map */
127static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
128
129
Christopher Faulet6b81df72019-10-01 22:08:43 +0200130/* trace source and events */
131static void h1_trace(enum trace_level level, uint64_t mask,
132 const struct trace_source *src,
133 const struct ist where, const struct ist func,
134 const void *a1, const void *a2, const void *a3, const void *a4);
135
136/* The event representation is split like this :
137 * h1c - internal H1 connection
138 * h1s - internal H1 stream
139 * strm - application layer
140 * rx - data receipt
141 * tx - data transmission
142 *
143 */
144static const struct trace_event h1_trace_events[] = {
145#define H1_EV_H1C_NEW (1ULL << 0)
146 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
147#define H1_EV_H1C_RECV (1ULL << 1)
148 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
149#define H1_EV_H1C_SEND (1ULL << 2)
150 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
151#define H1_EV_H1C_BLK (1ULL << 3)
152 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
153#define H1_EV_H1C_WAKE (1ULL << 4)
154 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
155#define H1_EV_H1C_END (1ULL << 5)
156 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
157#define H1_EV_H1C_ERR (1ULL << 6)
158 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
159
160#define H1_EV_RX_DATA (1ULL << 7)
161 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
162#define H1_EV_RX_EOI (1ULL << 8)
163 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
164#define H1_EV_RX_HDRS (1ULL << 9)
165 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
166#define H1_EV_RX_BODY (1ULL << 10)
167 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
168#define H1_EV_RX_TLRS (1ULL << 11)
169 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
170
171#define H1_EV_TX_DATA (1ULL << 12)
172 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
173#define H1_EV_TX_EOI (1ULL << 13)
174 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
175#define H1_EV_TX_HDRS (1ULL << 14)
176 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
177#define H1_EV_TX_BODY (1ULL << 15)
178 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
179#define H1_EV_TX_TLRS (1ULL << 16)
180 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
181
182#define H1_EV_H1S_NEW (1ULL << 17)
183 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
184#define H1_EV_H1S_BLK (1ULL << 18)
185 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
186#define H1_EV_H1S_END (1ULL << 19)
187 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
188#define H1_EV_H1S_ERR (1ULL << 20)
189 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
190
191#define H1_EV_STRM_NEW (1ULL << 21)
192 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
193#define H1_EV_STRM_RECV (1ULL << 22)
194 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
195#define H1_EV_STRM_SEND (1ULL << 23)
196 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
197#define H1_EV_STRM_WAKE (1ULL << 24)
198 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
199#define H1_EV_STRM_SHUT (1ULL << 25)
200 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
201#define H1_EV_STRM_END (1ULL << 26)
202 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
203#define H1_EV_STRM_ERR (1ULL << 27)
204 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
205
206 { }
207};
208
209static const struct name_desc h1_trace_lockon_args[4] = {
210 /* arg1 */ { /* already used by the connection */ },
211 /* arg2 */ { .name="h1s", .desc="H1 stream" },
212 /* arg3 */ { },
213 /* arg4 */ { }
214};
215
216static const struct name_desc h1_trace_decoding[] = {
217#define H1_VERB_CLEAN 1
218 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
219#define H1_VERB_MINIMAL 2
220 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
221#define H1_VERB_SIMPLE 3
222 { .name="simple", .desc="add request/response status line or htx info when available" },
223#define H1_VERB_ADVANCED 4
224 { .name="advanced", .desc="add header fields or frame decoding when available" },
225#define H1_VERB_COMPLETE 5
226 { .name="complete", .desc="add full data dump when available" },
227 { /* end */ }
228};
229
230static struct trace_source trace_h1 = {
231 .name = IST("h1"),
232 .desc = "HTTP/1 multiplexer",
233 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
234 .default_cb = h1_trace,
235 .known_events = h1_trace_events,
236 .lockon_args = h1_trace_lockon_args,
237 .decoding = h1_trace_decoding,
238 .report_events = ~0, // report everything by default
239};
240
241#define TRACE_SOURCE &trace_h1
242INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
243
Christopher Faulet51dbc942018-09-13 09:05:15 +0200244/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100245DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
246DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200247
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248static int h1_recv(struct h1c *h1c);
249static int h1_send(struct h1c *h1c);
250static int h1_process(struct h1c *h1c);
251static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100252static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100253static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200254static void h1_wake_stream_for_recv(struct h1s *h1s);
255static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256
Christopher Faulet6b81df72019-10-01 22:08:43 +0200257/* the H1 traces always expect that arg1, if non-null, is of type connection
258 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
259 * that arg3, if non-null, is a htx for rx/tx headers.
260 */
261static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
262 const struct ist where, const struct ist func,
263 const void *a1, const void *a2, const void *a3, const void *a4)
264{
265 const struct connection *conn = a1;
266 const struct h1c *h1c = conn ? conn->ctx : NULL;
267 const struct h1s *h1s = a2;
268 const struct htx *htx = a3;
269 const size_t *val = a4;
270
271 if (!h1c)
272 h1c = (h1s ? h1s->h1c : NULL);
273
274 if (!h1c || src->verbosity < H1_VERB_CLEAN)
275 return;
276
277 /* Display frontend/backend info by default */
278 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
279
280 /* Display request and response states if h1s is defined */
281 if (h1s)
282 chunk_appendf(&trace_buf, " [%s, %s]",
283 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
284
285 if (src->verbosity == H1_VERB_CLEAN)
286 return;
287
288 /* Display the value to the 4th argument (level > STATE) */
289 if (src->level > TRACE_LEVEL_STATE && val)
290 chunk_appendf(&trace_buf, " - VAL=%lu", *val);
291
292 /* Display status-line if possible (verbosity > MINIMAL) */
293 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
294 const struct htx_blk *blk = htx_get_head_blk(htx);
295 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
296 enum htx_blk_type type = htx_get_blk_type(blk);
297
298 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
299 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
300 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
301 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
302 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
303 }
304
305 /* Display h1c info and, if defined, h1s info (pointer + flags) */
306 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
307 if (h1s)
308 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
309
310 if (src->verbosity == H1_VERB_MINIMAL)
311 return;
312
313 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
314 if (src->level > TRACE_LEVEL_USER) {
315 if (src->verbosity == H1_VERB_COMPLETE ||
316 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
317 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
318 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
319 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
320 if (src->verbosity == H1_VERB_COMPLETE ||
321 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
322 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
323 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
324 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
325 }
326
327 /* Display htx info if defined (level > USER) */
328 if (src->level > TRACE_LEVEL_USER && htx) {
329 int full = 0;
330
331 /* Full htx info (level > STATE && verbosity > SIMPLE) */
332 if (src->level > TRACE_LEVEL_STATE) {
333 if (src->verbosity == H1_VERB_COMPLETE)
334 full = 1;
335 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
336 full = 1;
337 }
338
339 chunk_memcat(&trace_buf, "\n\t", 2);
340 htx_dump(&trace_buf, htx, full);
341 }
342}
343
344
Christopher Faulet51dbc942018-09-13 09:05:15 +0200345/*****************************************************/
346/* functions below are for dynamic buffer management */
347/*****************************************************/
348/*
349 * Indicates whether or not the we may call the h1_recv() function to
350 * attempt to receive data into the buffer and/or parse pending data. The
351 * condition is a bit complex due to some API limits for now. The rules are the
352 * following :
353 * - if an error or a shutdown was detected on the connection and the buffer
354 * is empty, we must not attempt to receive
355 * - if the input buffer failed to be allocated, we must not try to receive
356 * and we know there is nothing pending
357 * - if no flag indicates a blocking condition, we may attempt to receive,
358 * regardless of whether the input buffer is full or not, so that only de
359 * receiving part decides whether or not to block. This is needed because
360 * the connection API indeed prevents us from re-enabling receipt that is
361 * already enabled in a polled state, so we must always immediately stop as
362 * soon as the mux can't proceed so as never to hit an end of read with data
363 * pending in the buffers.
364 * - otherwise must may not attempt to receive
365 */
366static inline int h1_recv_allowed(const struct h1c *h1c)
367{
Christopher Faulet6b81df72019-10-01 22:08:43 +0200368 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN))) {
369 TRACE_DEVEL("recv not allowed because of (error|shudown) on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200370 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200371 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372
Christopher Faulet6b81df72019-10-01 22:08:43 +0200373 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn)) {
374 TRACE_DEVEL("recv not allowed because of (error|read0) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletcf56b992018-12-11 16:12:31 +0100375 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200376 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100377
Christopher Fauletcb55f482018-12-10 11:56:47 +0100378 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200379 return 1;
380
Christopher Faulet6b81df72019-10-01 22:08:43 +0200381 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 +0200382 return 0;
383}
384
385/*
386 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
387 * flags are used to figure what buffer was requested. It returns 1 if the
388 * allocation succeeds, in which case the connection is woken up, or 0 if it's
389 * impossible to wake up and we prefer to be woken up later.
390 */
391static int h1_buf_available(void *target)
392{
393 struct h1c *h1c = target;
394
395 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200396 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 +0200397 h1c->flags &= ~H1C_F_IN_ALLOC;
398 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200399 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200400 return 1;
401 }
402
403 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200404 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 +0200405 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200406 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200407 if (h1c->h1s)
408 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409 return 1;
410 }
411
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412 return 0;
413}
414
415/*
416 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
417 */
418static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
419{
420 struct buffer *buf = NULL;
421
422 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
423 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
424 h1c->buf_wait.target = h1c;
425 h1c->buf_wait.wakeup_cb = h1_buf_available;
426 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
427 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
428 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
429 __conn_xprt_stop_recv(h1c->conn);
430 }
431 return buf;
432}
433
434/*
435 * Release a buffer, if any, and try to wake up entities waiting in the buffer
436 * wait queue.
437 */
438static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
439{
440 if (bptr->size) {
441 b_free(bptr);
442 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
443 }
444}
445
Willy Tarreau00f18a32019-01-26 12:19:01 +0100446/* returns the number of streams in use on a connection to figure if it's
447 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
448 * as the caller will want to know if it was the last one after a detach().
449 */
450static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200451{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100452 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200453
Willy Tarreau00f18a32019-01-26 12:19:01 +0100454 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200455}
456
Willy Tarreau00f18a32019-01-26 12:19:01 +0100457/* returns the number of streams still available on a connection */
458static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100459{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100460 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100461}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200462
Willy Tarreau00f18a32019-01-26 12:19:01 +0100463
Christopher Faulet51dbc942018-09-13 09:05:15 +0200464/*****************************************************************/
465/* functions below are dedicated to the mux setup and management */
466/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200467
468/* returns non-zero if there are input data pending for stream h1s. */
469static inline size_t h1s_data_pending(const struct h1s *h1s)
470{
471 const struct h1m *h1m;
472
473 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
474 if (h1m->state == H1_MSG_DONE)
475 return 0; // data not for this stream (e.g. pipelining)
476
477 return b_data(&h1s->h1c->ibuf);
478}
479
Christopher Faulet47365272018-10-31 17:40:50 +0100480static struct conn_stream *h1s_new_cs(struct h1s *h1s)
481{
482 struct conn_stream *cs;
483
Christopher Faulet6b81df72019-10-01 22:08:43 +0200484 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100485 cs = cs_new(h1s->h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200486 if (!cs) {
487 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 +0100488 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200489 }
Christopher Faulet47365272018-10-31 17:40:50 +0100490 h1s->cs = cs;
491 cs->ctx = h1s;
492
493 if (h1s->flags & H1S_F_NOT_FIRST)
494 cs->flags |= CS_FL_NOT_FIRST;
495
Christopher Faulet6b81df72019-10-01 22:08:43 +0200496 if (stream_create_from_cs(cs) < 0) {
497 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 +0100498 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200499 }
500
501 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100502 return cs;
503
504 err:
505 cs_free(cs);
506 h1s->cs = NULL;
507 return NULL;
508}
509
Olivier Houchardf502aca2018-12-14 19:42:40 +0100510static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200511{
512 struct h1s *h1s;
513
Christopher Faulet6b81df72019-10-01 22:08:43 +0200514 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
515
Christopher Faulet51dbc942018-09-13 09:05:15 +0200516 h1s = pool_alloc(pool_head_h1s);
517 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100518 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200519
520 h1s->h1c = h1c;
521 h1c->h1s = h1s;
522
Olivier Houchardf502aca2018-12-14 19:42:40 +0100523 h1s->sess = sess;
524
Christopher Faulet51dbc942018-09-13 09:05:15 +0200525 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100526 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200527
528 h1s->recv_wait = NULL;
529 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200530
531 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100532 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200533
Christopher Faulet129817b2018-09-20 16:14:40 +0200534 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100535 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200536
537 h1s->status = 0;
538 h1s->meth = HTTP_METH_OTHER;
539
Christopher Faulet47365272018-10-31 17:40:50 +0100540 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
541 h1s->flags |= H1S_F_NOT_FIRST;
542 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
543
Christopher Faulet129817b2018-09-20 16:14:40 +0200544 if (!conn_is_back(h1c->conn)) {
545 if (h1c->px->options2 & PR_O2_REQBUG_OK)
546 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200547
548 /* For frontend connections we should always have a session */
549 if (!sess)
550 sess = h1c->conn->owner;
551
Dave Pirotte234740f2019-07-10 13:57:38 +0000552 /* Timers for subsequent sessions on the same HTTP 1.x connection
553 * measure from `now`, not from the connection accept time */
554 if (h1s->flags & H1S_F_NOT_FIRST) {
555 h1s->csinfo.create_date = date;
556 h1s->csinfo.tv_create = now;
557 h1s->csinfo.t_handshake = 0;
558 h1s->csinfo.t_idle = -1;
559 }
560 else {
561 h1s->csinfo.create_date = sess->accept_date;
562 h1s->csinfo.tv_create = sess->tv_accept;
563 h1s->csinfo.t_handshake = sess->t_handshake;
564 h1s->csinfo.t_idle = -1;
565 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200566 }
567 else {
568 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
569 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200570
Christopher Fauletfeb11742018-11-29 15:12:34 +0100571 h1s->csinfo.create_date = date;
572 h1s->csinfo.tv_create = now;
573 h1s->csinfo.t_handshake = 0;
574 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200575 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100576
Christopher Faulete9b70722019-04-08 10:46:02 +0200577 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
578 * create a new one.
579 */
580 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200581 cs->ctx = h1s;
582 h1s->cs = cs;
583 }
Christopher Faulet47365272018-10-31 17:40:50 +0100584 else {
585 cs = h1s_new_cs(h1s);
586 if (!cs)
587 goto fail;
588 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200589 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200590 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100591
592 fail:
593 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200594 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 +0100595 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200596}
597
598static void h1s_destroy(struct h1s *h1s)
599{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200600 if (h1s) {
601 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200602
Christopher Faulet6b81df72019-10-01 22:08:43 +0200603 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200604 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200605
Christopher Fauletf2824e62018-10-01 12:12:37 +0200606 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100607 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200608 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100609 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200610
Christopher Fauletcb55f482018-12-10 11:56:47 +0100611 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100612 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200613 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100614 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200615 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
616 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200617 pool_free(pool_head_h1s, h1s);
618 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200619}
620
Christopher Fauletfeb11742018-11-29 15:12:34 +0100621static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
622{
623 struct h1s *h1s = cs->ctx;
624
625 if (h1s && !conn_is_back(cs->conn))
626 return &h1s->csinfo;
627 return NULL;
628}
629
Christopher Faulet51dbc942018-09-13 09:05:15 +0200630/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200631 * Initialize the mux once it's attached. It is expected that conn->ctx points
632 * to the existing conn_stream (for outgoing connections or for incoming onces
633 * during a mux upgrade) or NULL (for incoming ones during the connexion
634 * establishment). <input> is always used as Input buffer and may contain
635 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
636 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200637 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200638static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
639 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200640{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200641 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100642 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200643 void *conn_ctx = conn->ctx;
644
645 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200646
647 h1c = pool_alloc(pool_head_h1c);
648 if (!h1c)
649 goto fail_h1c;
650 h1c->conn = conn;
651 h1c->px = proxy;
652
653 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200654 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200655 h1c->obuf = BUF_NULL;
656 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200657 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200658
Christopher Faulet51dbc942018-09-13 09:05:15 +0200659 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200660 h1c->wait_event.tasklet = tasklet_new();
661 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200662 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200663 h1c->wait_event.tasklet->process = h1_io_cb;
664 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100665 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200666
Christopher Faulete9b70722019-04-08 10:46:02 +0200667 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100668 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
669 if (tick_isset(proxy->timeout.serverfin))
670 h1c->shut_timeout = proxy->timeout.serverfin;
671 } else {
672 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
673 if (tick_isset(proxy->timeout.clientfin))
674 h1c->shut_timeout = proxy->timeout.clientfin;
675 }
676 if (tick_isset(h1c->timeout)) {
677 t = task_new(tid_bit);
678 if (!t)
679 goto fail;
680
681 h1c->task = t;
682 t->process = h1_timeout_task;
683 t->context = h1c;
684 t->expire = tick_add(now_ms, h1c->timeout);
685 }
686
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100687 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200688
Christopher Faulet6b81df72019-10-01 22:08:43 +0200689 /* Always Create a new H1S */
690 if (!h1s_create(h1c, conn_ctx, sess))
691 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100692
693 if (t)
694 task_queue(t);
695
Christopher Faulet51dbc942018-09-13 09:05:15 +0200696 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200697 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200698
699 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200700 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200701 return 0;
702
703 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200704 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200705 if (h1c->wait_event.tasklet)
706 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200707 pool_free(pool_head_h1c, h1c);
708 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200709 conn->ctx = conn_ctx; // restore saved context
710 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200711 return -1;
712}
713
Christopher Faulet73c12072019-04-08 11:23:22 +0200714/* release function. This one should be called to free all resources allocated
715 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200716 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200717static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200718{
Christopher Faulet61840e72019-04-15 09:33:32 +0200719 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200720
Christopher Faulet6b81df72019-10-01 22:08:43 +0200721 TRACE_POINT(H1_EV_H1C_END);
722
Christopher Faulet51dbc942018-09-13 09:05:15 +0200723 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200724 /* The connection must be aattached to this mux to be released */
725 if (h1c->conn && h1c->conn->ctx == h1c)
726 conn = h1c->conn;
727
Christopher Faulet6b81df72019-10-01 22:08:43 +0200728 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
729
Christopher Faulet61840e72019-04-15 09:33:32 +0200730 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200731 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200732 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200733 /* Make sure we're no longer subscribed to anything */
734 if (h1c->wait_event.events)
735 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
736 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200737 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200738 /* connection successfully upgraded to H2, this
739 * mux was already released */
740 return;
741 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200742 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200743 sess_log(conn->owner); /* Log if the upgrade failed */
744 }
745
Olivier Houchard45c44372019-06-11 14:06:23 +0200746
Christopher Faulet51dbc942018-09-13 09:05:15 +0200747 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
748 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
749 LIST_DEL(&h1c->buf_wait.list);
750 LIST_INIT(&h1c->buf_wait.list);
751 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
752 }
753
754 h1_release_buf(h1c, &h1c->ibuf);
755 h1_release_buf(h1c, &h1c->obuf);
756
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100757 if (h1c->task) {
758 h1c->task->context = NULL;
759 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
760 h1c->task = NULL;
761 }
762
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200763 if (h1c->wait_event.tasklet)
764 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200765
Christopher Fauletf2824e62018-10-01 12:12:37 +0200766 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200767 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100768 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200769 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200770 pool_free(pool_head_h1c, h1c);
771 }
772
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200773 if (conn) {
774 conn->mux = NULL;
775 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200776 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200777
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200778 conn_stop_tracking(conn);
779 conn_full_close(conn);
780 if (conn->destroy_cb)
781 conn->destroy_cb(conn);
782 conn_free(conn);
783 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200784}
785
786/******************************************************/
787/* functions below are for the H1 protocol processing */
788/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200789/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
790 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200791 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100792static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200793{
Christopher Faulet570d1612018-11-26 11:13:57 +0100794 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200795
Christopher Faulet570d1612018-11-26 11:13:57 +0100796 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200797 (*(p + 5) > '1' ||
798 (*(p + 5) == '1' && *(p + 7) >= '1')))
799 h1m->flags |= H1_MF_VER_11;
800}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200801
Christopher Faulet9768c262018-10-22 09:34:31 +0200802/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
803 * greater or equal to 1.1
804 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100805static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200806{
Christopher Faulet570d1612018-11-26 11:13:57 +0100807 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200808
Christopher Faulet570d1612018-11-26 11:13:57 +0100809 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200810 (*(p + 5) > '1' ||
811 (*(p + 5) == '1' && *(p + 7) >= '1')))
812 h1m->flags |= H1_MF_VER_11;
813}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200814
Christopher Fauletf2824e62018-10-01 12:12:37 +0200815/* Deduce the connection mode of the client connection, depending on the
816 * configuration and the H1 message flags. This function is called twice, the
817 * first time when the request is parsed and the second time when the response
818 * is parsed.
819 */
820static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
821{
822 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200823
824 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100825 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200826 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100827 h1s->status == 101) {
828 /* Either we've established an explicit tunnel, or we're
829 * switching the protocol. In both cases, we're very unlikely to
830 * understand the next protocols. We have to switch to tunnel
831 * mode, so that we transfer the request and responses then let
832 * this protocol pass unmodified. When we later implement
833 * specific parsers for such protocols, we'll want to check the
834 * Upgrade header which contains information about that protocol
835 * for responses with status 101 (eg: see RFC2817 about TLS).
836 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200837 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200838 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 +0100839 }
840 else if (h1s->flags & H1S_F_WANT_KAL) {
841 /* By default the client is in KAL mode. CLOSE mode mean
842 * it is imposed by the client itself. So only change
843 * KAL mode here. */
844 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
845 /* no length known or explicit close => close */
846 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200847 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 +0100848 }
849 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
850 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
851 /* no explict keep-alive and option httpclose => close */
852 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200853 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 +0100854 }
855 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200856 }
857 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100858 /* Input direction: first pass */
859 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
860 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200861 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200862 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 +0100863 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200864 }
865
866 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200867 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200868 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200869 TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
870 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200871}
872
873/* Deduce the connection mode of the client connection, depending on the
874 * configuration and the H1 message flags. This function is called twice, the
875 * first time when the request is parsed and the second time when the response
876 * is parsed.
877 */
878static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
879{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100880 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100881 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100882 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200883
Christopher Fauletf2824e62018-10-01 12:12:37 +0200884 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100885 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200886 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100887 h1s->status == 101) {
888 /* Either we've established an explicit tunnel, or we're
889 * switching the protocol. In both cases, we're very unlikely to
890 * understand the next protocols. We have to switch to tunnel
891 * mode, so that we transfer the request and responses then let
892 * this protocol pass unmodified. When we later implement
893 * specific parsers for such protocols, we'll want to check the
894 * Upgrade header which contains information about that protocol
895 * for responses with status 101 (eg: see RFC2817 about TLS).
896 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200897 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200898 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 +0100899 }
900 else if (h1s->flags & H1S_F_WANT_KAL) {
901 /* By default the server is in KAL mode. CLOSE mode mean
902 * it is imposed by haproxy itself. So only change KAL
903 * mode here. */
904 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
905 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
906 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
907 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200908 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 +0100909 }
910 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200911 }
Christopher Faulet70033782018-12-05 13:50:11 +0100912 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100913 /* Output direction: first pass */
914 if (h1m->flags & H1_MF_CONN_CLO) {
915 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100916 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200917 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 +0100918 }
919 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
920 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
921 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
922 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
923 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
924 /* no explicit keep-alive option httpclose/server-close => close */
925 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200926 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 +0100927 }
Christopher Faulet70033782018-12-05 13:50:11 +0100928 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200929
930 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200931 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200932 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200933 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);
934 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200935}
936
Christopher Fauletb992af02019-03-28 15:42:24 +0100937static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200938{
939 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200940
941 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
942 * token is found
943 */
944 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200945 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200946
947 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200948 if (!(h1m->flags & H1_MF_VER_11)) {
949 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 +0100950 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200951 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200952 }
953 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200954 if (h1m->flags & H1_MF_VER_11) {
955 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100956 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200957 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200958 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200959}
960
Christopher Fauletb992af02019-03-28 15:42:24 +0100961static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200962{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200963 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
964 * token is found
965 */
966 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200967 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200968
969 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100970 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +0200971 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
972 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 +0100973 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200974 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200975 }
976 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200977 if (h1m->flags & H1_MF_VER_11) {
978 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100979 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200980 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200981 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200982}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200983
Christopher Fauletb992af02019-03-28 15:42:24 +0100984static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200985{
Christopher Fauletb992af02019-03-28 15:42:24 +0100986 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200987 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100988 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200989 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200990}
991
Christopher Fauletb992af02019-03-28 15:42:24 +0100992static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
993{
994 if (!conn_is_back(h1s->h1c->conn))
995 h1_set_cli_conn_mode(h1s, h1m);
996 else
997 h1_set_srv_conn_mode(h1s, h1m);
998
999 if (!(h1m->flags & H1_MF_RESP))
1000 h1_update_req_conn_value(h1s, h1m, conn_val);
1001 else
1002 h1_update_res_conn_value(h1s, h1m, conn_val);
1003}
Christopher Faulete44769b2018-11-29 23:01:45 +01001004
Christopher Faulet98fbe952019-07-22 16:18:24 +02001005/* Try to adjust the case of the message header name using the global map
1006 * <hdrs_map>.
1007 */
1008static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1009{
1010 struct ebpt_node *node;
1011 struct h1_hdr_entry *entry;
1012
1013 /* No entry in the map, do nothing */
1014 if (eb_is_empty(&hdrs_map.map))
1015 return;
1016
1017 /* No conversion fo the request headers */
1018 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1019 return;
1020
1021 /* No conversion fo the response headers */
1022 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1023 return;
1024
1025 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1026 if (!node)
1027 return;
1028 entry = container_of(node, struct h1_hdr_entry, node);
1029 name->ptr = entry->name.ptr;
1030 name->len = entry->name.len;
1031}
1032
Christopher Faulete44769b2018-11-29 23:01:45 +01001033/* Append the description of what is present in error snapshot <es> into <out>.
1034 * The description must be small enough to always fit in a buffer. The output
1035 * buffer may be the trash so the trash must not be used inside this function.
1036 */
1037static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1038{
1039 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001040 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1041 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1042 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1043 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1044 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1045 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001046}
1047/*
1048 * Capture a bad request or response and archive it in the proxy's structure.
1049 * By default it tries to report the error position as h1m->err_pos. However if
1050 * this one is not set, it will then report h1m->next, which is the last known
1051 * parsing point. The function is able to deal with wrapping buffers. It always
1052 * displays buffers as a contiguous area starting at buf->p. The direction is
1053 * determined thanks to the h1m's flags.
1054 */
1055static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1056 struct h1m *h1m, struct buffer *buf)
1057{
1058 struct session *sess = h1c->conn->owner;
1059 struct proxy *proxy = h1c->px;
1060 struct proxy *other_end = sess->fe;
1061 union error_snapshot_ctx ctx;
1062
Willy Tarreau598d7fc2018-12-18 18:10:38 +01001063 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +01001064 other_end = si_strm(h1s->cs->data)->be;
1065
1066 /* http-specific part now */
1067 ctx.h1.state = h1m->state;
1068 ctx.h1.c_flags = h1c->flags;
1069 ctx.h1.s_flags = h1s->flags;
1070 ctx.h1.m_flags = h1m->flags;
1071 ctx.h1.m_clen = h1m->curr_len;
1072 ctx.h1.m_blen = h1m->body_len;
1073
1074 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1075 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001076 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1077 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001078}
1079
Christopher Fauletadb22202018-12-12 10:32:09 +01001080/* Emit the chunksize followed by a CRLF in front of data of the buffer
1081 * <buf>. It goes backwards and starts with the byte before the buffer's
1082 * head. The caller is responsible for ensuring there is enough room left before
1083 * the buffer's head for the string.
1084 */
1085static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1086{
1087 char *beg, *end;
1088
1089 beg = end = b_head(buf);
1090 *--beg = '\n';
1091 *--beg = '\r';
1092 do {
1093 *--beg = hextab[chksz & 0xF];
1094 } while (chksz >>= 4);
1095 buf->head -= (end - beg);
1096 b_add(buf, end - beg);
1097}
1098
1099/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1100 * ensuring there is enough room left in the buffer for the string. */
1101static void h1_emit_chunk_crlf(struct buffer *buf)
1102{
1103 *(b_peek(buf, b_data(buf))) = '\r';
1104 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1105 b_add(buf, 2);
1106}
1107
Christopher Faulet129817b2018-09-20 16:14:40 +02001108/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001109 * Switch the request to tunnel mode. This function must only be called for
1110 * CONNECT requests. On the client side, the mux is mark as busy on input,
1111 * waiting the response.
1112 */
1113static void h1_set_req_tunnel_mode(struct h1s *h1s)
1114{
1115 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1116 h1s->req.state = H1_MSG_TUNNEL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001117 if (!conn_is_back(h1s->h1c->conn)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001118 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001119 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1120 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001121}
1122
1123/*
1124 * Switch the response to tunnel mode. This function must only be called on
1125 * successfull replies to CONNECT requests or on protocol switching. On the
1126 * server side, if the request is not finished, the mux is mark as busy on
1127 * input. Otherwise the request is also switch to tunnel mode.
1128 */
1129static void h1_set_res_tunnel_mode(struct h1s *h1s)
1130{
1131 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1132 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001133 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001134 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001135 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1136 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001137 else {
1138 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1139 h1s->req.state = H1_MSG_TUNNEL;
1140 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1141 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001142 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001143 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001144 }
1145 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001146}
1147
1148/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001149 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001150 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001151 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001152 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001153static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001154 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001155{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001156 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001157 int ret = 0;
1158
Christopher Faulet6b81df72019-10-01 22:08:43 +02001159 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){max});
1160
Christopher Fauleteec96b52019-09-25 09:10:46 +02001161 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001162 /* Try to match H2 preface before parsing the request headers. */
1163 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001164 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001165 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001166 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001167 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001168 else {
1169 if (h1s->meth == HTTP_METH_CONNECT)
1170 h1m->flags |= H1_MF_METH_CONNECT;
1171 if (h1s->meth == HTTP_METH_HEAD)
1172 h1m->flags |= H1_MF_METH_HEAD;
1173 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001174
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001175 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1176 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001177 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 +02001178 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001179 if (!(h1m->flags & H1_MF_RESP)) {
1180 h1s->flags |= H1S_F_REQ_ERROR;
1181 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1182 }
1183 else {
1184 h1s->flags |= H1S_F_RES_ERROR;
1185 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1186 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001187 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001188 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001189 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1190 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001191 goto end;
1192 }
1193
Christopher Faulet486498c2019-10-11 14:22:00 +02001194 if (h1m->err_pos >= 0) {
1195 /* Maybe we found an error during the parsing while we were
1196 * configured not to block on that, so we have to capture it
1197 * now.
1198 */
1199 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1200 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1201 }
1202
Christopher Faulet129817b2018-09-20 16:14:40 +02001203 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001204 h1s->meth = h1sl.rq.meth;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001205 if (h1m->state == H1_MSG_TUNNEL) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001206 h1_set_req_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001207 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1208 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 }
1210 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001211 h1s->status = h1sl.st.status;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001212 if (h1m->state == H1_MSG_TUNNEL) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001213 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001214 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1215 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001216 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001217 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001218 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001219
Christopher Faulet129817b2018-09-20 16:14:40 +02001220 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001221 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001222 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001223
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001224 h2c_upgrade:
1225 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001226 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001227 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001228 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1229 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001230}
1231
1232/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001233 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001234 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1235 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001236 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001237static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001238 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001239 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001240{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001241 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001242
Christopher Faulet6b81df72019-10-01 22:08:43 +02001243 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001244 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001245 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001246 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 +01001247 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001248 if (!(h1m->flags & H1_MF_RESP)) {
1249 h1s->flags |= H1S_F_REQ_ERROR;
1250 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1251 }
1252 else {
1253 h1s->flags |= H1S_F_RES_ERROR;
1254 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1255 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001256 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001257 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001258 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001259 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001260 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001261 }
1262
Christopher Faulet02a02532019-11-15 09:36:28 +01001263 *ofs += ret;
1264
1265 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001266 if (h1m->state == H1_MSG_DONE) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001267 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001268 TRACE_STATE("end of message", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_RX_EOI, h1s->h1c->conn);
1269 }
1270
Christopher Faulet6b81df72019-10-01 22:08:43 +02001271 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001272 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001273}
1274
1275/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001276 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1277 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1278 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1279 * responsible to update the parser state <h1m>.
1280 */
1281static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1282 struct buffer *buf, size_t *ofs, size_t max)
1283{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001284 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001285
Christopher Faulet6b81df72019-10-01 22:08:43 +02001286 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001287 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001288 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001289 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 +01001290 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001291 if (!(h1m->flags & H1_MF_RESP)) {
1292 h1s->flags |= H1S_F_REQ_ERROR;
1293 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1294 }
1295 else {
1296 h1s->flags |= H1S_F_RES_ERROR;
1297 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1298 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001299 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001300 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001301 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1302 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001303 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001304 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001305
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001306 *ofs += ret;
1307 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet02a02532019-11-15 09:36:28 +01001308
1309 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001310 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001311 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001312}
1313
1314/*
1315 * Add the EOM in the HTX message and switch the message to the DONE state. It
1316 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
1317 * functions is responsible to update the parser state <h1m>. It also add the
1318 * flag CS_FL_EOI on the CS.
1319 */
1320static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1321{
Christopher Faulet6b81df72019-10-01 22:08:43 +02001322 TRACE_ENTER(H1_EV_RX_DATA, h1s->h1c->conn, h1s,, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001323 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1324 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001325 TRACE_STATE("leaving on append_eom", H1_EV_RX_DATA, h1s->h1c->conn);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001326 return 0;
1327 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001328
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001329 h1s->flags &= ~H1S_F_APPEND_EOM;
1330 h1m->state = H1_MSG_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001331 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001332 TRACE_STATE("end of message", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1333 TRACE_LEAVE(H1_EV_RX_DATA, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001334 return (sizeof(struct htx_blk) + 1);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001335}
1336
1337/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001338 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001339 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1340 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001341 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001342static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001343{
Christopher Faulet539e0292018-11-19 10:40:09 +01001344 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001345 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001346 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001347 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001348 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001349 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001350
Christopher Faulet539e0292018-11-19 10:40:09 +01001351 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001352 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001353
Christopher Fauletf2824e62018-10-01 12:12:37 +02001354 if (!conn_is_back(h1c->conn)) {
1355 h1m = &h1s->req;
1356 errflag = H1S_F_REQ_ERROR;
1357 }
1358 else {
1359 h1m = &h1s->res;
1360 errflag = H1S_F_RES_ERROR;
1361 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001362
Christopher Faulet74027762019-02-26 14:45:05 +01001363 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001364 if (h1s->flags & errflag)
1365 goto end;
1366
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001367 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001368 size_t used = htx_used_space(htx);
1369
Christopher Faulet129817b2018-09-20 16:14:40 +02001370 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001371 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001372 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001373 if (!ret)
1374 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001375
1376 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1377 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1378
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001379 if ((h1m->flags & H1_MF_RESP) &&
1380 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1381 h1m_init_res(&h1s->res);
1382 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001383 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001384 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001385 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001386 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001387 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001388 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001389 if (!ret)
1390 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001391
1392 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1393 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
1394
1395 if (h1m->state == H1_MSG_DONE)
1396 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1397 H1_EV_RX_DATA, h1c->conn, h1s, htx);
Christopher Faulet129817b2018-09-20 16:14:40 +02001398 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001399 else if (h1m->state == H1_MSG_TRAILERS) {
1400 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001401 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001402 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1403 if (!ret)
1404 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001405
1406 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1407 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001408 }
Christopher Fauletf1ef7f62019-09-03 21:55:14 +02001409 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001410 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001411
1412 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1413 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001414 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001415 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001416 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001417 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001418 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1419 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001420 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001421 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001422 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001423 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001424 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001425 if (!ret)
1426 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001427
1428 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1429 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001430 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001431 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001432 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001433 break;
1434 }
1435
Christopher Faulet30db3d72019-05-17 15:35:33 +02001436 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001437 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001438
Christopher Faulet6b81df72019-10-01 22:08:43 +02001439 if (h1s->flags & errflag) {
1440 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001441 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001442 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001443
Christopher Faulet539e0292018-11-19 10:40:09 +01001444 b_del(&h1c->ibuf, total);
1445
1446 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001447 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001448 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001449 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001450 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001451 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001452 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001453 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001454
Christopher Fauletcf56b992018-12-11 16:12:31 +01001455 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1456
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001457 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001458 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001459 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001460 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001461
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001462 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1463 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001464 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001465 if (h1m->state == H1_MSG_TUNNEL)
1466 h1s->cs->flags |= CS_FL_EOI;
1467 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001468 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001469 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001470
Christopher Faulet6b81df72019-10-01 22:08:43 +02001471 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001472 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001473
1474 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001475 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001476 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001477 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001479}
1480
Christopher Faulet129817b2018-09-20 16:14:40 +02001481/*
1482 * Process outgoing data. It parses data and transfer them from the channel buffer into
1483 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1484 * 0 if it couldn't proceed.
1485 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001486static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1487{
Christopher Faulet129817b2018-09-20 16:14:40 +02001488 struct h1s *h1s = h1c->h1s;
1489 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001490 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001491 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001492 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001493 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001494 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001495
Christopher Faulet47365272018-10-31 17:40:50 +01001496 if (!count)
1497 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001498
Christopher Faulet94b2c762019-05-24 15:28:57 +02001499 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001500 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1501
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001502 if (htx_is_empty(chn_htx))
1503 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001504
Christopher Faulet51dbc942018-09-13 09:05:15 +02001505 if (!h1_get_buf(h1c, &h1c->obuf)) {
1506 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001507 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 +02001508 goto end;
1509 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001510
Christopher Fauletf2824e62018-10-01 12:12:37 +02001511 if (!conn_is_back(h1c->conn)) {
1512 h1m = &h1s->res;
1513 errflag = H1S_F_RES_ERROR;
1514 }
1515 else {
1516 h1m = &h1s->req;
1517 errflag = H1S_F_REQ_ERROR;
1518 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001519
Christopher Faulet0e54d542019-07-04 21:22:34 +02001520 if (h1s->flags & errflag)
1521 goto end;
1522
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001523 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001524 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001525
Willy Tarreau3815b222018-12-11 19:50:43 +01001526 /* Perform some optimizations to reduce the number of buffer copies.
1527 * First, if the mux's buffer is empty and the htx area contains
1528 * exactly one data block of the same size as the requested count,
1529 * then it's possible to simply swap the caller's buffer with the
1530 * mux's output buffer and adjust offsets and length to match the
1531 * entire DATA HTX block in the middle. In this case we perform a
1532 * true zero-copy operation from end-to-end. This is the situation
1533 * that happens all the time with large files. Second, if this is not
1534 * possible, but the mux's output buffer is empty, we still have an
1535 * opportunity to avoid the copy to the intermediary buffer, by making
1536 * the intermediary buffer's area point to the output buffer's area.
1537 * In this case we want to skip the HTX header to make sure that copies
1538 * remain aligned and that this operation remains possible all the
1539 * time. This goes for headers, data blocks and any data extracted from
1540 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001541 */
1542 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001543 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001544 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001545 htx_get_blk_value(chn_htx, blk).len == count) {
1546 void *old_area = h1c->obuf.area;
1547
Christopher Faulet6b81df72019-10-01 22:08:43 +02001548 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 +01001549 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001550 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001551 h1c->obuf.data = count;
1552
1553 buf->area = old_area;
1554 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001555
Christopher Faulet6b81df72019-10-01 22:08:43 +02001556 chn_htx = (struct htx *)buf->area;
1557 htx_reset(chn_htx);
1558
Christopher Fauletadb22202018-12-12 10:32:09 +01001559 /* The message is chunked. We need to emit the chunk
1560 * size. We have at least the size of the struct htx to
1561 * write the chunk envelope. It should be enough.
1562 */
1563 if (h1m->flags & H1_MF_CHNK) {
1564 h1_emit_chunk_size(&h1c->obuf, count);
1565 h1_emit_chunk_crlf(&h1c->obuf);
1566 }
1567
Willy Tarreau3815b222018-12-11 19:50:43 +01001568 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001569 if (h1m->state == H1_MSG_DATA)
1570 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001571 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001572 else
1573 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001574 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001575 goto out;
1576 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001577 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001578 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001579 else
1580 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001581
Christopher Fauletc2518a52019-06-25 21:41:02 +02001582 tmp.data = 0;
1583 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001584 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001585 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001586 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001587 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001588 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001589 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001590
Christopher Fauletb2e84162018-12-06 11:39:49 +01001591 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001592 if (type != HTX_BLK_DATA && vlen > count)
1593 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001594
Christopher Faulet94b2c762019-05-24 15:28:57 +02001595 if (type == HTX_BLK_UNUSED)
1596 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001597
Christopher Faulet94b2c762019-05-24 15:28:57 +02001598 switch (h1m->state) {
1599 case H1_MSG_RQBEFORE:
1600 if (type != HTX_BLK_REQ_SL)
1601 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001602 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 +02001603 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001604 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001605 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001606 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001607 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001608 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001609 if (sl->flags & HTX_SL_F_BODYLESS)
1610 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001611 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001612 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001613
Christopher Faulet94b2c762019-05-24 15:28:57 +02001614 case H1_MSG_RPBEFORE:
1615 if (type != HTX_BLK_RES_SL)
1616 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001617 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 +02001618 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001619 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001620 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001621 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001622 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001623 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001624 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001625 if (sl->info.res.status < 200 &&
1626 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001627 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001628 h1m->state = H1_MSG_HDR_FIRST;
1629 break;
1630
Christopher Faulet94b2c762019-05-24 15:28:57 +02001631 case H1_MSG_HDR_FIRST:
1632 case H1_MSG_HDR_NAME:
1633 case H1_MSG_HDR_L2_LWS:
1634 if (type == HTX_BLK_EOH)
1635 goto last_lf;
1636 if (type != HTX_BLK_HDR)
1637 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001638
Christopher Faulet9768c262018-10-22 09:34:31 +02001639 h1m->state = H1_MSG_HDR_NAME;
1640 n = htx_get_blk_name(chn_htx, blk);
1641 v = htx_get_blk_value(chn_htx, blk);
1642
Christopher Faulet86d144c2019-08-14 16:32:25 +02001643 /* Skip all pseudo-headers */
1644 if (*(n.ptr) == ':')
1645 goto skip_hdr;
1646
Christopher Faulet9768c262018-10-22 09:34:31 +02001647 if (isteqi(n, ist("transfer-encoding")))
1648 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001649 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001650 /* Only skip C-L header with invalid value. */
1651 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001652 goto skip_hdr;
1653 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001654 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001655 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001656 if (!v.len)
1657 goto skip_hdr;
1658 }
1659
Christopher Faulet67d58092019-10-02 10:51:38 +02001660 /* Skip header if same name is used to add the server name */
1661 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1662 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1663 goto skip_hdr;
1664
Christopher Faulet98fbe952019-07-22 16:18:24 +02001665 /* Try to adjust the case of the header name */
1666 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1667 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001668 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001669 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001670 skip_hdr:
1671 h1m->state = H1_MSG_HDR_L2_LWS;
1672 break;
1673
Christopher Faulet94b2c762019-05-24 15:28:57 +02001674 case H1_MSG_LAST_LF:
1675 if (type != HTX_BLK_EOH)
1676 goto error;
1677 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001678 h1m->state = H1_MSG_LAST_LF;
1679 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001680 /* If the reply comes from haproxy while the request is
1681 * not finished, we force the connection close. */
1682 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1683 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1684 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1685 }
1686
1687 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001688 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001689 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001690 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001691 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001692 /* Try to adjust the case of the header name */
1693 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1694 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001695 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001696 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001697 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001698 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001699 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001700
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001701 if ((h1s->meth != HTTP_METH_CONNECT &&
1702 (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 +01001703 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1704 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1705 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1706 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1707 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001708 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001709 n = ist("transfer-encoding");
1710 v = ist("chunked");
1711 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1712 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001713 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001714 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001715 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001716 h1m->flags |= H1_MF_CHNK;
1717 }
1718
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001719 /* Now add the server name to a header (if requested) */
1720 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1721 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1722 struct server *srv = objt_server(h1c->conn->target);
1723
1724 if (srv) {
1725 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1726 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001727
1728 /* Try to adjust the case of the header name */
1729 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1730 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001731 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001732 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001733 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001734 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001735 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1736 }
1737
Christopher Fauletc2518a52019-06-25 21:41:02 +02001738 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001739 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001740
Christopher Faulet6b81df72019-10-01 22:08:43 +02001741 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1742 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1743
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001744 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1745 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1746 h1_set_req_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001747 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001748 }
1749 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1750 /* a successfull reply to a CONNECT or a protocol switching is sent
1751 * to the client . Switch the response to tunnel mode. */
1752 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001753 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001754 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001755 else if ((h1m->flags & H1_MF_RESP) &&
1756 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1757 h1m_init_res(&h1s->res);
1758 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001759 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001760 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001761 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001762 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001763 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001764 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1765 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001766 else
1767 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001768 break;
1769
Christopher Faulet94b2c762019-05-24 15:28:57 +02001770 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001771 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001772 if (type == HTX_BLK_EOM) {
1773 /* Chunked message without explicit trailers */
1774 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001775 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001776 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001777 }
1778 goto done;
1779 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001780 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001781 /* If the message is not chunked, never
1782 * add the last chunk. */
1783 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001784 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001785 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 +02001786 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001787 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001788 else if (type != HTX_BLK_DATA)
1789 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001790
1791 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 +02001792
1793
1794 if (vlen > count) {
1795 /* Get the maximum amount of data we can xferred */
1796 vlen = count;
1797 }
1798
1799 chklen = 0;
1800 if (h1m->flags & H1_MF_CHNK) {
1801 chklen = b_room(&tmp);
1802 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1803 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1804 (chklen < 1048576) ? 5 : 8);
1805 chklen += 4; /* 2 x CRLF */
1806 }
1807
1808 if (vlen + chklen > b_room(&tmp)) {
1809 /* too large for the buffer */
1810 if (chklen >= b_room(&tmp))
1811 goto full;
1812 vlen = b_room(&tmp) - chklen;
1813 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001814 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001815 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001816 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001817 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001818
1819 if (h1m->state == H1_MSG_DATA)
1820 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001821 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001822 else
1823 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Christopher Faulet08618a72019-10-08 11:59:47 +02001824 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s,, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001825 break;
1826
Christopher Faulet94b2c762019-05-24 15:28:57 +02001827 case H1_MSG_TRAILERS:
1828 if (type == HTX_BLK_EOM)
1829 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001830 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001831 goto error;
1832 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001833 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001834 /* If the message is not chunked, ignore
1835 * trailers. It may happen with H2 messages. */
1836 if (!(h1m->flags & H1_MF_CHNK))
1837 break;
1838
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001839 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001840 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001841 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001842 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1843 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001844 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001845 else { // HTX_BLK_TLR
1846 n = htx_get_blk_name(chn_htx, blk);
1847 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001848
1849 /* Try to adjust the case of the header name */
1850 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1851 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001852 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001853 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001854 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001855 break;
1856
Christopher Faulet94b2c762019-05-24 15:28:57 +02001857 case H1_MSG_DONE:
1858 if (type != HTX_BLK_EOM)
1859 goto error;
1860 done:
1861 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001862 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1863 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1864 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001865 TRACE_STATE("h1c no more busy", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001866 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001867
1868 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1869 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001870 break;
1871
Christopher Faulet9768c262018-10-22 09:34:31 +02001872 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001873 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001874 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001875 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001876 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001877 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001878 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001879 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1880 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001881 break;
1882 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001883
1884 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001885 total += vlen;
1886 count -= vlen;
1887 if (sz == vlen)
1888 blk = htx_remove_blk(chn_htx, blk);
1889 else {
1890 htx_cut_data_blk(chn_htx, blk, vlen);
1891 break;
1892 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001893 }
1894
Christopher Faulet9768c262018-10-22 09:34:31 +02001895 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001896 /* when the output buffer is empty, tmp shares the same area so that we
1897 * only have to update pointers and lengths.
1898 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001899 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1900 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001901 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001902 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001903
Willy Tarreau3815b222018-12-11 19:50:43 +01001904 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001905 out:
1906 if (!buf_room_for_htx_data(&h1c->obuf)) {
1907 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001908 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001909 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001910 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001911 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001912 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001913
1914 full:
1915 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1916 h1c->flags |= H1C_F_OUT_FULL;
1917 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001918}
1919
Christopher Faulet51dbc942018-09-13 09:05:15 +02001920/*********************************************************/
1921/* functions below are I/O callbacks from the connection */
1922/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001923static void h1_wake_stream_for_recv(struct h1s *h1s)
1924{
1925 if (h1s && h1s->recv_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001926 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001927 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001928 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001929 h1s->recv_wait = NULL;
1930 }
1931}
1932static void h1_wake_stream_for_send(struct h1s *h1s)
1933{
1934 if (h1s && h1s->send_wait) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001935 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001936 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001937 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001938 h1s->send_wait = NULL;
1939 }
1940}
1941
Christopher Faulet51dbc942018-09-13 09:05:15 +02001942/*
1943 * Attempt to read data, and subscribe if none available
1944 */
1945static int h1_recv(struct h1c *h1c)
1946{
1947 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001948 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001949 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001950 int rcvd = 0;
1951
Christopher Faulet6b81df72019-10-01 22:08:43 +02001952 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
1953
1954 if (h1c->wait_event.events & SUB_RETRY_RECV) {
1955 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01001956 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001957 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001958
Olivier Houchard75159a92018-12-03 18:46:09 +01001959 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001960 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01001961 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001962 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001963 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001964
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001965 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1966 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001967 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02001968 goto end;
1969 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001970
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001971 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001972 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001973 h1_wake_stream_for_recv(h1s);
1974 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001975 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02001976 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001977 }
1978
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001979 /*
1980 * If we only have a small amount of data, realign it,
1981 * it's probably cheaper than doing 2 recv() calls.
1982 */
1983 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1984 b_slow_realign(&h1c->ibuf, trash.area, 0);
1985
Willy Tarreau45f2b892018-12-05 07:59:27 +01001986 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001987 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001988 if (h1c->flags & H1C_F_IN_FULL) {
1989 h1c->flags &= ~H1C_F_IN_FULL;
1990 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
1991 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01001992
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001993 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001994 if (!b_data(&h1c->ibuf)) {
1995 /* try to pre-align the buffer like the rxbufs will be
1996 * to optimize memory copies.
1997 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001998 h1c->ibuf.head = sizeof(struct htx);
1999 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002000 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002001 }
Christopher Faulet47365272018-10-31 17:40:50 +01002002 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002003 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn,,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002004 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002005 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002006 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002007 if (h1s->csinfo.t_idle == -1)
2008 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2009 }
Christopher Faulet47365272018-10-31 17:40:50 +01002010 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002011
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002012 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002013 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002014 goto end;
2015 }
2016
Christopher Faulet6b81df72019-10-01 22:08:43 +02002017 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002018 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002019
Christopher Faulet9768c262018-10-22 09:34:31 +02002020 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002021 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2022 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002023
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002024 if (conn_xprt_read0_pending(conn) && h1s) {
2025 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002026 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002027 rcvd = 1;
2028 }
2029
Christopher Faulet51dbc942018-09-13 09:05:15 +02002030 if (!b_data(&h1c->ibuf))
2031 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002032 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002033 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002034 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2035 }
2036
2037 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002038 return rcvd;
2039}
2040
2041
2042/*
2043 * Try to send data if possible
2044 */
2045static int h1_send(struct h1c *h1c)
2046{
2047 struct connection *conn = h1c->conn;
2048 unsigned int flags = 0;
2049 size_t ret;
2050 int sent = 0;
2051
Christopher Faulet6b81df72019-10-01 22:08:43 +02002052 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2053
2054 if (conn->flags & CO_FL_ERROR) {
2055 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002056 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002057 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058
2059 if (!b_data(&h1c->obuf))
2060 goto end;
2061
2062 if (h1c->flags & H1C_F_OUT_FULL)
2063 flags |= CO_SFL_MSG_MORE;
2064
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002065 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066 if (ret > 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002067 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn,,, (size_t[]){ret});
2068 if (h1c->flags & H1C_F_OUT_FULL) {
2069 h1c->flags &= ~H1C_F_OUT_FULL;
2070 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2071 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002072 b_del(&h1c->obuf, ret);
2073 sent = 1;
2074 }
2075
Christopher Faulet145aa472018-12-06 10:56:20 +01002076 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002077 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002078 /* error or output closed, nothing to send, clear the buffer to release it */
2079 b_reset(&h1c->obuf);
2080 }
2081
Christopher Faulet51dbc942018-09-13 09:05:15 +02002082 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002083 if (!(h1c->flags & H1C_F_OUT_FULL))
2084 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002085
Christopher Faulet51dbc942018-09-13 09:05:15 +02002086 /* We're done, no more to send */
2087 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002088 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002089 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002090 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2091 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002092 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002093 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002094 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002095 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2096 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002097 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002098 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002099
Christopher Faulet6b81df72019-10-01 22:08:43 +02002100 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002101 return sent;
2102}
2103
Christopher Faulet51dbc942018-09-13 09:05:15 +02002104
2105/* callback called on any event by the connection handler.
2106 * It applies changes and returns zero, or < 0 if it wants immediate
2107 * destruction of the connection.
2108 */
2109static int h1_process(struct h1c * h1c)
2110{
2111 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002112 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002113
Christopher Faulet6b81df72019-10-01 22:08:43 +02002114 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2115
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002116 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002117 return -1;
2118
Christopher Fauletfeb11742018-11-29 15:12:34 +01002119 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002120 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002121 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002122 conn_xprt_read0_pending(conn))
2123 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002124 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002125 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002126 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002127 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002128 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002129 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002130 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002131 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002132 }
2133
Christopher Fauletfeb11742018-11-29 15:12:34 +01002134 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2135 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2136
Christopher Faulet6b81df72019-10-01 22:08:43 +02002137 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002138 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002139 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2140 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002141
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002142 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002143 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002144 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002145 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002146 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002147 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002148 h1s->cs->data_cb->wake(h1s->cs);
2149 }
Christopher Faulet47365272018-10-31 17:40:50 +01002150 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002151 if (h1c->task) {
2152 h1c->task->expire = TICK_ETERNITY;
2153 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002154 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002155 ? h1c->shut_timeout
2156 : h1c->timeout));
2157 task_queue(h1c->task);
2158 }
2159 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002160 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002161 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002162
2163 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002164 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002165 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002166 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002167}
2168
2169static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2170{
2171 struct h1c *h1c = ctx;
2172 int ret = 0;
2173
Christopher Faulet6b81df72019-10-01 22:08:43 +02002174 TRACE_POINT(H1_EV_H1C_WAKE, h1c->conn);
2175
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002176 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002177 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002178 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002179 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002180 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002181 h1_process(h1c);
2182 return NULL;
2183}
2184
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002185static void h1_reset(struct connection *conn)
2186{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002187
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002188}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002189
2190static int h1_wake(struct connection *conn)
2191{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002192 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002193 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002194
Christopher Faulet6b81df72019-10-01 22:08:43 +02002195 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2196
Christopher Faulet539e0292018-11-19 10:40:09 +01002197 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002198 ret = h1_process(h1c);
2199 if (ret == 0) {
2200 struct h1s *h1s = h1c->h1s;
2201
Christopher Faulet6b81df72019-10-01 22:08:43 +02002202 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2203 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002204 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002205 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002206 }
2207 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208}
2209
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002210/* Connection timeout management. The principle is that if there's no receipt
2211 * nor sending for a certain amount of time, the connection is closed.
2212 */
2213static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2214{
2215 struct h1c *h1c = context;
2216 int expired = tick_is_expired(t->expire, now_ms);
2217
Christopher Faulet6b81df72019-10-01 22:08:43 +02002218 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2219
2220 if (!expired && h1c) {
2221 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002222 return t;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002223 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002224
Olivier Houchard3f795f72019-04-17 22:51:06 +02002225 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002226
2227 if (!h1c) {
2228 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002229 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002230 return NULL;
2231 }
2232
2233 h1c->task = NULL;
2234 /* If a stream is still attached to the mux, just set an error and wait
2235 * for the stream's timeout. Otherwise, release the mux. This is only ok
2236 * because same timeouts are used.
2237 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002238 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002239 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002240 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2241 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002242 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002243 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002244
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002245 return NULL;
2246}
2247
Christopher Faulet51dbc942018-09-13 09:05:15 +02002248/*******************************************/
2249/* functions below are used by the streams */
2250/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002251
Christopher Faulet51dbc942018-09-13 09:05:15 +02002252/*
2253 * Attach a new stream to a connection
2254 * (Used for outgoing connections)
2255 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002256static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002257{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002258 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002259 struct conn_stream *cs = NULL;
2260 struct h1s *h1s;
2261
Christopher Faulet6b81df72019-10-01 22:08:43 +02002262 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2263 if (h1c->flags & H1C_F_CS_ERROR) {
2264 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 +02002265 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002266 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002267
2268 cs = cs_new(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002269 if (!cs) {
2270 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 +02002271 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002272 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002273
Olivier Houchardf502aca2018-12-14 19:42:40 +01002274 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002275 if (h1s == NULL) {
2276 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 +02002277 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002278 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002279
Christopher Faulet6b81df72019-10-01 22:08:43 +02002280 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002281 return cs;
2282 end:
2283 cs_free(cs);
2284 return NULL;
2285}
2286
2287/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2288 * this mux, it's easy as we can only store a single conn_stream.
2289 */
2290static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2291{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002292 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002293 struct h1s *h1s = h1c->h1s;
2294
2295 if (h1s)
2296 return h1s->cs;
2297
2298 return NULL;
2299}
2300
Christopher Faulet73c12072019-04-08 11:23:22 +02002301static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002302{
Christopher Faulet73c12072019-04-08 11:23:22 +02002303 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002304
Christopher Faulet6b81df72019-10-01 22:08:43 +02002305 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002306 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002307 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002308}
2309
2310/*
2311 * Detach the stream from the connection and possibly release the connection.
2312 */
2313static void h1_detach(struct conn_stream *cs)
2314{
2315 struct h1s *h1s = cs->ctx;
2316 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002317 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002318 int has_keepalive;
2319 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002320
Christopher Faulet6b81df72019-10-01 22:08:43 +02002321 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2322
Christopher Faulet51dbc942018-09-13 09:05:15 +02002323 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002324 if (!h1s) {
2325 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002326 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002327 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002328
Olivier Houchardf502aca2018-12-14 19:42:40 +01002329 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002330 h1c = h1s->h1c;
2331 h1s->cs = NULL;
2332
Olivier Houchard8a786902018-12-15 16:05:40 +01002333 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2334 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2335 h1s_destroy(h1s);
2336
2337 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002338 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002339 /* If there are any excess server data in the input buffer,
2340 * release it and close the connection ASAP (some data may
2341 * remain in the output buffer). This happens if a server sends
2342 * invalid responses. So in such case, we don't want to reuse
2343 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002344 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002345 if (b_data(&h1c->ibuf)) {
2346 h1_release_buf(h1c, &h1c->ibuf);
2347 h1c->flags |= H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002348 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002349 goto release;
2350 }
Christopher Faulet03627242019-07-19 11:34:08 +02002351
Christopher Faulet9400a392018-11-23 23:10:39 +01002352 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002353 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002354 h1c->conn->flags |= CO_FL_PRIVATE;
2355
Olivier Houchard44d59142018-12-13 18:46:22 +01002356 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002357 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002358 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2359 h1c->conn->owner = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002360 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) {
Olivier Houchard351411f2018-12-27 17:20:54 +01002361 /* The server doesn't want it, let's kill the connection right away */
2362 h1c->conn->mux->destroy(h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002363 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2364 goto end;
2365 }
2366 tasklet_wakeup(h1c->wait_event.tasklet);
2367 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2368 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002369 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002370 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002371 if (h1c->conn->owner == sess) {
2372 int ret = session_check_idle_conn(sess, h1c->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002373 if (ret == -1) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002374 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002375 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2376 goto end;
2377 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002378 else if (ret == 1) {
2379 /* The connection was added to the server list,
2380 * wake the task so we can subscribe to events
2381 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002382 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002383 TRACE_DEVEL("reusable idle connection", H1_EV_STRM_END, h1c->conn);
2384 goto end;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002385 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002386 TRACE_DEVEL("connection in idle session list", H1_EV_STRM_END, h1c->conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002387 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002388 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002389 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002390 struct server *srv = objt_server(h1c->conn->target);
2391
2392 if (srv) {
2393 if (h1c->conn->flags & CO_FL_PRIVATE)
2394 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002395 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002396 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2397 else
2398 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002399 TRACE_DEVEL("connection in idle server list", H1_EV_STRM_END, h1c->conn);
Christopher Faulet9400a392018-11-23 23:10:39 +01002400 }
2401 }
2402 }
2403
Christopher Fauletf1204b82019-07-19 14:51:06 +02002404 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002405 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet37243bc2019-07-11 15:40:25 +02002406 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2407 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2408 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002409 !h1c->conn->owner) {
2410 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002411 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002412 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002413 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002414 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002415 if (h1c->task) {
2416 h1c->task->expire = TICK_ETERNITY;
2417 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002418 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002419 ? h1c->shut_timeout
2420 : h1c->timeout));
2421 task_queue(h1c->task);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002422 TRACE_DEVEL("refreshing connection's timeout", H1_EV_STRM_END, h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002423 }
2424 }
2425 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002426 end:
2427 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002428}
2429
2430
2431static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2432{
2433 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002434 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002435
2436 if (!h1s)
2437 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002438 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002439
Christopher Faulet6b81df72019-10-01 22:08:43 +02002440 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2441
2442 if (cs->flags & CS_FL_KILL_CONN) {
2443 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2444 goto do_shutr;
2445 }
2446 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2447 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002448 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002449 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002450
Christopher Faulet6b81df72019-10-01 22:08:43 +02002451 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2452 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2453 goto end;
2454 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002455
Christopher Faulet7f366362019-04-08 10:51:20 +02002456 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002457 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2458 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002459 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002460 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002461 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002462 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002463 if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet7f366362019-04-08 10:51:20 +02002464 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002465 end:
2466 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002467}
2468
2469static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2470{
2471 struct h1s *h1s = cs->ctx;
2472 struct h1c *h1c;
2473
2474 if (!h1s)
2475 return;
2476 h1c = h1s->h1c;
2477
Christopher Faulet6b81df72019-10-01 22:08:43 +02002478 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2479
2480 if (cs->flags & CS_FL_KILL_CONN) {
2481 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002482 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002483 }
2484 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2485 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2486 goto do_shutw;
2487 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002488
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002489 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002490 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2491 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2492 goto end;
2493 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494
Christopher Faulet7f366362019-04-08 10:51:20 +02002495 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002496 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2497 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002498 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002499 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002500 end:
2501 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002502}
2503
Christopher Faulet666a0c42019-01-08 11:12:04 +01002504static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002505{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002506 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002507
Christopher Faulet6b81df72019-10-01 22:08:43 +02002508 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002509 conn_xprt_shutw(conn);
2510 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2511 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2512 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002513 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002514}
2515
2516/* Called from the upper layer, to unsubscribe to events */
2517static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2518{
2519 struct wait_event *sw;
2520 struct h1s *h1s = cs->ctx;
2521
2522 if (!h1s)
2523 return 0;
2524
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002525 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002526 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002527 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002528 BUG_ON(h1s->recv_wait != sw);
2529 sw->events &= ~SUB_RETRY_RECV;
2530 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002531 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002532 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002533 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002534 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002535 BUG_ON(h1s->send_wait != sw);
2536 sw->events &= ~SUB_RETRY_SEND;
2537 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002538 }
2539 return 0;
2540}
2541
2542/* Called from the upper layer, to subscribe to events, such as being able to send */
2543static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2544{
2545 struct wait_event *sw;
2546 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002547 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002548
2549 if (!h1s)
2550 return -1;
2551
Christopher Faulet6b81df72019-10-01 22:08:43 +02002552 if (event_type & SUB_RETRY_RECV) {
2553 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
2554 sw = param;
2555 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2556 sw->events |= SUB_RETRY_RECV;
2557 h1s->recv_wait = sw;
2558 event_type &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002559 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002560 if (event_type & SUB_RETRY_SEND) {
2561 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
2562 sw = param;
2563 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2564 sw->events |= SUB_RETRY_SEND;
2565 h1s->send_wait = sw;
2566 event_type &= ~SUB_RETRY_SEND;
2567 /*
2568 * If the conn_stream attempt to subscribe, and the
2569 * mux isn't subscribed to the connection, then it
2570 * probably means the connection wasn't established
2571 * yet, so we have to subscribe.
2572 */
2573 h1c = h1s->h1c;
2574 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2575 h1c->conn->xprt->subscribe(h1c->conn,
2576 h1c->conn->xprt_ctx,
2577 SUB_RETRY_SEND,
2578 &h1c->wait_event);
2579 }
2580 if (event_type != 0)
2581 return -1;
2582 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002583}
2584
2585/* Called from the upper layer, to receive data */
2586static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2587{
2588 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002589 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002590 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002591 size_t ret = 0;
2592
Christopher Faulet6b81df72019-10-01 22:08:43 +02002593 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002594 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002595 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002596 else
2597 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002598
Christopher Faulete18777b2019-04-16 16:46:36 +02002599 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002600 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002601 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002602 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2603 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002604 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002605 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002606 if (h1s->flags & H1S_F_SPLICED_DATA) {
2607 h1s->flags &= ~H1S_F_SPLICED_DATA;
2608 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2609 }
2610 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002611 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002612 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002613 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s,, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002614 return ret;
2615}
2616
2617
2618/* Called from the upper layer, to send data */
2619static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2620{
2621 struct h1s *h1s = cs->ctx;
2622 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002623 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002624
2625 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002626 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002627 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002628
Christopher Faulet6b81df72019-10-01 22:08:43 +02002629 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){count});
2630
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002631 /* If we're not connected yet, or we're waiting for a handshake, stop
2632 * now, as we don't want to remove everything from the channel buffer
2633 * before we're sure we can send it.
2634 */
2635 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002636 (h1c->conn->flags & CO_FL_HANDSHAKE)) {
2637 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002638 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002639 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002640
Christopher Fauletc31872f2019-06-04 22:09:36 +02002641 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002642 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002643
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002644 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2645 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002646 else
2647 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002648 if (!ret)
2649 break;
2650 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002651 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002652 if (!h1_send(h1c))
2653 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002654 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002655
Christopher Faulet6b81df72019-10-01 22:08:43 +02002656 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s,, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002657 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002658}
2659
Willy Tarreaue5733232019-05-22 19:24:06 +02002660#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002661/* Send and get, using splicing */
2662static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2663{
2664 struct h1s *h1s = cs->ctx;
2665 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2666 int ret = 0;
2667
Christopher Faulet6b81df72019-10-01 22:08:43 +02002668 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s,, (size_t[]){count});
2669
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002670 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002671 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002672 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2673 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2674 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002675 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002676 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002677 goto end;
2678 }
2679
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002680 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002681 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002682 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002683 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002684 }
2685
2686 h1s->flags &= ~H1S_F_BUF_FLUSH;
2687 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002688 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002689 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2690 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002691 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002692 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002693 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002694 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002695 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002696 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2697 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002698 }
2699
Christopher Faulet1be55f92018-10-02 15:59:23 +02002700 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002701 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002702 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002703 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002704 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002705
2706 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002707 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002708}
2709
2710static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2711{
2712 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002713 int ret = 0;
2714
Christopher Faulet6b81df72019-10-01 22:08:43 +02002715 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s,, (size_t[]){pipe->data});
2716
Christopher Faulet1be55f92018-10-02 15:59:23 +02002717 if (b_data(&h1s->h1c->obuf))
2718 goto end;
2719
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002720 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002721 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002722 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002723 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2724 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002725 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002726 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002727 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002728
2729 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002730 return ret;
2731}
2732#endif
2733
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002734static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2735{
2736 int ret = 0;
2737 switch (mux_ctl) {
2738 case MUX_STATUS:
2739 if (conn->flags & CO_FL_CONNECTED)
2740 ret |= MUX_STATUS_READY;
2741 return ret;
2742 default:
2743 return -1;
2744 }
2745}
2746
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002747/* for debugging with CLI's "show fd" command */
2748static void h1_show_fd(struct buffer *msg, struct connection *conn)
2749{
2750 struct h1c *h1c = conn->ctx;
2751 struct h1s *h1s = h1c->h1s;
2752
Christopher Fauletf376a312019-01-04 15:16:06 +01002753 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2754 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002755 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2756 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2757 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2758 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2759
2760 if (h1s) {
2761 char *method;
2762
2763 if (h1s->meth < HTTP_METH_OTHER)
2764 method = http_known_methods[h1s->meth].ptr;
2765 else
2766 method = "UNKNOWN";
2767 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2768 " .meth=%s status=%d",
2769 h1s, h1s->flags,
2770 h1m_state_str(h1s->req.state),
2771 h1m_state_str(h1s->res.state), method, h1s->status);
2772 if (h1s->cs)
2773 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2774 h1s->cs->flags, h1s->cs->data);
2775 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002776}
2777
2778
2779/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2780static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2781{
2782 struct h1_hdr_entry *entry;
2783
2784 /* Be sure there is a non-empty <to> */
2785 if (!strlen(to)) {
2786 memprintf(err, "expect <to>");
2787 return -1;
2788 }
2789
2790 /* Be sure only the case differs between <from> and <to> */
2791 if (strcasecmp(from, to)) {
2792 memprintf(err, "<from> and <to> must not differ execpt the case");
2793 return -1;
2794 }
2795
2796 /* Be sure <from> does not already existsin the tree */
2797 if (ebis_lookup(&hdrs_map.map, from)) {
2798 memprintf(err, "duplicate entry '%s'", from);
2799 return -1;
2800 }
2801
2802 /* Create the entry and insert it in the tree */
2803 entry = malloc(sizeof(*entry));
2804 if (!entry) {
2805 memprintf(err, "out of memory");
2806 return -1;
2807 }
2808
2809 entry->node.key = strdup(from);
2810 entry->name.ptr = strdup(to);
2811 entry->name.len = strlen(to);
2812 if (!entry->node.key || !entry->name.ptr) {
2813 free(entry->node.key);
2814 free(entry->name.ptr);
2815 free(entry);
2816 memprintf(err, "out of memory");
2817 return -1;
2818 }
2819 ebis_insert(&hdrs_map.map, &entry->node);
2820 return 0;
2821}
2822
2823static void h1_hdeaders_case_adjust_deinit()
2824{
2825 struct ebpt_node *node, *next;
2826 struct h1_hdr_entry *entry;
2827
2828 node = ebpt_first(&hdrs_map.map);
2829 while (node) {
2830 next = ebpt_next(node);
2831 ebpt_delete(node);
2832 entry = container_of(node, struct h1_hdr_entry, node);
2833 free(entry->node.key);
2834 free(entry->name.ptr);
2835 free(entry);
2836 node = next;
2837 }
2838 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002839}
2840
Christopher Faulet98fbe952019-07-22 16:18:24 +02002841static int cfg_h1_headers_case_adjust_postparser()
2842{
2843 FILE *file = NULL;
2844 char *c, *key_beg, *key_end, *value_beg, *value_end;
2845 char *err;
2846 int rc, line = 0, err_code = 0;
2847
2848 if (!hdrs_map.name)
2849 goto end;
2850
2851 file = fopen(hdrs_map.name, "r");
2852 if (!file) {
2853 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2854 hdrs_map.name);
2855 err_code |= ERR_ALERT | ERR_FATAL;
2856 goto end;
2857 }
2858
2859 /* now parse all lines. The file may contain only two header name per
2860 * line, separated by spaces. All heading and trailing spaces will be
2861 * ignored. Lines starting with a # are ignored.
2862 */
2863 while (fgets(trash.area, trash.size, file) != NULL) {
2864 line++;
2865 c = trash.area;
2866
2867 /* strip leading spaces and tabs */
2868 while (*c == ' ' || *c == '\t')
2869 c++;
2870
2871 /* ignore emptu lines, or lines beginning with a dash */
2872 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2873 continue;
2874
2875 /* look for the end of the key */
2876 key_beg = c;
2877 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2878 c++;
2879 key_end = c;
2880
2881 /* strip middle spaces and tabs */
2882 while (*c == ' ' || *c == '\t')
2883 c++;
2884
2885 /* look for the end of the value, it is the end of the line */
2886 value_beg = c;
2887 while (*c && *c != '\n' && *c != '\r')
2888 c++;
2889 value_end = c;
2890
2891 /* trim possibly trailing spaces and tabs */
2892 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2893 value_end--;
2894
2895 /* set final \0 and check entries */
2896 *key_end = '\0';
2897 *value_end = '\0';
2898
2899 err = NULL;
2900 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2901 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002902 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2903 hdrs_map.name, err, line);
2904 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002905 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002906 goto end;
2907 }
2908 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002909 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2910 hdrs_map.name, err, line);
2911 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002912 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002913 }
2914 }
2915
2916 end:
2917 if (file)
2918 fclose(file);
2919 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2920 return err_code;
2921}
2922
2923
2924/* config parser for global "h1-outgoing-header-case-adjust" */
2925static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2926 struct proxy *defpx, const char *file, int line,
2927 char **err)
2928{
2929 if (too_many_args(2, args, err, NULL))
2930 return -1;
2931 if (!*(args[1]) || !*(args[2])) {
2932 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2933 return -1;
2934 }
2935 return add_hdr_case_adjust(args[1], args[2], err);
2936}
2937
2938/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2939static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2940 struct proxy *defpx, const char *file, int line,
2941 char **err)
2942{
2943 if (too_many_args(1, args, err, NULL))
2944 return -1;
2945 if (!*(args[1])) {
2946 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2947 return -1;
2948 }
2949 free(hdrs_map.name);
2950 hdrs_map.name = strdup(args[1]);
2951 return 0;
2952}
2953
2954
2955/* config keyword parsers */
2956static struct cfg_kw_list cfg_kws = {{ }, {
2957 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2958 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2959 { 0, NULL, NULL },
2960 }
2961};
2962
2963INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2964REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2965
2966
Christopher Faulet51dbc942018-09-13 09:05:15 +02002967/****************************************/
2968/* MUX initialization and instanciation */
2969/****************************************/
2970
2971/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002972static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002973 .init = h1_init,
2974 .wake = h1_wake,
2975 .attach = h1_attach,
2976 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002977 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002978 .detach = h1_detach,
2979 .destroy = h1_destroy,
2980 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002981 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002982 .rcv_buf = h1_rcv_buf,
2983 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002984#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002985 .rcv_pipe = h1_rcv_pipe,
2986 .snd_pipe = h1_snd_pipe,
2987#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002988 .subscribe = h1_subscribe,
2989 .unsubscribe = h1_unsubscribe,
2990 .shutr = h1_shutr,
2991 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002992 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002993 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002994 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002995 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002996 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002997};
2998
2999
3000/* this mux registers default HTX proto */
3001static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003002{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003003
Willy Tarreau0108d902018-11-25 19:14:37 +01003004INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3005
Christopher Faulet51dbc942018-09-13 09:05:15 +02003006/*
3007 * Local variables:
3008 * c-indent-level: 8
3009 * c-basic-offset: 8
3010 * End:
3011 */