blob: 3e8527734cbc6bcf31ed8e4f1cf775f68f04051f [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 Faulet1be55f92018-10-02 15:59:23 +020019#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020020#include <types/proxy.h>
21#include <types/session.h>
22
Christopher Faulet51dbc942018-09-13 09:05:15 +020023#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020024#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020025#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010026#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020027#include <proto/stream.h>
28#include <proto/stream_interface.h>
29
30/*
31 * H1 Connection flags (32 bits)
32 */
33#define H1C_F_NONE 0x00000000
34
35/* Flags indicating why writing output data are blocked */
36#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
37#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
38/* 0x00000004 - 0x00000008 unused */
39
40/* Flags indicating why reading input data are blocked. */
41#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
42#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010043#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010044/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020045
46#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
47#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010048#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020049#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020050
Christopher Fauletf2824e62018-10-01 12:12:37 +020051#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 +020052#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020053
Christopher Faulet51dbc942018-09-13 09:05:15 +020054/*
55 * H1 Stream flags (32 bits)
56 */
Christopher Faulet129817b2018-09-20 16:14:40 +020057#define H1S_F_NONE 0x00000000
58#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020059#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
60#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020061#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020062#define H1S_F_WANT_KAL 0x00000010
63#define H1S_F_WANT_TUN 0x00000020
64#define H1S_F_WANT_CLO 0x00000040
65#define H1S_F_WANT_MSK 0x00000070
66#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010067#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010068#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010069#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau62038152019-08-23 09:29:29 +020070#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
71/* 0x00002000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020072#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073
74/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020075struct h1c {
76 struct connection *conn;
77 struct proxy *px;
78 uint32_t flags; /* Connection flags: H1C_F_* */
79
80 struct buffer ibuf; /* Input buffer to store data before parsing */
81 struct buffer obuf; /* Output buffer to store data after reformatting */
82
83 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
84 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
85
86 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010087 struct task *task; /* timeout management task */
88 int timeout; /* idle timeout duration in ticks */
89 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020090};
91
92/* H1 stream descriptor */
93struct h1s {
94 struct h1c *h1c;
95 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010096 struct cs_info csinfo; /* CS info, only used for client connections */
97 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098
Christopher Faulet51dbc942018-09-13 09:05:15 +020099 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
100 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200101
Olivier Houchardf502aca2018-12-14 19:42:40 +0100102 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200103 struct h1m req;
104 struct h1m res;
105
106 enum http_meth_t meth; /* HTTP resquest method */
107 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200108};
109
110/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100111DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
112DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200113
Christopher Faulet51dbc942018-09-13 09:05:15 +0200114static int h1_recv(struct h1c *h1c);
115static int h1_send(struct h1c *h1c);
116static int h1_process(struct h1c *h1c);
117static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100118static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100119static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200120
121/*****************************************************/
122/* functions below are for dynamic buffer management */
123/*****************************************************/
124/*
125 * Indicates whether or not the we may call the h1_recv() function to
126 * attempt to receive data into the buffer and/or parse pending data. The
127 * condition is a bit complex due to some API limits for now. The rules are the
128 * following :
129 * - if an error or a shutdown was detected on the connection and the buffer
130 * is empty, we must not attempt to receive
131 * - if the input buffer failed to be allocated, we must not try to receive
132 * and we know there is nothing pending
133 * - if no flag indicates a blocking condition, we may attempt to receive,
134 * regardless of whether the input buffer is full or not, so that only de
135 * receiving part decides whether or not to block. This is needed because
136 * the connection API indeed prevents us from re-enabling receipt that is
137 * already enabled in a polled state, so we must always immediately stop as
138 * soon as the mux can't proceed so as never to hit an end of read with data
139 * pending in the buffers.
140 * - otherwise must may not attempt to receive
141 */
142static inline int h1_recv_allowed(const struct h1c *h1c)
143{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100144 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200145 return 0;
146
Christopher Fauletcf56b992018-12-11 16:12:31 +0100147 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
148 return 0;
149
Christopher Fauletcb55f482018-12-10 11:56:47 +0100150 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200151 return 1;
152
153 return 0;
154}
155
156/*
157 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
158 * flags are used to figure what buffer was requested. It returns 1 if the
159 * allocation succeeds, in which case the connection is woken up, or 0 if it's
160 * impossible to wake up and we prefer to be woken up later.
161 */
162static int h1_buf_available(void *target)
163{
164 struct h1c *h1c = target;
165
166 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
167 h1c->flags &= ~H1C_F_IN_ALLOC;
168 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200169 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200170 return 1;
171 }
172
173 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
174 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200175 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200176 return 1;
177 }
178
Christopher Faulet51dbc942018-09-13 09:05:15 +0200179 return 0;
180}
181
182/*
183 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
184 */
185static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
186{
187 struct buffer *buf = NULL;
188
189 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
190 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
191 h1c->buf_wait.target = h1c;
192 h1c->buf_wait.wakeup_cb = h1_buf_available;
193 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
194 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
195 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
196 __conn_xprt_stop_recv(h1c->conn);
197 }
198 return buf;
199}
200
201/*
202 * Release a buffer, if any, and try to wake up entities waiting in the buffer
203 * wait queue.
204 */
205static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
206{
207 if (bptr->size) {
208 b_free(bptr);
209 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
210 }
211}
212
Willy Tarreau00f18a32019-01-26 12:19:01 +0100213/* returns the number of streams in use on a connection to figure if it's
214 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
215 * as the caller will want to know if it was the last one after a detach().
216 */
217static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200218{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100219 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200220
Willy Tarreau00f18a32019-01-26 12:19:01 +0100221 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200222}
223
Willy Tarreau00f18a32019-01-26 12:19:01 +0100224/* returns the number of streams still available on a connection */
225static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100226{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100227 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100228}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200229
Willy Tarreau00f18a32019-01-26 12:19:01 +0100230
Christopher Faulet51dbc942018-09-13 09:05:15 +0200231/*****************************************************************/
232/* functions below are dedicated to the mux setup and management */
233/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200234
235/* returns non-zero if there are input data pending for stream h1s. */
236static inline size_t h1s_data_pending(const struct h1s *h1s)
237{
238 const struct h1m *h1m;
239
240 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
241 if (h1m->state == H1_MSG_DONE)
242 return 0; // data not for this stream (e.g. pipelining)
243
244 return b_data(&h1s->h1c->ibuf);
245}
246
Christopher Faulet47365272018-10-31 17:40:50 +0100247static struct conn_stream *h1s_new_cs(struct h1s *h1s)
248{
249 struct conn_stream *cs;
250
251 cs = cs_new(h1s->h1c->conn);
252 if (!cs)
253 goto err;
254 h1s->cs = cs;
255 cs->ctx = h1s;
256
257 if (h1s->flags & H1S_F_NOT_FIRST)
258 cs->flags |= CS_FL_NOT_FIRST;
259
260 if (stream_create_from_cs(cs) < 0)
261 goto err;
262 return cs;
263
264 err:
265 cs_free(cs);
266 h1s->cs = NULL;
267 return NULL;
268}
269
Olivier Houchardf502aca2018-12-14 19:42:40 +0100270static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200271{
272 struct h1s *h1s;
273
274 h1s = pool_alloc(pool_head_h1s);
275 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100276 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200277
278 h1s->h1c = h1c;
279 h1c->h1s = h1s;
280
Olivier Houchardf502aca2018-12-14 19:42:40 +0100281 h1s->sess = sess;
282
Christopher Faulet51dbc942018-09-13 09:05:15 +0200283 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100284 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200285
286 h1s->recv_wait = NULL;
287 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200288
289 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100290 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200291
Christopher Faulet129817b2018-09-20 16:14:40 +0200292 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100293 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200294
295 h1s->status = 0;
296 h1s->meth = HTTP_METH_OTHER;
297
Christopher Faulet47365272018-10-31 17:40:50 +0100298 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
299 h1s->flags |= H1S_F_NOT_FIRST;
300 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
301
Christopher Faulet129817b2018-09-20 16:14:40 +0200302 if (!conn_is_back(h1c->conn)) {
303 if (h1c->px->options2 & PR_O2_REQBUG_OK)
304 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200305
306 /* For frontend connections we should always have a session */
307 if (!sess)
308 sess = h1c->conn->owner;
309
Dave Pirottecf67ec52019-07-10 13:57:38 +0000310 /* Timers for subsequent sessions on the same HTTP 1.x connection
311 * measure from `now`, not from the connection accept time */
312 if (h1s->flags & H1S_F_NOT_FIRST) {
313 h1s->csinfo.create_date = date;
314 h1s->csinfo.tv_create = now;
315 h1s->csinfo.t_handshake = 0;
316 h1s->csinfo.t_idle = -1;
317 }
318 else {
319 h1s->csinfo.create_date = sess->accept_date;
320 h1s->csinfo.tv_create = sess->tv_accept;
321 h1s->csinfo.t_handshake = sess->t_handshake;
322 h1s->csinfo.t_idle = -1;
323 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200324 }
325 else {
326 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
327 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200328
Christopher Fauletfeb11742018-11-29 15:12:34 +0100329 h1s->csinfo.create_date = date;
330 h1s->csinfo.tv_create = now;
331 h1s->csinfo.t_handshake = 0;
332 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200333 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100334
Christopher Faulete9b70722019-04-08 10:46:02 +0200335 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
336 * create a new one.
337 */
338 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200339 cs->ctx = h1s;
340 h1s->cs = cs;
341 }
Christopher Faulet47365272018-10-31 17:40:50 +0100342 else {
343 cs = h1s_new_cs(h1s);
344 if (!cs)
345 goto fail;
346 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100348
349 fail:
350 pool_free(pool_head_h1s, h1s);
351 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200352}
353
354static void h1s_destroy(struct h1s *h1s)
355{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200356 if (h1s) {
357 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200358
Christopher Fauletf2824e62018-10-01 12:12:37 +0200359 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360
Christopher Fauletf2824e62018-10-01 12:12:37 +0200361 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100362 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200363 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100364 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200365
Christopher Fauletcb55f482018-12-10 11:56:47 +0100366 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100367 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
368 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
369 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200370 pool_free(pool_head_h1s, h1s);
371 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372}
373
Christopher Fauletfeb11742018-11-29 15:12:34 +0100374static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
375{
376 struct h1s *h1s = cs->ctx;
377
378 if (h1s && !conn_is_back(cs->conn))
379 return &h1s->csinfo;
380 return NULL;
381}
382
Christopher Faulet51dbc942018-09-13 09:05:15 +0200383/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200384 * Initialize the mux once it's attached. It is expected that conn->ctx points
385 * to the existing conn_stream (for outgoing connections or for incoming onces
386 * during a mux upgrade) or NULL (for incoming ones during the connexion
387 * establishment). <input> is always used as Input buffer and may contain
388 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
389 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200390 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200391static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
392 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200393{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200394 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100395 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200396
397 h1c = pool_alloc(pool_head_h1c);
398 if (!h1c)
399 goto fail_h1c;
400 h1c->conn = conn;
401 h1c->px = proxy;
402
403 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200404 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200405 h1c->obuf = BUF_NULL;
406 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200407 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200408
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200410 h1c->wait_event.tasklet = tasklet_new();
411 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200413 h1c->wait_event.tasklet->process = h1_io_cb;
414 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100415 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200416
Christopher Faulete9b70722019-04-08 10:46:02 +0200417 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100418 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
419 if (tick_isset(proxy->timeout.serverfin))
420 h1c->shut_timeout = proxy->timeout.serverfin;
421 } else {
422 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
423 if (tick_isset(proxy->timeout.clientfin))
424 h1c->shut_timeout = proxy->timeout.clientfin;
425 }
426 if (tick_isset(h1c->timeout)) {
427 t = task_new(tid_bit);
428 if (!t)
429 goto fail;
430
431 h1c->task = t;
432 t->process = h1_timeout_task;
433 t->context = h1c;
434 t->expire = tick_add(now_ms, h1c->timeout);
435 }
436
Olivier Houchard985234d2019-06-13 17:54:33 +0200437 if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200438 h1c->flags |= H1C_F_CS_WAIT_CONN;
439
Christopher Fauletf2824e62018-10-01 12:12:37 +0200440 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100441 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200442 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200443
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100444 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200445
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100446
447 if (t)
448 task_queue(t);
449
Christopher Faulet51dbc942018-09-13 09:05:15 +0200450 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200451 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200452
453 /* mux->wake will be called soon to complete the operation */
454 return 0;
455
456 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200457 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200458 if (h1c->wait_event.tasklet)
459 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460 pool_free(pool_head_h1c, h1c);
461 fail_h1c:
462 return -1;
463}
464
Christopher Faulet73c12072019-04-08 11:23:22 +0200465/* release function. This one should be called to free all resources allocated
466 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200467 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200468static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200469{
Christopher Faulet61840e72019-04-15 09:33:32 +0200470 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200471
Christopher Faulet51dbc942018-09-13 09:05:15 +0200472 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200473 /* The connection must be aattached to this mux to be released */
474 if (h1c->conn && h1c->conn->ctx == h1c)
475 conn = h1c->conn;
476
477 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200478 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard13f556e2019-07-03 13:08:18 +0200479 /* Make sure we're no longer subscribed to anything */
480 if (h1c->wait_event.events)
481 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
482 h1c->wait_event.events, &h1c->wait_event);
Olivier Houchard45c44372019-06-11 14:06:23 +0200483 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200484 /* connection successfully upgraded to H2, this
485 * mux was already released */
486 return;
487 }
488 sess_log(conn->owner); /* Log if the upgrade failed */
489 }
490
Olivier Houchard45c44372019-06-11 14:06:23 +0200491
Christopher Faulet51dbc942018-09-13 09:05:15 +0200492 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
493 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
494 LIST_DEL(&h1c->buf_wait.list);
495 LIST_INIT(&h1c->buf_wait.list);
496 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
497 }
498
499 h1_release_buf(h1c, &h1c->ibuf);
500 h1_release_buf(h1c, &h1c->obuf);
501
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100502 if (h1c->task) {
503 h1c->task->context = NULL;
504 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
505 h1c->task = NULL;
506 }
507
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200508 if (h1c->wait_event.tasklet)
509 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200510
Christopher Fauletf2824e62018-10-01 12:12:37 +0200511 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200512 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100513 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200514 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200515 pool_free(pool_head_h1c, h1c);
516 }
517
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200518 if (conn) {
519 conn->mux = NULL;
520 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200521
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200522 conn_stop_tracking(conn);
523 conn_full_close(conn);
524 if (conn->destroy_cb)
525 conn->destroy_cb(conn);
526 conn_free(conn);
527 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200528}
529
530/******************************************************/
531/* functions below are for the H1 protocol processing */
532/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200533/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
534 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200535 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100536static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200537{
Christopher Faulet570d1612018-11-26 11:13:57 +0100538 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200539
Christopher Faulet570d1612018-11-26 11:13:57 +0100540 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200541 (*(p + 5) > '1' ||
542 (*(p + 5) == '1' && *(p + 7) >= '1')))
543 h1m->flags |= H1_MF_VER_11;
544}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200545
Christopher Faulet9768c262018-10-22 09:34:31 +0200546/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
547 * greater or equal to 1.1
548 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100549static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200550{
Christopher Faulet570d1612018-11-26 11:13:57 +0100551 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200552
Christopher Faulet570d1612018-11-26 11:13:57 +0100553 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200554 (*(p + 5) > '1' ||
555 (*(p + 5) == '1' && *(p + 7) >= '1')))
556 h1m->flags |= H1_MF_VER_11;
557}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200558
Christopher Faulet9768c262018-10-22 09:34:31 +0200559/*
560 * Check the validity of the request version. If the version is valid, it
561 * returns 1. Otherwise, it returns 0.
562 */
563static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
564{
565 struct h1c *h1c = h1s->h1c;
566
567 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
568 * exactly one digit "." one digit. This check may be disabled using
569 * option accept-invalid-http-request.
570 */
571 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
572 if (sl.rq.v.len != 8)
573 return 0;
574
575 if (*(sl.rq.v.ptr + 4) != '/' ||
576 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
577 *(sl.rq.v.ptr + 6) != '.' ||
578 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
579 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200580 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200581 else if (!sl.rq.v.len) {
582 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200583
Christopher Faulet9768c262018-10-22 09:34:31 +0200584 /* RFC 1945 allows only GET for HTTP/0.9 requests */
585 if (sl.rq.meth != HTTP_METH_GET)
586 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200587
Christopher Faulet9768c262018-10-22 09:34:31 +0200588 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
589 if (!sl.rq.u.len)
590 return 0;
591
592 /* Add HTTP version */
593 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100594 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200595 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100596
597 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100598 ((*(sl.rq.v.ptr + 5) > '1') ||
599 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100600 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200601 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200602}
603
Christopher Faulet9768c262018-10-22 09:34:31 +0200604/*
605 * Check the validity of the response version. If the version is valid, it
606 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200607 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200608static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200609{
Christopher Faulet9768c262018-10-22 09:34:31 +0200610 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200611
Christopher Faulet9768c262018-10-22 09:34:31 +0200612 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
613 * exactly one digit "." one digit. This check may be disabled using
614 * option accept-invalid-http-request.
615 */
616 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
617 if (sl.st.v.len != 8)
618 return 0;
619
620 if (*(sl.st.v.ptr + 4) != '/' ||
621 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
622 *(sl.st.v.ptr + 6) != '.' ||
623 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
624 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200625 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100626
627 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100628 ((*(sl.st.v.ptr + 5) > '1') ||
629 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100630 h1m->flags |= H1_MF_VER_11;
631
Christopher Faulet9768c262018-10-22 09:34:31 +0200632 return 1;
633}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200634
635/* Deduce the connection mode of the client connection, depending on the
636 * configuration and the H1 message flags. This function is called twice, the
637 * first time when the request is parsed and the second time when the response
638 * is parsed.
639 */
640static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
641{
642 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200643
644 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100645 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200646 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100647 h1s->status == 101) {
648 /* Either we've established an explicit tunnel, or we're
649 * switching the protocol. In both cases, we're very unlikely to
650 * understand the next protocols. We have to switch to tunnel
651 * mode, so that we transfer the request and responses then let
652 * this protocol pass unmodified. When we later implement
653 * specific parsers for such protocols, we'll want to check the
654 * Upgrade header which contains information about that protocol
655 * for responses with status 101 (eg: see RFC2817 about TLS).
656 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200657 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100658 }
659 else if (h1s->flags & H1S_F_WANT_KAL) {
660 /* By default the client is in KAL mode. CLOSE mode mean
661 * it is imposed by the client itself. So only change
662 * KAL mode here. */
663 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
664 /* no length known or explicit close => close */
665 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
666 }
667 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
668 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
669 /* no explict keep-alive and option httpclose => close */
670 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
671 }
672 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673 }
674 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100675 /* Input direction: first pass */
676 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
677 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200678 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100679 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200680 }
681
682 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
683 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
684 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
685}
686
687/* Deduce the connection mode of the client connection, depending on the
688 * configuration and the H1 message flags. This function is called twice, the
689 * first time when the request is parsed and the second time when the response
690 * is parsed.
691 */
692static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
693{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100694 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100695 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100696 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200697
Christopher Fauletf2824e62018-10-01 12:12:37 +0200698 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100699 /* Input direction: second pass */
700
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100702 h1s->status == 101) {
703 /* Either we've established an explicit tunnel, or we're
704 * switching the protocol. In both cases, we're very unlikely to
705 * understand the next protocols. We have to switch to tunnel
706 * mode, so that we transfer the request and responses then let
707 * this protocol pass unmodified. When we later implement
708 * specific parsers for such protocols, we'll want to check the
709 * Upgrade header which contains information about that protocol
710 * for responses with status 101 (eg: see RFC2817 about TLS).
711 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200712 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100713 }
714 else if (h1s->flags & H1S_F_WANT_KAL) {
715 /* By default the server is in KAL mode. CLOSE mode mean
716 * it is imposed by haproxy itself. So only change KAL
717 * mode here. */
718 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
719 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
720 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
721 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
722 }
723 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724 }
Christopher Faulet70033782018-12-05 13:50:11 +0100725 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100726 /* Output direction: first pass */
727 if (h1m->flags & H1_MF_CONN_CLO) {
728 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100729 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100730 }
731 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
732 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
733 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
734 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
735 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
736 /* no explicit keep-alive option httpclose/server-close => close */
737 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
738 }
Christopher Faulet70033782018-12-05 13:50:11 +0100739 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200740
741 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
742 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
743 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200744}
745
Christopher Fauletb992af02019-03-28 15:42:24 +0100746static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200747{
748 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200749
750 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
751 * token is found
752 */
753 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200754 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200755
756 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100757 if (!(h1m->flags & H1_MF_VER_11))
758 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200759 }
760 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100761 if (h1m->flags & H1_MF_VER_11)
762 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200763 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200764}
765
Christopher Fauletb992af02019-03-28 15:42:24 +0100766static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200767{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200768 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
769 * token is found
770 */
771 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200772 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200773
774 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100775 if (!(h1m->flags & H1_MF_VER_11) ||
776 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
777 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200778 }
779 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100780 if (h1m->flags & H1_MF_VER_11)
781 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200782 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200783}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200784
Christopher Fauletb992af02019-03-28 15:42:24 +0100785static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200786{
Christopher Fauletb992af02019-03-28 15:42:24 +0100787 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200788 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100789 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200790 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200791}
792
Christopher Fauletb992af02019-03-28 15:42:24 +0100793static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
794{
795 if (!conn_is_back(h1s->h1c->conn))
796 h1_set_cli_conn_mode(h1s, h1m);
797 else
798 h1_set_srv_conn_mode(h1s, h1m);
799
800 if (!(h1m->flags & H1_MF_RESP))
801 h1_update_req_conn_value(h1s, h1m, conn_val);
802 else
803 h1_update_res_conn_value(h1s, h1m, conn_val);
804}
Christopher Faulete44769b2018-11-29 23:01:45 +0100805
806/* Append the description of what is present in error snapshot <es> into <out>.
807 * The description must be small enough to always fit in a buffer. The output
808 * buffer may be the trash so the trash must not be used inside this function.
809 */
810static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
811{
812 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100813 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
814 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
815 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
816 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
817 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
818 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100819}
820/*
821 * Capture a bad request or response and archive it in the proxy's structure.
822 * By default it tries to report the error position as h1m->err_pos. However if
823 * this one is not set, it will then report h1m->next, which is the last known
824 * parsing point. The function is able to deal with wrapping buffers. It always
825 * displays buffers as a contiguous area starting at buf->p. The direction is
826 * determined thanks to the h1m's flags.
827 */
828static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
829 struct h1m *h1m, struct buffer *buf)
830{
831 struct session *sess = h1c->conn->owner;
832 struct proxy *proxy = h1c->px;
833 struct proxy *other_end = sess->fe;
834 union error_snapshot_ctx ctx;
835
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100836 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100837 other_end = si_strm(h1s->cs->data)->be;
838
839 /* http-specific part now */
840 ctx.h1.state = h1m->state;
841 ctx.h1.c_flags = h1c->flags;
842 ctx.h1.s_flags = h1s->flags;
843 ctx.h1.m_flags = h1m->flags;
844 ctx.h1.m_clen = h1m->curr_len;
845 ctx.h1.m_blen = h1m->body_len;
846
847 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
848 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100849 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
850 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100851}
852
Christopher Fauletadb22202018-12-12 10:32:09 +0100853/* Emit the chunksize followed by a CRLF in front of data of the buffer
854 * <buf>. It goes backwards and starts with the byte before the buffer's
855 * head. The caller is responsible for ensuring there is enough room left before
856 * the buffer's head for the string.
857 */
858static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
859{
860 char *beg, *end;
861
862 beg = end = b_head(buf);
863 *--beg = '\n';
864 *--beg = '\r';
865 do {
866 *--beg = hextab[chksz & 0xF];
867 } while (chksz >>= 4);
868 buf->head -= (end - beg);
869 b_add(buf, end - beg);
870}
871
872/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
873 * ensuring there is enough room left in the buffer for the string. */
874static void h1_emit_chunk_crlf(struct buffer *buf)
875{
876 *(b_peek(buf, b_data(buf))) = '\r';
877 *(b_peek(buf, b_data(buf) + 1)) = '\n';
878 b_add(buf, 2);
879}
880
Christopher Faulet129817b2018-09-20 16:14:40 +0200881/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100882 * Switch the request to tunnel mode. This function must only be called for
883 * CONNECT requests. On the client side, the mux is mark as busy on input,
884 * waiting the response.
885 */
886static void h1_set_req_tunnel_mode(struct h1s *h1s)
887{
888 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
889 h1s->req.state = H1_MSG_TUNNEL;
890 if (!conn_is_back(h1s->h1c->conn))
891 h1s->h1c->flags |= H1C_F_IN_BUSY;
892}
893
894/*
895 * Switch the response to tunnel mode. This function must only be called on
896 * successfull replies to CONNECT requests or on protocol switching. On the
897 * server side, if the request is not finished, the mux is mark as busy on
898 * input. Otherwise the request is also switch to tunnel mode.
899 */
900static void h1_set_res_tunnel_mode(struct h1s *h1s)
901{
902 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
903 h1s->res.state = H1_MSG_TUNNEL;
904 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
905 h1s->h1c->flags |= H1C_F_IN_BUSY;
906 else {
907 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
908 h1s->req.state = H1_MSG_TUNNEL;
909 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
910 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200911 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100912 }
913 }
914}
915
Christopher Faulet82f01602019-05-24 16:50:16 +0200916/* Estimate the size of the HTX headers after the parsing, including the EOH. */
917static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
918{
919 size_t sz = 0;
920 int i;
921
922 for (i = 0; hdrs[i].n.len; i++)
923 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
924 sz += sizeof(struct htx_blk) + 1;
925 return sz;
926}
927
Christopher Faulet30db3d72019-05-17 15:35:33 +0200928/* Estimate the size of the HTX request after the parsing. */
929static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
930{
931 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200932
933 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +0200934 sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200935 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200936 return sz;
937}
938
939/* Estimate the size of the HTX response after the parsing. */
940static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
941{
942 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200943
944 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +0200945 sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200946 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200947 return sz;
948}
949
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100950/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200951 * Add the EOM in the HTX message and switch the message to the DONE state. It
952 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
953 * functions is responsible to update the parser state <h1m>. It also add the
954 * flag CS_FL_EOI on the CS.
955 */
956static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
957{
Willy Tarreau62038152019-08-23 09:29:29 +0200958 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
959 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200960 return 0;
Willy Tarreau62038152019-08-23 09:29:29 +0200961 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200962
Willy Tarreau62038152019-08-23 09:29:29 +0200963 h1s->flags &= ~H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200964 h1m->state = H1_MSG_DONE;
965 h1s->cs->flags |= CS_FL_EOI;
966 return (sizeof(struct htx_blk) + 1);
967}
968
969/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200970 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200971 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
972 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800973 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200974 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200975static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200976 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200977{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200978 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200979 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100980 union h1_sl h1sl;
981 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200982 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +0200983 int ret = 0;
984
Christopher Faulet30db3d72019-05-17 15:35:33 +0200985 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100986 goto end;
987
Christopher Faulet129817b2018-09-20 16:14:40 +0200988 /* Realing input buffer if necessary */
989 if (b_head(buf) + b_data(buf) > b_wrap(buf))
990 b_slow_realign(buf, trash.area, 0);
991
Christopher Faulet842f41f2019-09-25 09:10:46 +0200992 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200993 /* Try to match H2 preface before parsing the request headers. */
994 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
995 if (ret > 0)
996 goto h2c_upgrade;
997 }
998
Christopher Faulet30db3d72019-05-17 15:35:33 +0200999 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001000 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001001 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001002 /* Incomplete or invalid message. If the input buffer only
1003 * contains headers and is full, which is detected by it being
1004 * full and the offset to be zero, it's an error because
1005 * headers are too large to be handled by the parser. */
1006 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001007 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001008 goto end;
1009 }
1010
1011 /* messages headers fully parsed, do some checks to prepare the body
1012 * parsing.
1013 */
1014
Christopher Faulet9768c262018-10-22 09:34:31 +02001015 /* Save the request's method or the response's status, check if the body
1016 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001017 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001018 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001019
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001020 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001021 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001022
1023 if (h1s->meth == HTTP_METH_CONNECT) {
1024 /* Switch CONNECT requests to tunnel mode */
1025 h1_set_req_tunnel_mode(h1s);
1026 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001027
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001028 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1029 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001030 h1m->err_state = h1m->state;
1031 goto vsn_error;
1032 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001033 }
1034 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001035 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001036
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001037 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1038 h1s->status == 101) {
1039 /* Switch successfull replies to CONNECT requests and
1040 * protocol switching to tunnel mode. */
1041 h1_set_res_tunnel_mode(h1s);
1042 }
1043 else if ((h1s->meth == HTTP_METH_HEAD) ||
1044 (h1s->status >= 100 && h1s->status < 200) ||
1045 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001046 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001047 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1048 h1m->flags |= H1_MF_XFER_LEN;
1049 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001050 }
1051 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001052 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001053 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001054 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001055 else {
1056 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001057 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001058 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001059
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001060 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1061 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001062 h1m->err_state = h1m->state;
1063 goto vsn_error;
1064 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001065 }
1066
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001067 /* Set HTX start-line flags */
1068 if (h1m->flags & H1_MF_VER_11)
1069 flags |= HTX_SL_F_VER_11;
1070 if (h1m->flags & H1_MF_XFER_ENC)
1071 flags |= HTX_SL_F_XFER_ENC;
1072 if (h1m->flags & H1_MF_XFER_LEN) {
1073 flags |= HTX_SL_F_XFER_LEN;
1074 if (h1m->flags & H1_MF_CHNK)
1075 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001076 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001077 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001078 if (h1m->body_len == 0)
1079 flags |= HTX_SL_F_BODYLESS;
1080 }
1081 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001082 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001083 }
1084
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001085 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001087 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1088 if (htx_is_empty(htx))
1089 goto error;
1090 h1m_init_req(h1m);
1091 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1092 ret = 0;
1093 goto end;
1094 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001095
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001096 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1097 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001098 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001099 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001100
1101 /* Check if the uri contains an explicit scheme and if it is
1102 * "http" or "https". */
1103 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1104 sl->flags |= HTX_SL_F_HAS_SCHM;
1105 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1106 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1107 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001108 }
1109 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001110 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1111 if (htx_is_empty(htx))
1112 goto error;
1113 h1m_init_res(h1m);
1114 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1115 ret = 0;
1116 goto end;
1117 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001118
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001119 flags |= HTX_SL_F_IS_RESP;
1120 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1121 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001122 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001123 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001124 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001125
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001126 /* Set bytes used in the HTX mesage for the headers now */
1127 sl->hdrs_bytes = htx_used_space(htx) - used;
1128
Christopher Fauletb992af02019-03-28 15:42:24 +01001129 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001130
1131 /* If body length cannot be determined, set htx->extra to
1132 * ULLONG_MAX. This value is impossible in other cases.
1133 */
1134 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001135 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001136 end:
1137 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001138
1139 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001140 h1m->err_state = h1m->state;
1141 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001142 vsn_error:
1143 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001144 h1s->cs->flags |= CS_FL_EOI;
1145 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001146 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001147 ret = 0;
1148 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001149
1150 h2c_upgrade:
1151 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001152 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001153 htx->flags |= HTX_FL_UPGRADE;
1154 ret = 0;
1155 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001156}
1157
1158/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001159 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1160 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001161 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001162 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001163 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001164static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001165 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001166 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001167{
1168 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001169 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001170
Christopher Faulet129817b2018-09-20 16:14:40 +02001171 if (h1m->flags & H1_MF_XFER_LEN) {
1172 if (h1m->flags & H1_MF_CLEN) {
1173 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001174 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001175 if ((uint64_t)ret > h1m->curr_len)
1176 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001177 if (ret > b_contig_data(buf, *ofs))
1178 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001179
Christopher Faulet9768c262018-10-22 09:34:31 +02001180 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001181 /* very often with large files we'll face the following
1182 * situation :
1183 * - htx is empty and points to <htxbuf>
1184 * - ret == buf->data
1185 * - buf->head == sizeof(struct htx)
1186 * => we can swap the buffers and place an htx header into
1187 * the target buffer instead
1188 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001189 int32_t try = ret;
1190
Willy Tarreau78f548f2018-12-05 10:02:39 +01001191 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1192 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1193 void *raw_area = buf->area;
1194 void *htx_area = htxbuf->area;
1195 struct htx_blk *blk;
1196
1197 buf->area = htx_area;
1198 htxbuf->area = raw_area;
1199 htx = (struct htx *)htxbuf->area;
1200 htx->size = htxbuf->size - sizeof(*htx);
1201 htx_reset(htx);
1202 b_set_data(htxbuf, b_size(htxbuf));
1203
1204 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1205 blk->info += ret;
1206 /* nothing else to do, the old buffer now contains an
1207 * empty pre-initialized HTX header
1208 */
1209 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001210 else {
1211 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1212 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001213 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001214 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001215 *ofs += ret;
1216 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001217 if (ret < try)
1218 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001219 }
1220
1221 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001222 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001224 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001225 }
1226 else if (h1m->flags & H1_MF_CHNK) {
1227 new_chunk:
1228 /* te:chunked : parse chunks */
1229 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001230 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001231 if (ret <= 0)
1232 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001233 h1m->state = H1_MSG_CHUNK_SIZE;
1234
Christopher Fauletf2824e62018-10-01 12:12:37 +02001235 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001236 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001237 }
1238
1239 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1240 unsigned int chksz;
1241
Christopher Faulet30db3d72019-05-17 15:35:33 +02001242 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001243 if (ret <= 0)
1244 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001245 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001246
Christopher Faulet129817b2018-09-20 16:14:40 +02001247 h1m->curr_len = chksz;
1248 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001249 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001250 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001251
1252 if (!h1m->curr_len)
1253 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001254 }
1255
1256 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001257 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001258 if ((uint64_t)ret > h1m->curr_len)
1259 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 if (ret > b_contig_data(buf, *ofs))
1261 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001262
Christopher Faulet9768c262018-10-22 09:34:31 +02001263 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001264 int32_t try = ret;
1265
1266 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001267 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001268 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001269 *ofs += ret;
1270 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001271 if (ret < try)
1272 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001273 }
1274 if (!h1m->curr_len) {
1275 h1m->state = H1_MSG_CHUNK_CRLF;
1276 goto new_chunk;
1277 }
1278 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001279 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001280 }
1281 else {
1282 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1283 * is no body. Switch the message in DONE state
1284 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001285 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001286 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001287 }
1288 }
1289 else {
1290 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001291 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001292 if (ret > b_contig_data(buf, *ofs))
1293 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001294
Christopher Faulet9768c262018-10-22 09:34:31 +02001295 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001296 int32_t try = ret;
1297
1298 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001299
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001300 *ofs += ret;
1301 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001302 if (ret < try)
1303 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001305 }
1306
1307 end:
1308 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001309 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001310 h1s->cs->flags |= CS_FL_EOI;
1311 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001312 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001313 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001314 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001315 return 0;
1316 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001317 /* update htx->extra, only when the body length is known */
1318 if (h1m->flags & H1_MF_XFER_LEN)
1319 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001320 return total;
1321}
1322
1323/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001324 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1325 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1326 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1327 * responsible to update the parser state <h1m>.
1328 */
1329static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1330 struct buffer *buf, size_t *ofs, size_t max)
1331{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001332 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001333 struct h1m tlr_h1m;
1334 int ret = 0;
1335
1336 if (!max || !b_data(buf))
1337 goto end;
1338
1339 /* Realing input buffer if necessary */
1340 if (b_peek(buf, *ofs) > b_tail(buf))
1341 b_slow_realign(buf, trash.area, 0);
1342
1343 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1344 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1345 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1346 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001347 /* Incomplete or invalid trailers. If the input buffer only
1348 * contains trailers and is full, which is detected by it being
1349 * full and the offset to be zero, it's an error because
1350 * trailers are too large to be handled by the parser. */
1351 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001352 goto error;
1353 goto end;
1354 }
1355
1356 /* messages trailers fully parsed. */
1357 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1358 if (htx_is_empty(htx))
1359 goto error;
1360 ret = 0;
1361 goto end;
1362 }
1363
1364 if (!htx_add_all_trailers(htx, hdrs))
1365 goto error;
1366
1367 *ofs += ret;
1368 h1s->flags |= H1S_F_HAVE_I_TLR;
1369 end:
1370 return ret;
1371
1372 error:
1373 h1m->err_state = h1m->state;
1374 h1m->err_pos = h1m->next;
1375 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1376 h1s->cs->flags |= CS_FL_EOI;
1377 htx->flags |= HTX_FL_PARSING_ERROR;
1378 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1379 ret = 0;
1380 goto end;
1381}
1382
1383/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001384 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001385 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1386 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001387 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001388static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001389{
Christopher Faulet539e0292018-11-19 10:40:09 +01001390 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001391 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001392 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001393 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001394 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001395 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001396
Christopher Faulet539e0292018-11-19 10:40:09 +01001397 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001398
Christopher Fauletf2824e62018-10-01 12:12:37 +02001399 if (!conn_is_back(h1c->conn)) {
1400 h1m = &h1s->req;
1401 errflag = H1S_F_REQ_ERROR;
1402 }
1403 else {
1404 h1m = &h1s->res;
1405 errflag = H1S_F_RES_ERROR;
1406 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001407
Christopher Faulet74027762019-02-26 14:45:05 +01001408 data = htx->data;
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001409 if (h1s->flags & errflag)
1410 goto end;
1411
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001412 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001413 size_t used = htx_used_space(htx);
1414
Christopher Faulet129817b2018-09-20 16:14:40 +02001415 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001416 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001417 if (!ret)
1418 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001419 if ((h1m->flags & H1_MF_RESP) &&
1420 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1421 h1m_init_res(&h1s->res);
1422 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1423 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001424 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001425 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001426 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001427 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001428 if (!ret)
1429 break;
1430 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001431 else if (h1m->state == H1_MSG_TRAILERS) {
1432 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1433 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1434 if (!ret)
1435 break;
1436 }
Christopher Faulet1021e722019-09-03 21:55:14 +02001437 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001438 break;
1439 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001440 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001441 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1442 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001443 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001444 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001445 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001446 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001447 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001448 if (!ret)
1449 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001450 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001451 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001452 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001453 break;
1454 }
1455
Christopher Faulet30db3d72019-05-17 15:35:33 +02001456 count -= htx_used_space(htx) - used;
Christopher Faulet98e2bc22019-09-03 16:26:15 +02001457 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001458
Christopher Faulet47365272018-10-31 17:40:50 +01001459 if (h1s->flags & errflag)
1460 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001461
Christopher Faulet539e0292018-11-19 10:40:09 +01001462 b_del(&h1c->ibuf, total);
1463
1464 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001465 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001466 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001467 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001468 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001469 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001470 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001471
Christopher Fauletcf56b992018-12-11 16:12:31 +01001472 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1473
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001474 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001475 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001476 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001477 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001478
Willy Tarreau62038152019-08-23 09:29:29 +02001479 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1480 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001481 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001482 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001483 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001484 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001485
Christopher Faulet30db3d72019-05-17 15:35:33 +02001486 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001487
1488 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001489 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001490 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001491 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001492}
1493
Christopher Faulet129817b2018-09-20 16:14:40 +02001494/*
1495 * Process outgoing data. It parses data and transfer them from the channel buffer into
1496 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1497 * 0 if it couldn't proceed.
1498 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001499static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1500{
Christopher Faulet129817b2018-09-20 16:14:40 +02001501 struct h1s *h1s = h1c->h1s;
1502 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 struct htx *chn_htx;
1504 struct htx_blk *blk;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001505 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001506 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001507 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001508
Christopher Faulet47365272018-10-31 17:40:50 +01001509 if (!count)
1510 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001511
Christopher Faulet94b2c762019-05-24 15:28:57 +02001512 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001513 if (htx_is_empty(chn_htx))
1514 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001515
Christopher Faulet51dbc942018-09-13 09:05:15 +02001516 if (!h1_get_buf(h1c, &h1c->obuf)) {
1517 h1c->flags |= H1C_F_OUT_ALLOC;
1518 goto end;
1519 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001520
Christopher Fauletf2824e62018-10-01 12:12:37 +02001521 if (!conn_is_back(h1c->conn)) {
1522 h1m = &h1s->res;
1523 errflag = H1S_F_RES_ERROR;
1524 }
1525 else {
1526 h1m = &h1s->req;
1527 errflag = H1S_F_REQ_ERROR;
1528 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001529
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001530 if (h1s->flags & errflag)
1531 goto end;
1532
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001533 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001534 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001535
Willy Tarreau3815b222018-12-11 19:50:43 +01001536 /* Perform some optimizations to reduce the number of buffer copies.
1537 * First, if the mux's buffer is empty and the htx area contains
1538 * exactly one data block of the same size as the requested count,
1539 * then it's possible to simply swap the caller's buffer with the
1540 * mux's output buffer and adjust offsets and length to match the
1541 * entire DATA HTX block in the middle. In this case we perform a
1542 * true zero-copy operation from end-to-end. This is the situation
1543 * that happens all the time with large files. Second, if this is not
1544 * possible, but the mux's output buffer is empty, we still have an
1545 * opportunity to avoid the copy to the intermediary buffer, by making
1546 * the intermediary buffer's area point to the output buffer's area.
1547 * In this case we want to skip the HTX header to make sure that copies
1548 * remain aligned and that this operation remains possible all the
1549 * time. This goes for headers, data blocks and any data extracted from
1550 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001551 */
1552 if (!b_data(&h1c->obuf)) {
Willy Tarreau3815b222018-12-11 19:50:43 +01001553 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001554 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001555 htx_get_blk_value(chn_htx, blk).len == count) {
1556 void *old_area = h1c->obuf.area;
1557
1558 h1c->obuf.area = buf->area;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001559 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001560 h1c->obuf.data = count;
1561
1562 buf->area = old_area;
1563 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001564
1565 /* The message is chunked. We need to emit the chunk
1566 * size. We have at least the size of the struct htx to
1567 * write the chunk envelope. It should be enough.
1568 */
1569 if (h1m->flags & H1_MF_CHNK) {
1570 h1_emit_chunk_size(&h1c->obuf, count);
1571 h1_emit_chunk_crlf(&h1c->obuf);
1572 }
1573
Willy Tarreau3815b222018-12-11 19:50:43 +01001574 total += count;
1575 goto out;
1576 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001577 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001578 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001579 else
1580 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001581
Christopher Faulet65fc1dc2019-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 Fauletb2e84162018-12-06 11:39:49 +01001589 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001590
Christopher Fauletb2e84162018-12-06 11:39:49 +01001591 vlen = sz;
1592 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001593 if (type != HTX_BLK_DATA)
Christopher Fauletb2e84162018-12-06 11:39:49 +01001594 goto copy;
1595 vlen = count;
1596 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001597
Christopher Faulet94b2c762019-05-24 15:28:57 +02001598 if (type == HTX_BLK_UNUSED)
1599 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001600
Christopher Faulet94b2c762019-05-24 15:28:57 +02001601 switch (h1m->state) {
1602 case H1_MSG_RQBEFORE:
1603 if (type != HTX_BLK_REQ_SL)
1604 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001605 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001606 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001607 h1_parse_req_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001608 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001609 goto copy;
1610 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001611 if (sl->flags & HTX_SL_F_BODYLESS)
1612 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001613 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001614 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001615
Christopher Faulet94b2c762019-05-24 15:28:57 +02001616 case H1_MSG_RPBEFORE:
1617 if (type != HTX_BLK_RES_SL)
1618 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001619 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001620 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001621 h1_parse_res_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001622 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001623 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001624 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001625 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001626 if (sl->info.res.status < 200 &&
1627 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001628 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001629 h1m->state = H1_MSG_HDR_FIRST;
1630 break;
1631
Christopher Faulet94b2c762019-05-24 15:28:57 +02001632 case H1_MSG_HDR_FIRST:
1633 case H1_MSG_HDR_NAME:
1634 case H1_MSG_HDR_L2_LWS:
1635 if (type == HTX_BLK_EOH)
1636 goto last_lf;
1637 if (type != HTX_BLK_HDR)
1638 goto error;
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
1643 if (isteqi(n, ist("transfer-encoding")))
1644 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001645 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001646 /* Only skip C-L header with invalid value. */
1647 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001648 goto skip_hdr;
1649 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001650 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001651 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001652 if (!v.len)
1653 goto skip_hdr;
1654 }
1655
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001656 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001657 goto copy;
1658 skip_hdr:
1659 h1m->state = H1_MSG_HDR_L2_LWS;
1660 break;
1661
Christopher Faulet94b2c762019-05-24 15:28:57 +02001662 case H1_MSG_LAST_LF:
1663 if (type != HTX_BLK_EOH)
1664 goto error;
1665 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001666 h1m->state = H1_MSG_LAST_LF;
1667 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001668 /* There is no "Connection:" header and
1669 * it the conn_mode must be
1670 * processed. So do it */
Christopher Faulet661bfc32019-06-17 14:07:46 +02001671 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001672 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001673 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001674 if (v.len) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001675 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001676 goto copy;
1677 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001678 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001679 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001680
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001681 if ((h1s->meth != HTTP_METH_CONNECT &&
1682 (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 +01001683 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1684 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1685 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1686 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1687 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001688 /* chunking needed but header not seen */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001689 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Willy Tarreau4710d202019-01-03 17:39:54 +01001690 goto copy;
1691 h1m->flags |= H1_MF_CHNK;
1692 }
1693
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001694 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet9768c262018-10-22 09:34:31 +02001695 goto copy;
1696
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001697 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1698 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1699 h1_set_req_tunnel_mode(h1s);
1700 }
1701 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1702 /* a successfull reply to a CONNECT or a protocol switching is sent
1703 * to the client . Switch the response to tunnel mode. */
1704 h1_set_res_tunnel_mode(h1s);
1705 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001706 else if ((h1m->flags & H1_MF_RESP) &&
1707 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1708 h1m_init_res(&h1s->res);
1709 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001710 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001711 }
Christopher Faulet33d58b52019-07-01 16:17:30 +02001712 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1713 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001714 else
1715 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001716 break;
1717
Christopher Faulet94b2c762019-05-24 15:28:57 +02001718 case H1_MSG_DATA:
Christopher Fauletaaa25062019-07-04 17:12:12 +02001719 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001720 if (type == HTX_BLK_EOM) {
1721 /* Chunked message without explicit trailers */
1722 if (h1m->flags & H1_MF_CHNK) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001723 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001724 goto copy;
1725 }
1726 goto done;
1727 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001728 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet52131682019-06-27 17:40:14 +02001729 /* If the message is not chunked, never
1730 * add the last chunk. */
1731 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet94b2c762019-05-24 15:28:57 +02001732 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001733 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001734 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001735 else if (type != HTX_BLK_DATA)
1736 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001737 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001738 v.len = vlen;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001739 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001740 goto copy;
1741 break;
1742
Christopher Faulet94b2c762019-05-24 15:28:57 +02001743 case H1_MSG_TRAILERS:
1744 if (type == HTX_BLK_EOM)
1745 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001746 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001747 goto error;
1748 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001749 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet52131682019-06-27 17:40:14 +02001750 /* If the message is not chunked, ignore
1751 * trailers. It may happen with H2 messages. */
1752 if (!(h1m->flags & H1_MF_CHNK))
1753 break;
1754
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001755 if (type == HTX_BLK_EOT) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001756 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001757 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001758 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001759 else { // HTX_BLK_TLR
1760 n = htx_get_blk_name(chn_htx, blk);
1761 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001762 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001763 goto copy;
1764 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001765 break;
1766
Christopher Faulet94b2c762019-05-24 15:28:57 +02001767 case H1_MSG_DONE:
1768 if (type != HTX_BLK_EOM)
1769 goto error;
1770 done:
1771 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001772 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1773 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1774 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1775 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001776 break;
1777
Christopher Faulet9768c262018-10-22 09:34:31 +02001778 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001779 error:
Christopher Fauletc431df82019-06-26 15:16:28 +02001780 /* Unexpected error during output processing */
1781 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001782 h1s->flags |= errflag;
Christopher Fauletc431df82019-06-26 15:16:28 +02001783 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001784 break;
1785 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001786
1787 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001788 total += vlen;
1789 count -= vlen;
1790 if (sz == vlen)
1791 blk = htx_remove_blk(chn_htx, blk);
1792 else {
1793 htx_cut_data_blk(chn_htx, blk, vlen);
1794 break;
1795 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001796 }
1797
Christopher Faulet9768c262018-10-22 09:34:31 +02001798 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001799 /* when the output buffer is empty, tmp shares the same area so that we
1800 * only have to update pointers and lengths.
1801 */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001802 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1803 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001804 else
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001805 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001806
Willy Tarreau3815b222018-12-11 19:50:43 +01001807 htx_to_buf(chn_htx, buf);
1808 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001809 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001810 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001811 end:
1812 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001813}
1814
Christopher Faulet51dbc942018-09-13 09:05:15 +02001815/*********************************************************/
1816/* functions below are I/O callbacks from the connection */
1817/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001818static void h1_wake_stream_for_recv(struct h1s *h1s)
1819{
1820 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001821 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001822 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001823 h1s->recv_wait = NULL;
1824 }
1825}
1826static void h1_wake_stream_for_send(struct h1s *h1s)
1827{
1828 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001829 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001830 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001831 h1s->send_wait = NULL;
1832 }
1833}
1834
Christopher Faulet51dbc942018-09-13 09:05:15 +02001835/*
1836 * Attempt to read data, and subscribe if none available
1837 */
1838static int h1_recv(struct h1c *h1c)
1839{
1840 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001841 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001842 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001843 int rcvd = 0;
1844
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001845 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001846 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001847
Olivier Houchard5b06cb32019-08-13 15:21:59 +02001848 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN)
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001849 return 0;
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001850
Olivier Houchard75159a92018-12-03 18:46:09 +01001851 if (!h1_recv_allowed(h1c)) {
1852 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001853 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001854 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001855
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001856 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1857 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001858 goto end;
1859 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001860
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001861 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001862 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001863 h1_wake_stream_for_recv(h1s);
1864 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001865 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001866 }
1867
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001868 /*
1869 * If we only have a small amount of data, realign it,
1870 * it's probably cheaper than doing 2 recv() calls.
1871 */
1872 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1873 b_slow_realign(&h1c->ibuf, trash.area, 0);
1874
Willy Tarreau45f2b892018-12-05 07:59:27 +01001875 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001876 if (max) {
1877 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001878
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001879 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001880 if (!b_data(&h1c->ibuf)) {
1881 /* try to pre-align the buffer like the rxbufs will be
1882 * to optimize memory copies.
1883 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001884 h1c->ibuf.head = sizeof(struct htx);
1885 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001886 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001887 }
Christopher Faulet47365272018-10-31 17:40:50 +01001888 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001889 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001890 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001891 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001892 if (h1s->csinfo.t_idle == -1)
1893 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1894 }
Christopher Faulet47365272018-10-31 17:40:50 +01001895 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001896
Christopher Fauletcf56b992018-12-11 16:12:31 +01001897 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001898 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001899 goto end;
1900 }
1901
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001902 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001903
Christopher Faulet9768c262018-10-22 09:34:31 +02001904 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001905 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1906 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001907
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001908 if (conn_xprt_read0_pending(conn) && h1s) {
1909 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001910 rcvd = 1;
1911 }
1912
Christopher Faulet51dbc942018-09-13 09:05:15 +02001913 if (!b_data(&h1c->ibuf))
1914 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001915 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001916 h1c->flags |= H1C_F_IN_FULL;
1917 return rcvd;
1918}
1919
1920
1921/*
1922 * Try to send data if possible
1923 */
1924static int h1_send(struct h1c *h1c)
1925{
1926 struct connection *conn = h1c->conn;
1927 unsigned int flags = 0;
1928 size_t ret;
1929 int sent = 0;
1930
1931 if (conn->flags & CO_FL_ERROR)
1932 return 0;
1933
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001934 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001935 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001936 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001937 return 0;
1938 }
1939
Christopher Faulet51dbc942018-09-13 09:05:15 +02001940 if (!b_data(&h1c->obuf))
1941 goto end;
1942
1943 if (h1c->flags & H1C_F_OUT_FULL)
1944 flags |= CO_SFL_MSG_MORE;
1945
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001946 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001947 if (ret > 0) {
1948 h1c->flags &= ~H1C_F_OUT_FULL;
1949 b_del(&h1c->obuf, ret);
1950 sent = 1;
1951 }
1952
Christopher Faulet145aa472018-12-06 10:56:20 +01001953 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1954 /* error or output closed, nothing to send, clear the buffer to release it */
1955 b_reset(&h1c->obuf);
1956 }
1957
Christopher Faulet51dbc942018-09-13 09:05:15 +02001958 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001959 if (!(h1c->flags & H1C_F_OUT_FULL))
1960 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001961
Christopher Faulet51dbc942018-09-13 09:05:15 +02001962 /* We're done, no more to send */
1963 if (!b_data(&h1c->obuf)) {
1964 h1_release_buf(h1c, &h1c->obuf);
1965 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001966 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001967 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001968 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001969 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001970
1971 return sent;
1972}
1973
Christopher Faulet51dbc942018-09-13 09:05:15 +02001974
1975/* callback called on any event by the connection handler.
1976 * It applies changes and returns zero, or < 0 if it wants immediate
1977 * destruction of the connection.
1978 */
1979static int h1_process(struct h1c * h1c)
1980{
1981 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001982 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001983
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001984 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001985 return -1;
1986
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001987 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02001988 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02001989 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01001990 goto end;
1991 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001992 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001993 }
1994
Christopher Fauletfeb11742018-11-29 15:12:34 +01001995 if (!h1s) {
Christopher Faulet470438c2019-07-11 15:40:25 +02001996 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001997 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001998 conn_xprt_read0_pending(conn))
1999 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002000 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002001 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002002 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002003 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002004 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002005 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002006 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002007 }
2008
Christopher Fauletfeb11742018-11-29 15:12:34 +01002009 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2010 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2011
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002012 if (conn_xprt_read0_pending(conn))
2013 h1s->flags |= H1S_F_REOS;
2014
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002015 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002016 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002017 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002018 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002019 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002020 h1s->cs->data_cb->wake(h1s->cs);
2021 }
Christopher Faulet47365272018-10-31 17:40:50 +01002022 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002023 if (h1c->task) {
2024 h1c->task->expire = TICK_ETERNITY;
2025 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002026 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 +01002027 ? h1c->shut_timeout
2028 : h1c->timeout));
2029 task_queue(h1c->task);
2030 }
2031 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002032 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002033
2034 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002035 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002036 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002037}
2038
2039static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2040{
2041 struct h1c *h1c = ctx;
2042 int ret = 0;
2043
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002044 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002045 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002046 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002047 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002048 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002049 h1_process(h1c);
2050 return NULL;
2051}
2052
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002053static void h1_reset(struct connection *conn)
2054{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002055 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002056
2057 /* Reset the flags, and let the mux know we're waiting for a connection */
2058 h1c->flags = H1C_F_CS_WAIT_CONN;
2059}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002060
2061static int h1_wake(struct connection *conn)
2062{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002063 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002064 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002065
Christopher Faulet539e0292018-11-19 10:40:09 +01002066 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002067 ret = h1_process(h1c);
2068 if (ret == 0) {
2069 struct h1s *h1s = h1c->h1s;
2070
2071 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2072 ret = h1s->cs->data_cb->wake(h1s->cs);
2073 }
2074 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002075}
2076
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002077/* Connection timeout management. The principle is that if there's no receipt
2078 * nor sending for a certain amount of time, the connection is closed.
2079 */
2080static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2081{
2082 struct h1c *h1c = context;
2083 int expired = tick_is_expired(t->expire, now_ms);
2084
2085 if (!expired && h1c)
2086 return t;
2087
Olivier Houchard3f795f72019-04-17 22:51:06 +02002088 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002089
2090 if (!h1c) {
2091 /* resources were already deleted */
2092 return NULL;
2093 }
2094
2095 h1c->task = NULL;
2096 /* If a stream is still attached to the mux, just set an error and wait
2097 * for the stream's timeout. Otherwise, release the mux. This is only ok
2098 * because same timeouts are used.
2099 */
2100 if (h1c->h1s && h1c->h1s->cs)
2101 h1c->flags |= H1C_F_CS_ERROR;
2102 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002103 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002104 return NULL;
2105}
2106
Christopher Faulet51dbc942018-09-13 09:05:15 +02002107/*******************************************/
2108/* functions below are used by the streams */
2109/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002110
Christopher Faulet51dbc942018-09-13 09:05:15 +02002111/*
2112 * Attach a new stream to a connection
2113 * (Used for outgoing connections)
2114 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002115static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002116{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002117 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002118 struct conn_stream *cs = NULL;
2119 struct h1s *h1s;
2120
2121 if (h1c->flags & H1C_F_CS_ERROR)
2122 goto end;
2123
2124 cs = cs_new(h1c->conn);
2125 if (!cs)
2126 goto end;
2127
Olivier Houchardf502aca2018-12-14 19:42:40 +01002128 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129 if (h1s == NULL)
2130 goto end;
2131
2132 return cs;
2133 end:
2134 cs_free(cs);
2135 return NULL;
2136}
2137
2138/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2139 * this mux, it's easy as we can only store a single conn_stream.
2140 */
2141static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2142{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002143 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002144 struct h1s *h1s = h1c->h1s;
2145
2146 if (h1s)
2147 return h1s->cs;
2148
2149 return NULL;
2150}
2151
Christopher Faulet73c12072019-04-08 11:23:22 +02002152static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002153{
Christopher Faulet73c12072019-04-08 11:23:22 +02002154 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002155
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002156 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002157 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002158}
2159
2160/*
2161 * Detach the stream from the connection and possibly release the connection.
2162 */
2163static void h1_detach(struct conn_stream *cs)
2164{
2165 struct h1s *h1s = cs->ctx;
2166 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002167 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002168 int has_keepalive;
2169 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002170
2171 cs->ctx = NULL;
2172 if (!h1s)
2173 return;
2174
Olivier Houchardf502aca2018-12-14 19:42:40 +01002175 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002176 h1c = h1s->h1c;
2177 h1s->cs = NULL;
2178
Olivier Houchard8a786902018-12-15 16:05:40 +01002179 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2180 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2181 h1s_destroy(h1s);
2182
2183 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002184 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002185 /* If there are any excess server data in the input buffer,
2186 * release it and close the connection ASAP (some data may
2187 * remain in the output buffer). This happens if a server sends
2188 * invalid responses. So in such case, we don't want to reuse
2189 * the connection
Christopher Faulet39e679b2019-07-19 11:34:08 +02002190 */
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002191 if (b_data(&h1c->ibuf)) {
2192 h1_release_buf(h1c, &h1c->ibuf);
2193 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2194 goto release;
2195 }
Christopher Faulet39e679b2019-07-19 11:34:08 +02002196
Christopher Faulet9400a392018-11-23 23:10:39 +01002197 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002198 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002199 h1c->conn->flags |= CO_FL_PRIVATE;
2200
Olivier Houchard44d59142018-12-13 18:46:22 +01002201 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002202 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002203 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2204 h1c->conn->owner = NULL;
2205 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2206 /* The server doesn't want it, let's kill the connection right away */
2207 h1c->conn->mux->destroy(h1c->conn);
2208 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002209 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002210 return;
2211
2212 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002213 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002214 if (h1c->conn->owner == sess) {
2215 int ret = session_check_idle_conn(sess, h1c->conn);
2216 if (ret == -1)
2217 /* The connection got destroyed, let's leave */
2218 return;
2219 else if (ret == 1) {
2220 /* The connection was added to the server list,
2221 * wake the task so we can subscribe to events
2222 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002223 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002224 return;
2225 }
2226 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002227 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002228 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002229 struct server *srv = objt_server(h1c->conn->target);
2230
2231 if (srv) {
2232 if (h1c->conn->flags & CO_FL_PRIVATE)
2233 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002234 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002235 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2236 else
2237 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2238 }
2239 }
2240 }
2241
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002242 release:
Christopher Faulet9fa93f62019-06-28 17:41:42 +02002243 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet470438c2019-07-11 15:40:25 +02002244 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2245 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2246 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2247 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002248 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002249 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002250 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002251 if (h1c->task) {
2252 h1c->task->expire = TICK_ETERNITY;
2253 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002254 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 +01002255 ? h1c->shut_timeout
2256 : h1c->timeout));
2257 task_queue(h1c->task);
2258 }
2259 }
2260 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002261}
2262
2263
2264static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2265{
2266 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002267 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002268
2269 if (!h1s)
2270 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002271 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002272
Christopher Faulet7f366362019-04-08 10:51:20 +02002273 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2274 goto do_shutr;
2275
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002276 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002277 return;
2278
Christopher Faulet7f366362019-04-08 10:51:20 +02002279 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002280 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2281 if (cs->flags & CS_FL_SHR)
2282 return;
2283 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002284 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2285 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002286 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 +02002287 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002288}
2289
2290static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2291{
2292 struct h1s *h1s = cs->ctx;
2293 struct h1c *h1c;
2294
2295 if (!h1s)
2296 return;
2297 h1c = h1s->h1c;
2298
Christopher Faulet7f366362019-04-08 10:51:20 +02002299 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2300 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002301
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002302 if ((h1c->flags & H1C_F_UPG_H2C) ||
2303 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002304 return;
2305
Christopher Faulet7f366362019-04-08 10:51:20 +02002306 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002307 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2308 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2309 return;
2310
Christopher Faulet666a0c42019-01-08 11:12:04 +01002311 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002312}
2313
Christopher Faulet666a0c42019-01-08 11:12:04 +01002314static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002315{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002316 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002317
Christopher Faulet666a0c42019-01-08 11:12:04 +01002318 conn_xprt_shutw(conn);
2319 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2320 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2321 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002322}
2323
2324/* Called from the upper layer, to unsubscribe to events */
2325static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2326{
2327 struct wait_event *sw;
2328 struct h1s *h1s = cs->ctx;
2329
2330 if (!h1s)
2331 return 0;
2332
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002333 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002334 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002335 BUG_ON(h1s->recv_wait != sw);
2336 sw->events &= ~SUB_RETRY_RECV;
2337 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002338 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002339 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002340 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002341 BUG_ON(h1s->send_wait != sw);
2342 sw->events &= ~SUB_RETRY_SEND;
2343 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002344 }
2345 return 0;
2346}
2347
2348/* Called from the upper layer, to subscribe to events, such as being able to send */
2349static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2350{
2351 struct wait_event *sw;
2352 struct h1s *h1s = cs->ctx;
2353
2354 if (!h1s)
2355 return -1;
2356
2357 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002358 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002359 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002360 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2361 sw->events |= SUB_RETRY_RECV;
2362 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002363 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002364 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002365 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002366 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2367 sw->events |= SUB_RETRY_SEND;
2368 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369 return 0;
2370 default:
2371 break;
2372 }
2373 return -1;
2374}
2375
2376/* Called from the upper layer, to receive data */
2377static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2378{
2379 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002380 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002381 size_t ret = 0;
2382
Christopher Faulet539e0292018-11-19 10:40:09 +01002383 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002384 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002385
Christopher Faulete18777b2019-04-16 16:46:36 +02002386 if (flags & CO_RFL_BUF_FLUSH) {
2387 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2388
2389 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2390 h1s->flags |= H1S_F_BUF_FLUSH;
2391 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002392 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2393 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002394 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002395 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002396 }
2397 return ret;
2398}
2399
2400
2401/* Called from the upper layer, to send data */
2402static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2403{
2404 struct h1s *h1s = cs->ctx;
2405 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002406 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002407
2408 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002409 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002410
2411 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002412 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2413 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002414
Christopher Fauletc31872f2019-06-04 22:09:36 +02002415 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002416 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002417
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002418 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2419 ret = h1_process_output(h1c, buf, count);
2420 if (!ret)
2421 break;
2422 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002423 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002424 if (!h1_send(h1c))
2425 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002426 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002427
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002428 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002429}
2430
Willy Tarreaue5733232019-05-22 19:24:06 +02002431#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002432/* Send and get, using splicing */
2433static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2434{
2435 struct h1s *h1s = cs->ctx;
2436 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2437 int ret = 0;
2438
Christopher Faulet1146f972019-05-29 14:35:24 +02002439 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002440 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002441 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2442 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002443 goto end;
2444 }
2445
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002446 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002447 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002448 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002449 }
2450
2451 h1s->flags &= ~H1S_F_BUF_FLUSH;
2452 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002453 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2454 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002455 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002456 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002457 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002458 if (!h1m->curr_len)
2459 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2460 }
2461
Christopher Faulet1be55f92018-10-02 15:59:23 +02002462 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002463 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002464 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002465 if (!pipe->data)
2466 cs->flags |= CS_FL_EOS;
2467 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002468 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002469}
2470
2471static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2472{
2473 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002474 int ret = 0;
2475
2476 if (b_data(&h1s->h1c->obuf))
2477 goto end;
2478
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002479 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002480 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002481 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002482 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002483 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002484 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002485 return ret;
2486}
2487#endif
2488
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002489/* for debugging with CLI's "show fd" command */
2490static void h1_show_fd(struct buffer *msg, struct connection *conn)
2491{
2492 struct h1c *h1c = conn->ctx;
2493 struct h1s *h1s = h1c->h1s;
2494
Christopher Fauletf376a312019-01-04 15:16:06 +01002495 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2496 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002497 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2498 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2499 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2500 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2501
2502 if (h1s) {
2503 char *method;
2504
2505 if (h1s->meth < HTTP_METH_OTHER)
2506 method = http_known_methods[h1s->meth].ptr;
2507 else
2508 method = "UNKNOWN";
2509 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2510 " .meth=%s status=%d",
2511 h1s, h1s->flags,
2512 h1m_state_str(h1s->req.state),
2513 h1m_state_str(h1s->res.state), method, h1s->status);
2514 if (h1s->cs)
2515 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2516 h1s->cs->flags, h1s->cs->data);
2517 }
2518}
2519
Christopher Faulet51dbc942018-09-13 09:05:15 +02002520/****************************************/
2521/* MUX initialization and instanciation */
2522/****************************************/
2523
2524/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002525static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002526 .init = h1_init,
2527 .wake = h1_wake,
2528 .attach = h1_attach,
2529 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002530 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002531 .detach = h1_detach,
2532 .destroy = h1_destroy,
2533 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002534 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002535 .rcv_buf = h1_rcv_buf,
2536 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002537#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002538 .rcv_pipe = h1_rcv_pipe,
2539 .snd_pipe = h1_snd_pipe,
2540#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002541 .subscribe = h1_subscribe,
2542 .unsubscribe = h1_unsubscribe,
2543 .shutr = h1_shutr,
2544 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002545 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002546 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002547 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002548 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002549};
2550
2551
2552/* this mux registers default HTX proto */
2553static struct mux_proto_list mux_proto_htx =
2554{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2555
Willy Tarreau0108d902018-11-25 19:14:37 +01002556INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2557
Christopher Faulet51dbc942018-09-13 09:05:15 +02002558/*
2559 * Local variables:
2560 * c-indent-level: 8
2561 * c-basic-offset: 8
2562 * End:
2563 */