blob: df382e0f5cc5a2dd1deaabea05f501726703c497 [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
Christopher Fauletb4bad502019-10-11 14:22:00 +02001011 if (h1m->err_pos >= 0) {
1012 /* Maybe we found an error during the parsing while we were
1013 * configured not to block on that, so we have to capture it
1014 * now.
1015 */
1016 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1017 }
1018
Christopher Faulet129817b2018-09-20 16:14:40 +02001019 /* messages headers fully parsed, do some checks to prepare the body
1020 * parsing.
1021 */
1022
Christopher Faulet9768c262018-10-22 09:34:31 +02001023 /* Save the request's method or the response's status, check if the body
1024 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001026 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001027
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001028 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001029 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001030
1031 if (h1s->meth == HTTP_METH_CONNECT) {
1032 /* Switch CONNECT requests to tunnel mode */
1033 h1_set_req_tunnel_mode(h1s);
1034 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001035
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001036 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1037 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 h1m->err_state = h1m->state;
1039 goto vsn_error;
1040 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001041 }
1042 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001043 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001044
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001045 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1046 h1s->status == 101) {
1047 /* Switch successfull replies to CONNECT requests and
1048 * protocol switching to tunnel mode. */
1049 h1_set_res_tunnel_mode(h1s);
1050 }
1051 else if ((h1s->meth == HTTP_METH_HEAD) ||
1052 (h1s->status >= 100 && h1s->status < 200) ||
1053 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001054 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001055 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1056 h1m->flags |= H1_MF_XFER_LEN;
1057 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001058 }
1059 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001060 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001061 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001062 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001063 else {
1064 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001065 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001066 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001067
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001068 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1069 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001070 h1m->err_state = h1m->state;
1071 goto vsn_error;
1072 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001073 }
1074
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001075 /* Set HTX start-line flags */
1076 if (h1m->flags & H1_MF_VER_11)
1077 flags |= HTX_SL_F_VER_11;
1078 if (h1m->flags & H1_MF_XFER_ENC)
1079 flags |= HTX_SL_F_XFER_ENC;
1080 if (h1m->flags & H1_MF_XFER_LEN) {
1081 flags |= HTX_SL_F_XFER_LEN;
1082 if (h1m->flags & H1_MF_CHNK)
1083 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001084 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001085 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001086 if (h1m->body_len == 0)
1087 flags |= HTX_SL_F_BODYLESS;
1088 }
1089 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001090 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001091 }
1092
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001093 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001094 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001095 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1096 if (htx_is_empty(htx))
1097 goto error;
1098 h1m_init_req(h1m);
1099 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1100 ret = 0;
1101 goto end;
1102 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001103
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001104 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1105 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001106 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001107 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001108
1109 /* Check if the uri contains an explicit scheme and if it is
1110 * "http" or "https". */
1111 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1112 sl->flags |= HTX_SL_F_HAS_SCHM;
1113 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1114 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1115 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116 }
1117 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001118 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1119 if (htx_is_empty(htx))
1120 goto error;
1121 h1m_init_res(h1m);
1122 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1123 ret = 0;
1124 goto end;
1125 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001126
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001127 flags |= HTX_SL_F_IS_RESP;
1128 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1129 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001130 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001131 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001132 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001133
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001134 /* Set bytes used in the HTX mesage for the headers now */
1135 sl->hdrs_bytes = htx_used_space(htx) - used;
1136
Christopher Fauletb992af02019-03-28 15:42:24 +01001137 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001138
1139 /* If body length cannot be determined, set htx->extra to
1140 * ULLONG_MAX. This value is impossible in other cases.
1141 */
1142 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001143 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001144 end:
1145 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146
1147 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001148 h1m->err_state = h1m->state;
1149 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 vsn_error:
1151 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001152 h1s->cs->flags |= CS_FL_EOI;
1153 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001154 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001155 ret = 0;
1156 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001157
1158 h2c_upgrade:
1159 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001160 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001161 htx->flags |= HTX_FL_UPGRADE;
1162 ret = 0;
1163 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001164}
1165
1166/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001167 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1168 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001169 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001170 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001171 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001172static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001173 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001174 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001175{
1176 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001177 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001178
Christopher Faulet129817b2018-09-20 16:14:40 +02001179 if (h1m->flags & H1_MF_XFER_LEN) {
1180 if (h1m->flags & H1_MF_CLEN) {
1181 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001182 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001183 if ((uint64_t)ret > h1m->curr_len)
1184 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001185 if (ret > b_contig_data(buf, *ofs))
1186 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001187
Christopher Faulet9768c262018-10-22 09:34:31 +02001188 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001189 /* very often with large files we'll face the following
1190 * situation :
1191 * - htx is empty and points to <htxbuf>
1192 * - ret == buf->data
1193 * - buf->head == sizeof(struct htx)
1194 * => we can swap the buffers and place an htx header into
1195 * the target buffer instead
1196 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001197 int32_t try = ret;
1198
Willy Tarreau78f548f2018-12-05 10:02:39 +01001199 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1200 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1201 void *raw_area = buf->area;
1202 void *htx_area = htxbuf->area;
1203 struct htx_blk *blk;
1204
1205 buf->area = htx_area;
1206 htxbuf->area = raw_area;
1207 htx = (struct htx *)htxbuf->area;
1208 htx->size = htxbuf->size - sizeof(*htx);
1209 htx_reset(htx);
1210 b_set_data(htxbuf, b_size(htxbuf));
1211
1212 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1213 blk->info += ret;
1214 /* nothing else to do, the old buffer now contains an
1215 * empty pre-initialized HTX header
1216 */
1217 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001218 else {
1219 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1220 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001221 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001222 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 *ofs += ret;
1224 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001225 if (ret < try)
1226 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001227 }
1228
1229 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001230 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001231 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001232 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001233 }
1234 else if (h1m->flags & H1_MF_CHNK) {
1235 new_chunk:
1236 /* te:chunked : parse chunks */
1237 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001238 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001239 if (ret <= 0)
1240 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 h1m->state = H1_MSG_CHUNK_SIZE;
1242
Christopher Fauletf2824e62018-10-01 12:12:37 +02001243 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001244 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001245 }
1246
1247 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1248 unsigned int chksz;
1249
Christopher Faulet30db3d72019-05-17 15:35:33 +02001250 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001251 if (ret <= 0)
1252 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001253 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001254
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 h1m->curr_len = chksz;
1256 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001257 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001258 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001259
1260 if (!h1m->curr_len)
1261 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001262 }
1263
1264 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001265 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001266 if ((uint64_t)ret > h1m->curr_len)
1267 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001268 if (ret > b_contig_data(buf, *ofs))
1269 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001270
Christopher Faulet9768c262018-10-22 09:34:31 +02001271 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001272 int32_t try = ret;
1273
1274 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001275 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001276 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001277 *ofs += ret;
1278 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001279 if (ret < try)
1280 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001281 }
1282 if (!h1m->curr_len) {
1283 h1m->state = H1_MSG_CHUNK_CRLF;
1284 goto new_chunk;
1285 }
1286 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001287 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001288 }
1289 else {
1290 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1291 * is no body. Switch the message in DONE state
1292 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001293 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 }
1296 }
1297 else {
1298 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001299 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001300 if (ret > b_contig_data(buf, *ofs))
1301 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001302
Christopher Faulet9768c262018-10-22 09:34:31 +02001303 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001304 int32_t try = ret;
1305
1306 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001307
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001308 *ofs += ret;
1309 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001310 if (ret < try)
1311 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001312 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001313 }
1314
1315 end:
1316 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001317 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001318 h1s->cs->flags |= CS_FL_EOI;
1319 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001320 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001321 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001322 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001323 return 0;
1324 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001325 /* update htx->extra, only when the body length is known */
1326 if (h1m->flags & H1_MF_XFER_LEN)
1327 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001328 return total;
1329}
1330
1331/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001332 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1333 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1334 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1335 * responsible to update the parser state <h1m>.
1336 */
1337static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1338 struct buffer *buf, size_t *ofs, size_t max)
1339{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001340 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001341 struct h1m tlr_h1m;
1342 int ret = 0;
1343
1344 if (!max || !b_data(buf))
1345 goto end;
1346
1347 /* Realing input buffer if necessary */
1348 if (b_peek(buf, *ofs) > b_tail(buf))
1349 b_slow_realign(buf, trash.area, 0);
1350
1351 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1352 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1353 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1354 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001355 /* Incomplete or invalid trailers. If the input buffer only
1356 * contains trailers and is full, which is detected by it being
1357 * full and the offset to be zero, it's an error because
1358 * trailers are too large to be handled by the parser. */
1359 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001360 goto error;
1361 goto end;
1362 }
1363
1364 /* messages trailers fully parsed. */
1365 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1366 if (htx_is_empty(htx))
1367 goto error;
1368 ret = 0;
1369 goto end;
1370 }
1371
1372 if (!htx_add_all_trailers(htx, hdrs))
1373 goto error;
1374
1375 *ofs += ret;
1376 h1s->flags |= H1S_F_HAVE_I_TLR;
1377 end:
1378 return ret;
1379
1380 error:
1381 h1m->err_state = h1m->state;
1382 h1m->err_pos = h1m->next;
1383 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1384 h1s->cs->flags |= CS_FL_EOI;
1385 htx->flags |= HTX_FL_PARSING_ERROR;
1386 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1387 ret = 0;
1388 goto end;
1389}
1390
1391/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001392 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001393 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1394 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001395 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001396static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001397{
Christopher Faulet539e0292018-11-19 10:40:09 +01001398 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001399 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001400 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001401 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001402 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001403 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001404
Christopher Faulet539e0292018-11-19 10:40:09 +01001405 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001406
Christopher Fauletf2824e62018-10-01 12:12:37 +02001407 if (!conn_is_back(h1c->conn)) {
1408 h1m = &h1s->req;
1409 errflag = H1S_F_REQ_ERROR;
1410 }
1411 else {
1412 h1m = &h1s->res;
1413 errflag = H1S_F_RES_ERROR;
1414 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001415
Christopher Faulet74027762019-02-26 14:45:05 +01001416 data = htx->data;
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001417 if (h1s->flags & errflag)
1418 goto end;
1419
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001420 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001421 size_t used = htx_used_space(htx);
1422
Christopher Faulet129817b2018-09-20 16:14:40 +02001423 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001424 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001425 if (!ret)
1426 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001427 if ((h1m->flags & H1_MF_RESP) &&
1428 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1429 h1m_init_res(&h1s->res);
1430 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1431 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001432 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001433 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001434 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001435 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001436 if (!ret)
1437 break;
1438 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001439 else if (h1m->state == H1_MSG_TRAILERS) {
1440 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1441 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1442 if (!ret)
1443 break;
1444 }
Christopher Faulet1021e722019-09-03 21:55:14 +02001445 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001446 break;
1447 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001448 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001449 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1450 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001451 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001452 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001453 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001454 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001455 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001456 if (!ret)
1457 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001458 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001459 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001460 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001461 break;
1462 }
1463
Christopher Faulet30db3d72019-05-17 15:35:33 +02001464 count -= htx_used_space(htx) - used;
Christopher Faulet98e2bc22019-09-03 16:26:15 +02001465 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001466
Christopher Faulet47365272018-10-31 17:40:50 +01001467 if (h1s->flags & errflag)
1468 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001469
Christopher Faulet539e0292018-11-19 10:40:09 +01001470 b_del(&h1c->ibuf, total);
1471
1472 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001473 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001474 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001475 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001476 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001477 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001478 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001479
Christopher Fauletcf56b992018-12-11 16:12:31 +01001480 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1481
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001482 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001483 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001484 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001485 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001486
Willy Tarreau62038152019-08-23 09:29:29 +02001487 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1488 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001489 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001490 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001491 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001492 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001493
Christopher Faulet30db3d72019-05-17 15:35:33 +02001494 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001495
1496 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001497 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001498 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001499 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001500}
1501
Christopher Faulet129817b2018-09-20 16:14:40 +02001502/*
1503 * Process outgoing data. It parses data and transfer them from the channel buffer into
1504 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1505 * 0 if it couldn't proceed.
1506 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001507static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1508{
Christopher Faulet129817b2018-09-20 16:14:40 +02001509 struct h1s *h1s = h1c->h1s;
1510 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001511 struct htx *chn_htx;
1512 struct htx_blk *blk;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001513 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001514 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001515 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001516
Christopher Faulet47365272018-10-31 17:40:50 +01001517 if (!count)
1518 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001519
Christopher Faulet94b2c762019-05-24 15:28:57 +02001520 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001521 if (htx_is_empty(chn_htx))
1522 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001523
Christopher Faulet51dbc942018-09-13 09:05:15 +02001524 if (!h1_get_buf(h1c, &h1c->obuf)) {
1525 h1c->flags |= H1C_F_OUT_ALLOC;
1526 goto end;
1527 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001528
Christopher Fauletf2824e62018-10-01 12:12:37 +02001529 if (!conn_is_back(h1c->conn)) {
1530 h1m = &h1s->res;
1531 errflag = H1S_F_RES_ERROR;
1532 }
1533 else {
1534 h1m = &h1s->req;
1535 errflag = H1S_F_REQ_ERROR;
1536 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001537
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001538 if (h1s->flags & errflag)
1539 goto end;
1540
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001541 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001542 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001543
Willy Tarreau3815b222018-12-11 19:50:43 +01001544 /* Perform some optimizations to reduce the number of buffer copies.
1545 * First, if the mux's buffer is empty and the htx area contains
1546 * exactly one data block of the same size as the requested count,
1547 * then it's possible to simply swap the caller's buffer with the
1548 * mux's output buffer and adjust offsets and length to match the
1549 * entire DATA HTX block in the middle. In this case we perform a
1550 * true zero-copy operation from end-to-end. This is the situation
1551 * that happens all the time with large files. Second, if this is not
1552 * possible, but the mux's output buffer is empty, we still have an
1553 * opportunity to avoid the copy to the intermediary buffer, by making
1554 * the intermediary buffer's area point to the output buffer's area.
1555 * In this case we want to skip the HTX header to make sure that copies
1556 * remain aligned and that this operation remains possible all the
1557 * time. This goes for headers, data blocks and any data extracted from
1558 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001559 */
1560 if (!b_data(&h1c->obuf)) {
Willy Tarreau3815b222018-12-11 19:50:43 +01001561 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001562 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001563 htx_get_blk_value(chn_htx, blk).len == count) {
1564 void *old_area = h1c->obuf.area;
1565
1566 h1c->obuf.area = buf->area;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001567 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001568 h1c->obuf.data = count;
1569
1570 buf->area = old_area;
1571 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001572
1573 /* The message is chunked. We need to emit the chunk
1574 * size. We have at least the size of the struct htx to
1575 * write the chunk envelope. It should be enough.
1576 */
1577 if (h1m->flags & H1_MF_CHNK) {
1578 h1_emit_chunk_size(&h1c->obuf, count);
1579 h1_emit_chunk_crlf(&h1c->obuf);
1580 }
1581
Willy Tarreau3815b222018-12-11 19:50:43 +01001582 total += count;
1583 goto out;
1584 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001585 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001586 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001587 else
1588 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001589
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001590 tmp.data = 0;
1591 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001592 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001593 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001594 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001595 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001596 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001597 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001598
Christopher Fauletb2e84162018-12-06 11:39:49 +01001599 vlen = sz;
1600 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001601 if (type != HTX_BLK_DATA)
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001602 goto full;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001603 vlen = count;
1604 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001605
Christopher Faulet94b2c762019-05-24 15:28:57 +02001606 if (type == HTX_BLK_UNUSED)
1607 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001608
Christopher Faulet94b2c762019-05-24 15:28:57 +02001609 switch (h1m->state) {
1610 case H1_MSG_RQBEFORE:
1611 if (type != HTX_BLK_REQ_SL)
1612 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001613 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001614 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001615 h1_parse_req_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001616 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001617 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001618 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001619 if (sl->flags & HTX_SL_F_BODYLESS)
1620 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001621 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001622 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001623
Christopher Faulet94b2c762019-05-24 15:28:57 +02001624 case H1_MSG_RPBEFORE:
1625 if (type != HTX_BLK_RES_SL)
1626 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001627 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001628 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001629 h1_parse_res_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001630 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001631 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001632 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001633 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001634 if (sl->info.res.status < 200 &&
1635 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001636 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001637 h1m->state = H1_MSG_HDR_FIRST;
1638 break;
1639
Christopher Faulet94b2c762019-05-24 15:28:57 +02001640 case H1_MSG_HDR_FIRST:
1641 case H1_MSG_HDR_NAME:
1642 case H1_MSG_HDR_L2_LWS:
1643 if (type == HTX_BLK_EOH)
1644 goto last_lf;
1645 if (type != HTX_BLK_HDR)
1646 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001647 h1m->state = H1_MSG_HDR_NAME;
1648 n = htx_get_blk_name(chn_htx, blk);
1649 v = htx_get_blk_value(chn_htx, blk);
1650
1651 if (isteqi(n, ist("transfer-encoding")))
1652 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001653 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001654 /* Only skip C-L header with invalid value. */
1655 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001656 goto skip_hdr;
1657 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001658 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001659 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 if (!v.len)
1661 goto skip_hdr;
1662 }
1663
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001664 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001665 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001666 skip_hdr:
1667 h1m->state = H1_MSG_HDR_L2_LWS;
1668 break;
1669
Christopher Faulet94b2c762019-05-24 15:28:57 +02001670 case H1_MSG_LAST_LF:
1671 if (type != HTX_BLK_EOH)
1672 goto error;
1673 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001674 h1m->state = H1_MSG_LAST_LF;
1675 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001676 /* There is no "Connection:" header and
1677 * it the conn_mode must be
1678 * processed. So do it */
Christopher Faulet661bfc32019-06-17 14:07:46 +02001679 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001680 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001681 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001682 if (v.len) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001683 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001684 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001685 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001686 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001687 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001688
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001689 if ((h1s->meth != HTTP_METH_CONNECT &&
1690 (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 +01001691 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1692 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1693 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1694 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1695 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001696 /* chunking needed but header not seen */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001697 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001698 goto full;
Willy Tarreau4710d202019-01-03 17:39:54 +01001699 h1m->flags |= H1_MF_CHNK;
1700 }
1701
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001702 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001703 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001704
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001705 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1706 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1707 h1_set_req_tunnel_mode(h1s);
1708 }
1709 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1710 /* a successfull reply to a CONNECT or a protocol switching is sent
1711 * to the client . Switch the response to tunnel mode. */
1712 h1_set_res_tunnel_mode(h1s);
1713 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001714 else if ((h1m->flags & H1_MF_RESP) &&
1715 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1716 h1m_init_res(&h1s->res);
1717 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001718 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001719 }
Christopher Faulet33d58b52019-07-01 16:17:30 +02001720 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1721 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001722 else
1723 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001724 break;
1725
Christopher Faulet94b2c762019-05-24 15:28:57 +02001726 case H1_MSG_DATA:
Christopher Fauletaaa25062019-07-04 17:12:12 +02001727 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001728 if (type == HTX_BLK_EOM) {
1729 /* Chunked message without explicit trailers */
1730 if (h1m->flags & H1_MF_CHNK) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001731 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001732 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001733 }
1734 goto done;
1735 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001736 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet52131682019-06-27 17:40:14 +02001737 /* If the message is not chunked, never
1738 * add the last chunk. */
1739 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001740 goto full;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001741 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001742 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001743 else if (type != HTX_BLK_DATA)
1744 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001745 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001746 v.len = vlen;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001747 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001748 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001749 break;
1750
Christopher Faulet94b2c762019-05-24 15:28:57 +02001751 case H1_MSG_TRAILERS:
1752 if (type == HTX_BLK_EOM)
1753 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001754 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001755 goto error;
1756 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001757 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet52131682019-06-27 17:40:14 +02001758 /* If the message is not chunked, ignore
1759 * trailers. It may happen with H2 messages. */
1760 if (!(h1m->flags & H1_MF_CHNK))
1761 break;
1762
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001763 if (type == HTX_BLK_EOT) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001764 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001765 goto full;
Christopher Faulet32188212018-11-20 18:21:43 +01001766 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001767 else { // HTX_BLK_TLR
1768 n = htx_get_blk_name(chn_htx, blk);
1769 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001770 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001771 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001772 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001773 break;
1774
Christopher Faulet94b2c762019-05-24 15:28:57 +02001775 case H1_MSG_DONE:
1776 if (type != HTX_BLK_EOM)
1777 goto error;
1778 done:
1779 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001780 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1781 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1782 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1783 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001784 break;
1785
Christopher Faulet9768c262018-10-22 09:34:31 +02001786 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001787 error:
Christopher Fauletc431df82019-06-26 15:16:28 +02001788 /* Unexpected error during output processing */
1789 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001790 h1s->flags |= errflag;
Christopher Fauletc431df82019-06-26 15:16:28 +02001791 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001792 break;
1793 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001794
1795 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001796 total += vlen;
1797 count -= vlen;
1798 if (sz == vlen)
1799 blk = htx_remove_blk(chn_htx, blk);
1800 else {
1801 htx_cut_data_blk(chn_htx, blk, vlen);
1802 break;
1803 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001804 }
1805
Christopher Faulet9768c262018-10-22 09:34:31 +02001806 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001807 /* when the output buffer is empty, tmp shares the same area so that we
1808 * only have to update pointers and lengths.
1809 */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001810 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1811 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001812 else
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001813 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001814
Willy Tarreau3815b222018-12-11 19:50:43 +01001815 htx_to_buf(chn_htx, buf);
1816 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001817 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001818 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001819 end:
1820 return total;
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001821
1822 full:
1823 h1c->flags |= H1C_F_OUT_FULL;
1824 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001825}
1826
Christopher Faulet51dbc942018-09-13 09:05:15 +02001827/*********************************************************/
1828/* functions below are I/O callbacks from the connection */
1829/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001830static void h1_wake_stream_for_recv(struct h1s *h1s)
1831{
1832 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001833 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001834 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001835 h1s->recv_wait = NULL;
1836 }
1837}
1838static void h1_wake_stream_for_send(struct h1s *h1s)
1839{
1840 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001841 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001842 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001843 h1s->send_wait = NULL;
1844 }
1845}
1846
Christopher Faulet51dbc942018-09-13 09:05:15 +02001847/*
1848 * Attempt to read data, and subscribe if none available
1849 */
1850static int h1_recv(struct h1c *h1c)
1851{
1852 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001853 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001854 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001855 int rcvd = 0;
1856
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001857 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001858 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001859
Olivier Houchard5b06cb32019-08-13 15:21:59 +02001860 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN)
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001861 return 0;
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001862
Olivier Houchard75159a92018-12-03 18:46:09 +01001863 if (!h1_recv_allowed(h1c)) {
1864 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001865 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001866 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001867
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001868 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1869 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001870 goto end;
1871 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001872
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001873 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001874 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001875 h1_wake_stream_for_recv(h1s);
1876 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001877 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001878 }
1879
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001880 /*
1881 * If we only have a small amount of data, realign it,
1882 * it's probably cheaper than doing 2 recv() calls.
1883 */
1884 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1885 b_slow_realign(&h1c->ibuf, trash.area, 0);
1886
Willy Tarreau45f2b892018-12-05 07:59:27 +01001887 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001888 if (max) {
1889 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001890
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001891 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001892 if (!b_data(&h1c->ibuf)) {
1893 /* try to pre-align the buffer like the rxbufs will be
1894 * to optimize memory copies.
1895 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001896 h1c->ibuf.head = sizeof(struct htx);
1897 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001898 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001899 }
Christopher Faulet47365272018-10-31 17:40:50 +01001900 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001901 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001902 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001903 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001904 if (h1s->csinfo.t_idle == -1)
1905 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1906 }
Christopher Faulet47365272018-10-31 17:40:50 +01001907 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001908
Christopher Fauletcf56b992018-12-11 16:12:31 +01001909 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001910 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001911 goto end;
1912 }
1913
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001914 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001915
Christopher Faulet9768c262018-10-22 09:34:31 +02001916 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001917 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1918 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001919
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001920 if (conn_xprt_read0_pending(conn) && h1s) {
1921 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001922 rcvd = 1;
1923 }
1924
Christopher Faulet51dbc942018-09-13 09:05:15 +02001925 if (!b_data(&h1c->ibuf))
1926 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001927 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001928 h1c->flags |= H1C_F_IN_FULL;
1929 return rcvd;
1930}
1931
1932
1933/*
1934 * Try to send data if possible
1935 */
1936static int h1_send(struct h1c *h1c)
1937{
1938 struct connection *conn = h1c->conn;
1939 unsigned int flags = 0;
1940 size_t ret;
1941 int sent = 0;
1942
1943 if (conn->flags & CO_FL_ERROR)
1944 return 0;
1945
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001946 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001947 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001948 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001949 return 0;
1950 }
1951
Christopher Faulet51dbc942018-09-13 09:05:15 +02001952 if (!b_data(&h1c->obuf))
1953 goto end;
1954
1955 if (h1c->flags & H1C_F_OUT_FULL)
1956 flags |= CO_SFL_MSG_MORE;
1957
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001958 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001959 if (ret > 0) {
1960 h1c->flags &= ~H1C_F_OUT_FULL;
1961 b_del(&h1c->obuf, ret);
1962 sent = 1;
1963 }
1964
Christopher Faulet145aa472018-12-06 10:56:20 +01001965 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1966 /* error or output closed, nothing to send, clear the buffer to release it */
1967 b_reset(&h1c->obuf);
1968 }
1969
Christopher Faulet51dbc942018-09-13 09:05:15 +02001970 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001971 if (!(h1c->flags & H1C_F_OUT_FULL))
1972 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001973
Christopher Faulet51dbc942018-09-13 09:05:15 +02001974 /* We're done, no more to send */
1975 if (!b_data(&h1c->obuf)) {
1976 h1_release_buf(h1c, &h1c->obuf);
1977 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001978 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001979 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001980 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001981 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001982
1983 return sent;
1984}
1985
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986
1987/* callback called on any event by the connection handler.
1988 * It applies changes and returns zero, or < 0 if it wants immediate
1989 * destruction of the connection.
1990 */
1991static int h1_process(struct h1c * h1c)
1992{
1993 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001994 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001995
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001996 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001997 return -1;
1998
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001999 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02002000 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02002001 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01002002 goto end;
2003 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01002004 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002005 }
2006
Christopher Fauletfeb11742018-11-29 15:12:34 +01002007 if (!h1s) {
Christopher Faulet470438c2019-07-11 15:40:25 +02002008 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002009 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002010 conn_xprt_read0_pending(conn))
2011 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002012 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002013 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002014 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002015 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002016 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002017 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002018 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002019 }
2020
Christopher Fauletfeb11742018-11-29 15:12:34 +01002021 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2022 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2023
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002024 if (conn_xprt_read0_pending(conn))
2025 h1s->flags |= H1S_F_REOS;
2026
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002027 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002028 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002029 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002030 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002031 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002032 h1s->cs->data_cb->wake(h1s->cs);
2033 }
Christopher Faulet47365272018-10-31 17:40:50 +01002034 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002035 if (h1c->task) {
2036 h1c->task->expire = TICK_ETERNITY;
2037 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002038 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 +01002039 ? h1c->shut_timeout
2040 : h1c->timeout));
2041 task_queue(h1c->task);
2042 }
2043 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002044 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002045
2046 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002047 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002048 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002049}
2050
2051static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2052{
2053 struct h1c *h1c = ctx;
2054 int ret = 0;
2055
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002056 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002057 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002058 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002059 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002060 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002061 h1_process(h1c);
2062 return NULL;
2063}
2064
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002065static void h1_reset(struct connection *conn)
2066{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002067 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002068
2069 /* Reset the flags, and let the mux know we're waiting for a connection */
2070 h1c->flags = H1C_F_CS_WAIT_CONN;
2071}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002072
2073static int h1_wake(struct connection *conn)
2074{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002075 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002076 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002077
Christopher Faulet539e0292018-11-19 10:40:09 +01002078 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002079 ret = h1_process(h1c);
2080 if (ret == 0) {
2081 struct h1s *h1s = h1c->h1s;
2082
2083 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2084 ret = h1s->cs->data_cb->wake(h1s->cs);
2085 }
2086 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002087}
2088
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002089/* Connection timeout management. The principle is that if there's no receipt
2090 * nor sending for a certain amount of time, the connection is closed.
2091 */
2092static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2093{
2094 struct h1c *h1c = context;
2095 int expired = tick_is_expired(t->expire, now_ms);
2096
2097 if (!expired && h1c)
2098 return t;
2099
Olivier Houchard3f795f72019-04-17 22:51:06 +02002100 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002101
2102 if (!h1c) {
2103 /* resources were already deleted */
2104 return NULL;
2105 }
2106
2107 h1c->task = NULL;
2108 /* If a stream is still attached to the mux, just set an error and wait
2109 * for the stream's timeout. Otherwise, release the mux. This is only ok
2110 * because same timeouts are used.
2111 */
2112 if (h1c->h1s && h1c->h1s->cs)
2113 h1c->flags |= H1C_F_CS_ERROR;
2114 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002115 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002116 return NULL;
2117}
2118
Christopher Faulet51dbc942018-09-13 09:05:15 +02002119/*******************************************/
2120/* functions below are used by the streams */
2121/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002122
Christopher Faulet51dbc942018-09-13 09:05:15 +02002123/*
2124 * Attach a new stream to a connection
2125 * (Used for outgoing connections)
2126 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002127static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002128{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002129 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002130 struct conn_stream *cs = NULL;
2131 struct h1s *h1s;
2132
2133 if (h1c->flags & H1C_F_CS_ERROR)
2134 goto end;
2135
2136 cs = cs_new(h1c->conn);
2137 if (!cs)
2138 goto end;
2139
Olivier Houchardf502aca2018-12-14 19:42:40 +01002140 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002141 if (h1s == NULL)
2142 goto end;
2143
2144 return cs;
2145 end:
2146 cs_free(cs);
2147 return NULL;
2148}
2149
2150/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2151 * this mux, it's easy as we can only store a single conn_stream.
2152 */
2153static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2154{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002155 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002156 struct h1s *h1s = h1c->h1s;
2157
2158 if (h1s)
2159 return h1s->cs;
2160
2161 return NULL;
2162}
2163
Christopher Faulet73c12072019-04-08 11:23:22 +02002164static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002165{
Christopher Faulet73c12072019-04-08 11:23:22 +02002166 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002167
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002168 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002169 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002170}
2171
2172/*
2173 * Detach the stream from the connection and possibly release the connection.
2174 */
2175static void h1_detach(struct conn_stream *cs)
2176{
2177 struct h1s *h1s = cs->ctx;
2178 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002179 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002180 int has_keepalive;
2181 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002182
2183 cs->ctx = NULL;
2184 if (!h1s)
2185 return;
2186
Olivier Houchardf502aca2018-12-14 19:42:40 +01002187 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002188 h1c = h1s->h1c;
2189 h1s->cs = NULL;
2190
Olivier Houchard8a786902018-12-15 16:05:40 +01002191 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2192 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2193 h1s_destroy(h1s);
2194
2195 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002196 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002197 /* If there are any excess server data in the input buffer,
2198 * release it and close the connection ASAP (some data may
2199 * remain in the output buffer). This happens if a server sends
2200 * invalid responses. So in such case, we don't want to reuse
2201 * the connection
Christopher Faulet39e679b2019-07-19 11:34:08 +02002202 */
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002203 if (b_data(&h1c->ibuf)) {
2204 h1_release_buf(h1c, &h1c->ibuf);
2205 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2206 goto release;
2207 }
Christopher Faulet39e679b2019-07-19 11:34:08 +02002208
Christopher Faulet9400a392018-11-23 23:10:39 +01002209 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002210 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002211 h1c->conn->flags |= CO_FL_PRIVATE;
2212
Olivier Houchard44d59142018-12-13 18:46:22 +01002213 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002214 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002215 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2216 h1c->conn->owner = NULL;
2217 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2218 /* The server doesn't want it, let's kill the connection right away */
2219 h1c->conn->mux->destroy(h1c->conn);
2220 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002221 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002222 return;
2223
2224 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002225 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002226 if (h1c->conn->owner == sess) {
2227 int ret = session_check_idle_conn(sess, h1c->conn);
2228 if (ret == -1)
2229 /* The connection got destroyed, let's leave */
2230 return;
2231 else if (ret == 1) {
2232 /* The connection was added to the server list,
2233 * wake the task so we can subscribe to events
2234 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002235 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002236 return;
2237 }
2238 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002239 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002240 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002241 struct server *srv = objt_server(h1c->conn->target);
2242
2243 if (srv) {
2244 if (h1c->conn->flags & CO_FL_PRIVATE)
2245 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002246 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002247 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2248 else
2249 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2250 }
2251 }
2252 }
2253
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002254 release:
Christopher Faulet9fa93f62019-06-28 17:41:42 +02002255 /* 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 +02002256 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2257 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2258 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2259 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002260 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002261 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002262 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002263 if (h1c->task) {
2264 h1c->task->expire = TICK_ETERNITY;
2265 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002266 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 +01002267 ? h1c->shut_timeout
2268 : h1c->timeout));
2269 task_queue(h1c->task);
2270 }
2271 }
2272 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002273}
2274
2275
2276static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2277{
2278 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002279 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002280
2281 if (!h1s)
2282 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002283 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002284
Christopher Faulet7f366362019-04-08 10:51:20 +02002285 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2286 goto do_shutr;
2287
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002288 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002289 return;
2290
Christopher Faulet7f366362019-04-08 10:51:20 +02002291 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002292 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2293 if (cs->flags & CS_FL_SHR)
2294 return;
2295 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002296 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2297 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002298 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 +02002299 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002300}
2301
2302static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2303{
2304 struct h1s *h1s = cs->ctx;
2305 struct h1c *h1c;
2306
2307 if (!h1s)
2308 return;
2309 h1c = h1s->h1c;
2310
Christopher Faulet7f366362019-04-08 10:51:20 +02002311 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2312 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002313
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002314 if ((h1c->flags & H1C_F_UPG_H2C) ||
2315 ((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 +02002316 return;
2317
Christopher Faulet7f366362019-04-08 10:51:20 +02002318 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002319 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2320 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2321 return;
2322
Christopher Faulet666a0c42019-01-08 11:12:04 +01002323 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002324}
2325
Christopher Faulet666a0c42019-01-08 11:12:04 +01002326static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002327{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002328 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002329
Christopher Faulet666a0c42019-01-08 11:12:04 +01002330 conn_xprt_shutw(conn);
2331 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2332 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2333 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002334}
2335
2336/* Called from the upper layer, to unsubscribe to events */
2337static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2338{
2339 struct wait_event *sw;
2340 struct h1s *h1s = cs->ctx;
2341
2342 if (!h1s)
2343 return 0;
2344
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002345 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002346 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002347 BUG_ON(h1s->recv_wait != sw);
2348 sw->events &= ~SUB_RETRY_RECV;
2349 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002350 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002351 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002352 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002353 BUG_ON(h1s->send_wait != sw);
2354 sw->events &= ~SUB_RETRY_SEND;
2355 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002356 }
2357 return 0;
2358}
2359
2360/* Called from the upper layer, to subscribe to events, such as being able to send */
2361static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2362{
2363 struct wait_event *sw;
2364 struct h1s *h1s = cs->ctx;
2365
2366 if (!h1s)
2367 return -1;
2368
2369 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002370 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002371 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002372 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2373 sw->events |= SUB_RETRY_RECV;
2374 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002375 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002376 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002377 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002378 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2379 sw->events |= SUB_RETRY_SEND;
2380 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002381 return 0;
2382 default:
2383 break;
2384 }
2385 return -1;
2386}
2387
2388/* Called from the upper layer, to receive data */
2389static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2390{
2391 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002392 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002393 size_t ret = 0;
2394
Christopher Faulet539e0292018-11-19 10:40:09 +01002395 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002396 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002397
Christopher Faulete18777b2019-04-16 16:46:36 +02002398 if (flags & CO_RFL_BUF_FLUSH) {
2399 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2400
2401 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2402 h1s->flags |= H1S_F_BUF_FLUSH;
2403 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002404 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2405 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002406 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002407 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002408 }
2409 return ret;
2410}
2411
2412
2413/* Called from the upper layer, to send data */
2414static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2415{
2416 struct h1s *h1s = cs->ctx;
2417 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002418 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002419
2420 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002421 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002422
2423 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002424 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2425 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002426
Christopher Fauletc31872f2019-06-04 22:09:36 +02002427 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002428 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002429
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002430 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2431 ret = h1_process_output(h1c, buf, count);
2432 if (!ret)
2433 break;
2434 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002435 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002436 if (!h1_send(h1c))
2437 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002438 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002439
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002440 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002441}
2442
Willy Tarreaue5733232019-05-22 19:24:06 +02002443#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002444/* Send and get, using splicing */
2445static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2446{
2447 struct h1s *h1s = cs->ctx;
2448 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2449 int ret = 0;
2450
Christopher Faulet2c411be2019-11-05 16:24:27 +01002451 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002452 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002453 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2454 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002455 goto end;
2456 }
2457
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002458 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002459 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002460 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002461 }
2462
2463 h1s->flags &= ~H1S_F_BUF_FLUSH;
2464 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002465 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2466 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002467 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002468 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002469 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002470 if (!h1m->curr_len)
2471 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2472 }
2473
Christopher Faulet1be55f92018-10-02 15:59:23 +02002474 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002475 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002476 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002477 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002478 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002479}
2480
2481static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2482{
2483 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002484 int ret = 0;
2485
2486 if (b_data(&h1s->h1c->obuf))
2487 goto end;
2488
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002489 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002490 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002491 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002492 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002493 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002494 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002495 return ret;
2496}
2497#endif
2498
Olivier Houchard198d1292019-10-25 16:19:26 +02002499static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2500{
2501 int ret = 0;
2502 switch (mux_ctl) {
2503 case MUX_STATUS:
2504 if (conn->flags & CO_FL_CONNECTED)
2505 ret |= MUX_STATUS_READY;
2506 return ret;
2507 default:
2508 return -1;
2509 }
2510}
2511
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002512/* for debugging with CLI's "show fd" command */
2513static void h1_show_fd(struct buffer *msg, struct connection *conn)
2514{
2515 struct h1c *h1c = conn->ctx;
2516 struct h1s *h1s = h1c->h1s;
2517
Christopher Fauletf376a312019-01-04 15:16:06 +01002518 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2519 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002520 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2521 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2522 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2523 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2524
2525 if (h1s) {
2526 char *method;
2527
2528 if (h1s->meth < HTTP_METH_OTHER)
2529 method = http_known_methods[h1s->meth].ptr;
2530 else
2531 method = "UNKNOWN";
2532 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2533 " .meth=%s status=%d",
2534 h1s, h1s->flags,
2535 h1m_state_str(h1s->req.state),
2536 h1m_state_str(h1s->res.state), method, h1s->status);
2537 if (h1s->cs)
2538 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2539 h1s->cs->flags, h1s->cs->data);
2540 }
2541}
2542
Christopher Faulet51dbc942018-09-13 09:05:15 +02002543/****************************************/
2544/* MUX initialization and instanciation */
2545/****************************************/
2546
2547/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002548static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002549 .init = h1_init,
2550 .wake = h1_wake,
2551 .attach = h1_attach,
2552 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002553 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002554 .detach = h1_detach,
2555 .destroy = h1_destroy,
2556 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002557 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002558 .rcv_buf = h1_rcv_buf,
2559 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002560#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002561 .rcv_pipe = h1_rcv_pipe,
2562 .snd_pipe = h1_snd_pipe,
2563#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002564 .subscribe = h1_subscribe,
2565 .unsubscribe = h1_unsubscribe,
2566 .shutr = h1_shutr,
2567 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002568 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002569 .reset = h1_reset,
Olivier Houchard198d1292019-10-25 16:19:26 +02002570 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002571 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002572 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002573};
2574
2575
2576/* this mux registers default HTX proto */
2577static struct mux_proto_list mux_proto_htx =
2578{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2579
Willy Tarreau0108d902018-11-25 19:14:37 +01002580INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2581
Christopher Faulet51dbc942018-09-13 09:05:15 +02002582/*
2583 * Local variables:
2584 * c-indent-level: 8
2585 * c-basic-offset: 8
2586 * End:
2587 */