blob: 9d6aaf23c291b39541235703901d7cdf637ac5e6 [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 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +020070/* 0x00001000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020071#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020072
73/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020074struct h1c {
75 struct connection *conn;
76 struct proxy *px;
77 uint32_t flags; /* Connection flags: H1C_F_* */
78
79 struct buffer ibuf; /* Input buffer to store data before parsing */
80 struct buffer obuf; /* Output buffer to store data after reformatting */
81
82 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
83 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
84
85 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010086 struct task *task; /* timeout management task */
87 int timeout; /* idle timeout duration in ticks */
88 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020089};
90
91/* H1 stream descriptor */
92struct h1s {
93 struct h1c *h1c;
94 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010095 struct cs_info csinfo; /* CS info, only used for client connections */
96 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020097
Christopher Faulet51dbc942018-09-13 09:05:15 +020098 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
99 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200100
Olivier Houchardf502aca2018-12-14 19:42:40 +0100101 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200102 struct h1m req;
103 struct h1m res;
104
105 enum http_meth_t meth; /* HTTP resquest method */
106 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200107};
108
109/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100110DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
111DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200112
Christopher Faulet51dbc942018-09-13 09:05:15 +0200113static int h1_recv(struct h1c *h1c);
114static int h1_send(struct h1c *h1c);
115static int h1_process(struct h1c *h1c);
116static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100117static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100118static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200119
120/*****************************************************/
121/* functions below are for dynamic buffer management */
122/*****************************************************/
123/*
124 * Indicates whether or not the we may call the h1_recv() function to
125 * attempt to receive data into the buffer and/or parse pending data. The
126 * condition is a bit complex due to some API limits for now. The rules are the
127 * following :
128 * - if an error or a shutdown was detected on the connection and the buffer
129 * is empty, we must not attempt to receive
130 * - if the input buffer failed to be allocated, we must not try to receive
131 * and we know there is nothing pending
132 * - if no flag indicates a blocking condition, we may attempt to receive,
133 * regardless of whether the input buffer is full or not, so that only de
134 * receiving part decides whether or not to block. This is needed because
135 * the connection API indeed prevents us from re-enabling receipt that is
136 * already enabled in a polled state, so we must always immediately stop as
137 * soon as the mux can't proceed so as never to hit an end of read with data
138 * pending in the buffers.
139 * - otherwise must may not attempt to receive
140 */
141static inline int h1_recv_allowed(const struct h1c *h1c)
142{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100143 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200144 return 0;
145
Christopher Fauletcf56b992018-12-11 16:12:31 +0100146 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
147 return 0;
148
Christopher Fauletcb55f482018-12-10 11:56:47 +0100149 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200150 return 1;
151
152 return 0;
153}
154
155/*
156 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
157 * flags are used to figure what buffer was requested. It returns 1 if the
158 * allocation succeeds, in which case the connection is woken up, or 0 if it's
159 * impossible to wake up and we prefer to be woken up later.
160 */
161static int h1_buf_available(void *target)
162{
163 struct h1c *h1c = target;
164
165 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
166 h1c->flags &= ~H1C_F_IN_ALLOC;
167 if (h1_recv_allowed(h1c))
168 tasklet_wakeup(h1c->wait_event.task);
169 return 1;
170 }
171
172 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
173 h1c->flags &= ~H1C_F_OUT_ALLOC;
174 tasklet_wakeup(h1c->wait_event.task);
175 return 1;
176 }
177
Christopher Faulet51dbc942018-09-13 09:05:15 +0200178 return 0;
179}
180
181/*
182 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
183 */
184static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
185{
186 struct buffer *buf = NULL;
187
188 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
189 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
190 h1c->buf_wait.target = h1c;
191 h1c->buf_wait.wakeup_cb = h1_buf_available;
192 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
193 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
194 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
195 __conn_xprt_stop_recv(h1c->conn);
196 }
197 return buf;
198}
199
200/*
201 * Release a buffer, if any, and try to wake up entities waiting in the buffer
202 * wait queue.
203 */
204static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
205{
206 if (bptr->size) {
207 b_free(bptr);
208 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
209 }
210}
211
Willy Tarreau00f18a32019-01-26 12:19:01 +0100212/* returns the number of streams in use on a connection to figure if it's
213 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
214 * as the caller will want to know if it was the last one after a detach().
215 */
216static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200217{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100218 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200219
Willy Tarreau00f18a32019-01-26 12:19:01 +0100220 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200221}
222
Willy Tarreau00f18a32019-01-26 12:19:01 +0100223/* returns the number of streams still available on a connection */
224static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100225{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100226 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100227}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200228
Willy Tarreau00f18a32019-01-26 12:19:01 +0100229
Christopher Faulet51dbc942018-09-13 09:05:15 +0200230/*****************************************************************/
231/* functions below are dedicated to the mux setup and management */
232/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200233
234/* returns non-zero if there are input data pending for stream h1s. */
235static inline size_t h1s_data_pending(const struct h1s *h1s)
236{
237 const struct h1m *h1m;
238
239 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
240 if (h1m->state == H1_MSG_DONE)
241 return 0; // data not for this stream (e.g. pipelining)
242
243 return b_data(&h1s->h1c->ibuf);
244}
245
Christopher Faulet47365272018-10-31 17:40:50 +0100246static struct conn_stream *h1s_new_cs(struct h1s *h1s)
247{
248 struct conn_stream *cs;
249
250 cs = cs_new(h1s->h1c->conn);
251 if (!cs)
252 goto err;
253 h1s->cs = cs;
254 cs->ctx = h1s;
255
256 if (h1s->flags & H1S_F_NOT_FIRST)
257 cs->flags |= CS_FL_NOT_FIRST;
258
259 if (stream_create_from_cs(cs) < 0)
260 goto err;
261 return cs;
262
263 err:
264 cs_free(cs);
265 h1s->cs = NULL;
266 return NULL;
267}
268
Olivier Houchardf502aca2018-12-14 19:42:40 +0100269static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200270{
271 struct h1s *h1s;
272
273 h1s = pool_alloc(pool_head_h1s);
274 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100275 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200276
277 h1s->h1c = h1c;
278 h1c->h1s = h1s;
279
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280 h1s->sess = sess;
281
Christopher Faulet51dbc942018-09-13 09:05:15 +0200282 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100283 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200284
285 h1s->recv_wait = NULL;
286 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200287
288 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100289 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200290
Christopher Faulet129817b2018-09-20 16:14:40 +0200291 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100292 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200293
294 h1s->status = 0;
295 h1s->meth = HTTP_METH_OTHER;
296
Christopher Faulet47365272018-10-31 17:40:50 +0100297 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
298 h1s->flags |= H1S_F_NOT_FIRST;
299 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
300
Christopher Faulet129817b2018-09-20 16:14:40 +0200301 if (!conn_is_back(h1c->conn)) {
302 if (h1c->px->options2 & PR_O2_REQBUG_OK)
303 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200304
305 /* For frontend connections we should always have a session */
306 if (!sess)
307 sess = h1c->conn->owner;
308
309 h1s->csinfo.create_date = sess->accept_date;
310 h1s->csinfo.tv_create = sess->tv_accept;
311 h1s->csinfo.t_handshake = sess->t_handshake;
312 h1s->csinfo.t_idle = -1;
Christopher Faulet129817b2018-09-20 16:14:40 +0200313 }
314 else {
315 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
316 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200317
Christopher Fauletfeb11742018-11-29 15:12:34 +0100318 h1s->csinfo.create_date = date;
319 h1s->csinfo.tv_create = now;
320 h1s->csinfo.t_handshake = 0;
321 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200322 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100323
Christopher Faulete9b70722019-04-08 10:46:02 +0200324 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
325 * create a new one.
326 */
327 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200328 cs->ctx = h1s;
329 h1s->cs = cs;
330 }
Christopher Faulet47365272018-10-31 17:40:50 +0100331 else {
332 cs = h1s_new_cs(h1s);
333 if (!cs)
334 goto fail;
335 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200336 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100337
338 fail:
339 pool_free(pool_head_h1s, h1s);
340 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200341}
342
343static void h1s_destroy(struct h1s *h1s)
344{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200345 if (h1s) {
346 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347
Christopher Fauletf2824e62018-10-01 12:12:37 +0200348 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200349
Christopher Fauletf2824e62018-10-01 12:12:37 +0200350 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100351 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200352 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100353 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200354
Christopher Fauletcb55f482018-12-10 11:56:47 +0100355 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100356 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
357 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
358 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200359 pool_free(pool_head_h1s, h1s);
360 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200361}
362
Christopher Fauletfeb11742018-11-29 15:12:34 +0100363static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
364{
365 struct h1s *h1s = cs->ctx;
366
367 if (h1s && !conn_is_back(cs->conn))
368 return &h1s->csinfo;
369 return NULL;
370}
371
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200373 * Initialize the mux once it's attached. It is expected that conn->ctx points
374 * to the existing conn_stream (for outgoing connections or for incoming onces
375 * during a mux upgrade) or NULL (for incoming ones during the connexion
376 * establishment). <input> is always used as Input buffer and may contain
377 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
378 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200379 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200380static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
381 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200382{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200383 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100384 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200385
386 h1c = pool_alloc(pool_head_h1c);
387 if (!h1c)
388 goto fail_h1c;
389 h1c->conn = conn;
390 h1c->px = proxy;
391
392 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200393 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200394 h1c->obuf = BUF_NULL;
395 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200396 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200397
Christopher Faulet51dbc942018-09-13 09:05:15 +0200398 LIST_INIT(&h1c->buf_wait.list);
399 h1c->wait_event.task = tasklet_new();
400 if (!h1c->wait_event.task)
401 goto fail;
402 h1c->wait_event.task->process = h1_io_cb;
403 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100404 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200405
Christopher Faulete9b70722019-04-08 10:46:02 +0200406 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100407 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
408 if (tick_isset(proxy->timeout.serverfin))
409 h1c->shut_timeout = proxy->timeout.serverfin;
410 } else {
411 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
412 if (tick_isset(proxy->timeout.clientfin))
413 h1c->shut_timeout = proxy->timeout.clientfin;
414 }
415 if (tick_isset(h1c->timeout)) {
416 t = task_new(tid_bit);
417 if (!t)
418 goto fail;
419
420 h1c->task = t;
421 t->process = h1_timeout_task;
422 t->context = h1c;
423 t->expire = tick_add(now_ms, h1c->timeout);
424 }
425
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200426 if (!(conn->flags & CO_FL_CONNECTED))
427 h1c->flags |= H1C_F_CS_WAIT_CONN;
428
Christopher Fauletf2824e62018-10-01 12:12:37 +0200429 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100430 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200431 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200432
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100433 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200434
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100435
436 if (t)
437 task_queue(t);
438
Christopher Faulet51dbc942018-09-13 09:05:15 +0200439 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100440 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200441
442 /* mux->wake will be called soon to complete the operation */
443 return 0;
444
445 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200446 task_destroy(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100447 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200448 tasklet_free(h1c->wait_event.task);
449 pool_free(pool_head_h1c, h1c);
450 fail_h1c:
451 return -1;
452}
453
Christopher Faulet73c12072019-04-08 11:23:22 +0200454/* release function. This one should be called to free all resources allocated
455 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200457static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200458{
Christopher Faulet61840e72019-04-15 09:33:32 +0200459 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200460
Christopher Faulet51dbc942018-09-13 09:05:15 +0200461 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200462 /* The connection must be aattached to this mux to be released */
463 if (h1c->conn && h1c->conn->ctx == h1c)
464 conn = h1c->conn;
465
466 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200467 h1c->flags &= ~H1C_F_UPG_H2C;
468 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
469 /* connection successfully upgraded to H2, this
470 * mux was already released */
471 return;
472 }
473 sess_log(conn->owner); /* Log if the upgrade failed */
474 }
475
Christopher Faulet61840e72019-04-15 09:33:32 +0200476
Christopher Faulet51dbc942018-09-13 09:05:15 +0200477 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
478 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
479 LIST_DEL(&h1c->buf_wait.list);
480 LIST_INIT(&h1c->buf_wait.list);
481 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
482 }
483
484 h1_release_buf(h1c, &h1c->ibuf);
485 h1_release_buf(h1c, &h1c->obuf);
486
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100487 if (h1c->task) {
488 h1c->task->context = NULL;
489 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
490 h1c->task = NULL;
491 }
492
Christopher Faulet51dbc942018-09-13 09:05:15 +0200493 if (h1c->wait_event.task)
494 tasklet_free(h1c->wait_event.task);
495
Christopher Fauletf2824e62018-10-01 12:12:37 +0200496 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200497 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100498 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200499 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200500 pool_free(pool_head_h1c, h1c);
501 }
502
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200503 if (conn) {
504 conn->mux = NULL;
505 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200506
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200507 conn_stop_tracking(conn);
508 conn_full_close(conn);
509 if (conn->destroy_cb)
510 conn->destroy_cb(conn);
511 conn_free(conn);
512 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200513}
514
515/******************************************************/
516/* functions below are for the H1 protocol processing */
517/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200518/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
519 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200520 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100521static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200522{
Christopher Faulet570d1612018-11-26 11:13:57 +0100523 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200524
Christopher Faulet570d1612018-11-26 11:13:57 +0100525 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200526 (*(p + 5) > '1' ||
527 (*(p + 5) == '1' && *(p + 7) >= '1')))
528 h1m->flags |= H1_MF_VER_11;
529}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200530
Christopher Faulet9768c262018-10-22 09:34:31 +0200531/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
532 * greater or equal to 1.1
533 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100534static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200535{
Christopher Faulet570d1612018-11-26 11:13:57 +0100536 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200537
Christopher Faulet570d1612018-11-26 11:13:57 +0100538 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200539 (*(p + 5) > '1' ||
540 (*(p + 5) == '1' && *(p + 7) >= '1')))
541 h1m->flags |= H1_MF_VER_11;
542}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200543
Christopher Faulet9768c262018-10-22 09:34:31 +0200544/*
545 * Check the validity of the request version. If the version is valid, it
546 * returns 1. Otherwise, it returns 0.
547 */
548static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
549{
550 struct h1c *h1c = h1s->h1c;
551
552 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
553 * exactly one digit "." one digit. This check may be disabled using
554 * option accept-invalid-http-request.
555 */
556 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
557 if (sl.rq.v.len != 8)
558 return 0;
559
560 if (*(sl.rq.v.ptr + 4) != '/' ||
561 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
562 *(sl.rq.v.ptr + 6) != '.' ||
563 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
564 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200565 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200566 else if (!sl.rq.v.len) {
567 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200568
Christopher Faulet9768c262018-10-22 09:34:31 +0200569 /* RFC 1945 allows only GET for HTTP/0.9 requests */
570 if (sl.rq.meth != HTTP_METH_GET)
571 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200572
Christopher Faulet9768c262018-10-22 09:34:31 +0200573 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
574 if (!sl.rq.u.len)
575 return 0;
576
577 /* Add HTTP version */
578 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100579 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200580 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100581
582 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100583 ((*(sl.rq.v.ptr + 5) > '1') ||
584 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100585 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200586 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200587}
588
Christopher Faulet9768c262018-10-22 09:34:31 +0200589/*
590 * Check the validity of the response version. If the version is valid, it
591 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200592 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200593static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200594{
Christopher Faulet9768c262018-10-22 09:34:31 +0200595 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596
Christopher Faulet9768c262018-10-22 09:34:31 +0200597 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
598 * exactly one digit "." one digit. This check may be disabled using
599 * option accept-invalid-http-request.
600 */
601 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
602 if (sl.st.v.len != 8)
603 return 0;
604
605 if (*(sl.st.v.ptr + 4) != '/' ||
606 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
607 *(sl.st.v.ptr + 6) != '.' ||
608 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
609 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200610 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100611
612 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100613 ((*(sl.st.v.ptr + 5) > '1') ||
614 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100615 h1m->flags |= H1_MF_VER_11;
616
Christopher Faulet9768c262018-10-22 09:34:31 +0200617 return 1;
618}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200619
620/* Deduce the connection mode of the client connection, depending on the
621 * configuration and the H1 message flags. This function is called twice, the
622 * first time when the request is parsed and the second time when the response
623 * is parsed.
624 */
625static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
626{
627 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200628
629 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100630 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200631 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100632 h1s->status == 101) {
633 /* Either we've established an explicit tunnel, or we're
634 * switching the protocol. In both cases, we're very unlikely to
635 * understand the next protocols. We have to switch to tunnel
636 * mode, so that we transfer the request and responses then let
637 * this protocol pass unmodified. When we later implement
638 * specific parsers for such protocols, we'll want to check the
639 * Upgrade header which contains information about that protocol
640 * for responses with status 101 (eg: see RFC2817 about TLS).
641 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200642 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100643 }
644 else if (h1s->flags & H1S_F_WANT_KAL) {
645 /* By default the client is in KAL mode. CLOSE mode mean
646 * it is imposed by the client itself. So only change
647 * KAL mode here. */
648 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
649 /* no length known or explicit close => close */
650 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
651 }
652 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
653 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
654 /* no explict keep-alive and option httpclose => close */
655 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
656 }
657 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200658 }
659 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100660 /* Input direction: first pass */
661 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
662 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200663 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100664 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200665 }
666
667 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
668 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
669 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
670}
671
672/* Deduce the connection mode of the client connection, depending on the
673 * configuration and the H1 message flags. This function is called twice, the
674 * first time when the request is parsed and the second time when the response
675 * is parsed.
676 */
677static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
678{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100679 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100680 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100681 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200682
Christopher Fauletf2824e62018-10-01 12:12:37 +0200683 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100684 /* Input direction: second pass */
685
Christopher Fauletf2824e62018-10-01 12:12:37 +0200686 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100687 h1s->status == 101) {
688 /* Either we've established an explicit tunnel, or we're
689 * switching the protocol. In both cases, we're very unlikely to
690 * understand the next protocols. We have to switch to tunnel
691 * mode, so that we transfer the request and responses then let
692 * this protocol pass unmodified. When we later implement
693 * specific parsers for such protocols, we'll want to check the
694 * Upgrade header which contains information about that protocol
695 * for responses with status 101 (eg: see RFC2817 about TLS).
696 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200697 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100698 }
699 else if (h1s->flags & H1S_F_WANT_KAL) {
700 /* By default the server is in KAL mode. CLOSE mode mean
701 * it is imposed by haproxy itself. So only change KAL
702 * mode here. */
703 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
704 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
705 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
706 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
707 }
708 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200709 }
Christopher Faulet70033782018-12-05 13:50:11 +0100710 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100711 /* Output direction: first pass */
712 if (h1m->flags & H1_MF_CONN_CLO) {
713 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100714 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100715 }
716 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
717 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
718 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
719 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
720 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
721 /* no explicit keep-alive option httpclose/server-close => close */
722 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
723 }
Christopher Faulet70033782018-12-05 13:50:11 +0100724 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200725
726 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
727 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
728 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200729}
730
Christopher Fauletb992af02019-03-28 15:42:24 +0100731static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200732{
733 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200734
735 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
736 * token is found
737 */
738 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200739 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200740
741 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100742 if (!(h1m->flags & H1_MF_VER_11))
743 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200744 }
745 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100746 if (h1m->flags & H1_MF_VER_11)
747 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200748 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200749}
750
Christopher Fauletb992af02019-03-28 15:42:24 +0100751static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200752{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200753 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
754 * token is found
755 */
756 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200757 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200758
759 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100760 if (!(h1m->flags & H1_MF_VER_11) ||
761 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
762 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200763 }
764 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100765 if (h1m->flags & H1_MF_VER_11)
766 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200767 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200768}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200769
Christopher Fauletb992af02019-03-28 15:42:24 +0100770static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200771{
Christopher Fauletb992af02019-03-28 15:42:24 +0100772 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200773 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100774 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200775 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200776}
777
Christopher Fauletb992af02019-03-28 15:42:24 +0100778static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
779{
780 if (!conn_is_back(h1s->h1c->conn))
781 h1_set_cli_conn_mode(h1s, h1m);
782 else
783 h1_set_srv_conn_mode(h1s, h1m);
784
785 if (!(h1m->flags & H1_MF_RESP))
786 h1_update_req_conn_value(h1s, h1m, conn_val);
787 else
788 h1_update_res_conn_value(h1s, h1m, conn_val);
789}
Christopher Faulete44769b2018-11-29 23:01:45 +0100790
791/* Append the description of what is present in error snapshot <es> into <out>.
792 * The description must be small enough to always fit in a buffer. The output
793 * buffer may be the trash so the trash must not be used inside this function.
794 */
795static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
796{
797 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100798 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
799 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
800 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
801 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
802 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
803 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100804}
805/*
806 * Capture a bad request or response and archive it in the proxy's structure.
807 * By default it tries to report the error position as h1m->err_pos. However if
808 * this one is not set, it will then report h1m->next, which is the last known
809 * parsing point. The function is able to deal with wrapping buffers. It always
810 * displays buffers as a contiguous area starting at buf->p. The direction is
811 * determined thanks to the h1m's flags.
812 */
813static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
814 struct h1m *h1m, struct buffer *buf)
815{
816 struct session *sess = h1c->conn->owner;
817 struct proxy *proxy = h1c->px;
818 struct proxy *other_end = sess->fe;
819 union error_snapshot_ctx ctx;
820
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100821 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100822 other_end = si_strm(h1s->cs->data)->be;
823
824 /* http-specific part now */
825 ctx.h1.state = h1m->state;
826 ctx.h1.c_flags = h1c->flags;
827 ctx.h1.s_flags = h1s->flags;
828 ctx.h1.m_flags = h1m->flags;
829 ctx.h1.m_clen = h1m->curr_len;
830 ctx.h1.m_blen = h1m->body_len;
831
832 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
833 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100834 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
835 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100836}
837
Christopher Fauletadb22202018-12-12 10:32:09 +0100838/* Emit the chunksize followed by a CRLF in front of data of the buffer
839 * <buf>. It goes backwards and starts with the byte before the buffer's
840 * head. The caller is responsible for ensuring there is enough room left before
841 * the buffer's head for the string.
842 */
843static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
844{
845 char *beg, *end;
846
847 beg = end = b_head(buf);
848 *--beg = '\n';
849 *--beg = '\r';
850 do {
851 *--beg = hextab[chksz & 0xF];
852 } while (chksz >>= 4);
853 buf->head -= (end - beg);
854 b_add(buf, end - beg);
855}
856
857/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
858 * ensuring there is enough room left in the buffer for the string. */
859static void h1_emit_chunk_crlf(struct buffer *buf)
860{
861 *(b_peek(buf, b_data(buf))) = '\r';
862 *(b_peek(buf, b_data(buf) + 1)) = '\n';
863 b_add(buf, 2);
864}
865
Christopher Faulet129817b2018-09-20 16:14:40 +0200866/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100867 * Switch the request to tunnel mode. This function must only be called for
868 * CONNECT requests. On the client side, the mux is mark as busy on input,
869 * waiting the response.
870 */
871static void h1_set_req_tunnel_mode(struct h1s *h1s)
872{
873 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
874 h1s->req.state = H1_MSG_TUNNEL;
875 if (!conn_is_back(h1s->h1c->conn))
876 h1s->h1c->flags |= H1C_F_IN_BUSY;
877}
878
879/*
880 * Switch the response to tunnel mode. This function must only be called on
881 * successfull replies to CONNECT requests or on protocol switching. On the
882 * server side, if the request is not finished, the mux is mark as busy on
883 * input. Otherwise the request is also switch to tunnel mode.
884 */
885static void h1_set_res_tunnel_mode(struct h1s *h1s)
886{
887 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
888 h1s->res.state = H1_MSG_TUNNEL;
889 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
890 h1s->h1c->flags |= H1C_F_IN_BUSY;
891 else {
892 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
893 h1s->req.state = H1_MSG_TUNNEL;
894 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
895 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
896 tasklet_wakeup(h1s->h1c->wait_event.task);
897 }
898 }
899}
900
Christopher Faulet82f01602019-05-24 16:50:16 +0200901/* Estimate the size of the HTX headers after the parsing, including the EOH. */
902static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
903{
904 size_t sz = 0;
905 int i;
906
907 for (i = 0; hdrs[i].n.len; i++)
908 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
909 sz += sizeof(struct htx_blk) + 1;
910 return sz;
911}
912
Christopher Faulet30db3d72019-05-17 15:35:33 +0200913/* Estimate the size of the HTX request after the parsing. */
914static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
915{
916 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200917
918 /* size of the HTX start-line */
919 sz = sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200920 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200921 return sz;
922}
923
924/* Estimate the size of the HTX response after the parsing. */
925static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
926{
927 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200928
929 /* size of the HTX start-line */
930 sz = sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200931 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200932 return sz;
933}
934
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100935/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200936 * Add the EOM in the HTX message and switch the message to the DONE state. It
937 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
938 * functions is responsible to update the parser state <h1m>. It also add the
939 * flag CS_FL_EOI on the CS.
940 */
941static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
942{
943 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
944 return 0;
945
946 h1m->state = H1_MSG_DONE;
947 h1s->cs->flags |= CS_FL_EOI;
948 return (sizeof(struct htx_blk) + 1);
949}
950
951/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200952 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200953 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
954 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800955 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200956 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200957static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200958 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200959{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200960 struct htx_sl *sl;
Christopher Faulet129817b2018-09-20 16:14:40 +0200961 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100962 union h1_sl h1sl;
963 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200964 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +0200965 int ret = 0;
966
Christopher Faulet30db3d72019-05-17 15:35:33 +0200967 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100968 goto end;
969
Christopher Faulet129817b2018-09-20 16:14:40 +0200970 /* Realing input buffer if necessary */
971 if (b_head(buf) + b_data(buf) > b_wrap(buf))
972 b_slow_realign(buf, trash.area, 0);
973
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200974 if (!(h1m->flags & H1_MF_RESP)) {
975 /* Try to match H2 preface before parsing the request headers. */
976 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
977 if (ret > 0)
978 goto h2c_upgrade;
979 }
980
Christopher Faulet30db3d72019-05-17 15:35:33 +0200981 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100982 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200983 if (ret <= 0) {
984 /* Incomplete or invalid message. If the buffer is full, it's an
985 * error because headers are too large to be handled by the
986 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200987 if (ret < 0 || (!ret && b_full(buf)))
988 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200989 goto end;
990 }
991
992 /* messages headers fully parsed, do some checks to prepare the body
993 * parsing.
994 */
995
Christopher Faulet9768c262018-10-22 09:34:31 +0200996 /* Save the request's method or the response's status, check if the body
997 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200998 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100999 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001000
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001001 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001002 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001003
1004 if (h1s->meth == HTTP_METH_CONNECT) {
1005 /* Switch CONNECT requests to tunnel mode */
1006 h1_set_req_tunnel_mode(h1s);
1007 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001008
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001009 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1010 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001011 h1m->err_state = h1m->state;
1012 goto vsn_error;
1013 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001014 }
1015 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001016 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001017
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001018 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1019 h1s->status == 101) {
1020 /* Switch successfull replies to CONNECT requests and
1021 * protocol switching to tunnel mode. */
1022 h1_set_res_tunnel_mode(h1s);
1023 }
1024 else if ((h1s->meth == HTTP_METH_HEAD) ||
1025 (h1s->status >= 100 && h1s->status < 200) ||
1026 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001027 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001028 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1029 h1m->flags |= H1_MF_XFER_LEN;
1030 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001031 }
1032 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001033 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001034 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001035 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001036 else {
1037 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001038 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001039 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001040
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001041 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1042 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001043 h1m->err_state = h1m->state;
1044 goto vsn_error;
1045 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001046 }
1047
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001048 /* Set HTX start-line flags */
1049 if (h1m->flags & H1_MF_VER_11)
1050 flags |= HTX_SL_F_VER_11;
1051 if (h1m->flags & H1_MF_XFER_ENC)
1052 flags |= HTX_SL_F_XFER_ENC;
1053 if (h1m->flags & H1_MF_XFER_LEN) {
1054 flags |= HTX_SL_F_XFER_LEN;
1055 if (h1m->flags & H1_MF_CHNK)
1056 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001057 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001058 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001059 if (h1m->body_len == 0)
1060 flags |= HTX_SL_F_BODYLESS;
1061 }
1062 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001063 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001064 }
1065
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001066 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001067 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001068 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1069 if (htx_is_empty(htx))
1070 goto error;
1071 h1m_init_req(h1m);
1072 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1073 ret = 0;
1074 goto end;
1075 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001076
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001077 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1078 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001079 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001080 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001081 }
1082 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001083 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1084 if (htx_is_empty(htx))
1085 goto error;
1086 h1m_init_res(h1m);
1087 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1088 ret = 0;
1089 goto end;
1090 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001091
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001092 flags |= HTX_SL_F_IS_RESP;
1093 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1094 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001095 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001096 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001097 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001098
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001099 /* Set bytes used in the HTX mesage for the headers now */
1100 sl->hdrs_bytes = htx_used_space(htx) - used;
1101
Christopher Fauletb992af02019-03-28 15:42:24 +01001102 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001103
1104 /* If body length cannot be determined, set htx->extra to
1105 * ULLONG_MAX. This value is impossible in other cases.
1106 */
1107 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001108 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001109 end:
1110 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001111
1112 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113 h1m->err_state = h1m->state;
1114 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001115 vsn_error:
1116 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001117 h1s->cs->flags |= CS_FL_EOI;
1118 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001119 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001120 ret = 0;
1121 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001122
1123 h2c_upgrade:
1124 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001125 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001126 htx->flags |= HTX_FL_UPGRADE;
1127 ret = 0;
1128 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001129}
1130
1131/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001132 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1133 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001134 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001135 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001136 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001137static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001138 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001139 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001140{
1141 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001142 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001143
Christopher Faulet129817b2018-09-20 16:14:40 +02001144 if (h1m->flags & H1_MF_XFER_LEN) {
1145 if (h1m->flags & H1_MF_CLEN) {
1146 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001147 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001148 if ((uint64_t)ret > h1m->curr_len)
1149 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 if (ret > b_contig_data(buf, *ofs))
1151 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001152
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001154 /* very often with large files we'll face the following
1155 * situation :
1156 * - htx is empty and points to <htxbuf>
1157 * - ret == buf->data
1158 * - buf->head == sizeof(struct htx)
1159 * => we can swap the buffers and place an htx header into
1160 * the target buffer instead
1161 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001162 int32_t try = ret;
1163
Willy Tarreau78f548f2018-12-05 10:02:39 +01001164 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1165 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1166 void *raw_area = buf->area;
1167 void *htx_area = htxbuf->area;
1168 struct htx_blk *blk;
1169
1170 buf->area = htx_area;
1171 htxbuf->area = raw_area;
1172 htx = (struct htx *)htxbuf->area;
1173 htx->size = htxbuf->size - sizeof(*htx);
1174 htx_reset(htx);
1175 b_set_data(htxbuf, b_size(htxbuf));
1176
1177 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1178 blk->info += ret;
1179 /* nothing else to do, the old buffer now contains an
1180 * empty pre-initialized HTX header
1181 */
1182 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001183 else {
1184 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1185 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001186 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001187 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001188 *ofs += ret;
1189 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001190 if (ret < try)
1191 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001192 }
1193
1194 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001195 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001196 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001197 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001198 }
1199 else if (h1m->flags & H1_MF_CHNK) {
1200 new_chunk:
1201 /* te:chunked : parse chunks */
1202 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001203 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001204 if (ret <= 0)
1205 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001206 h1m->state = H1_MSG_CHUNK_SIZE;
1207
Christopher Fauletf2824e62018-10-01 12:12:37 +02001208 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001210 }
1211
1212 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1213 unsigned int chksz;
1214
Christopher Faulet30db3d72019-05-17 15:35:33 +02001215 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001216 if (ret <= 0)
1217 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001218 if (!chksz) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001219 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOD))
Christopher Faulet9768c262018-10-22 09:34:31 +02001220 goto end;
1221 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001222 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 }
1224 else
1225 h1m->state = H1_MSG_DATA;
1226
Christopher Faulet129817b2018-09-20 16:14:40 +02001227 h1m->curr_len = chksz;
1228 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001229 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001231
1232 if (!h1m->curr_len)
1233 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001234 }
1235
1236 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001237 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001238 if ((uint64_t)ret > h1m->curr_len)
1239 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 if (ret > b_contig_data(buf, *ofs))
1241 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001242
Christopher Faulet9768c262018-10-22 09:34:31 +02001243 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001244 int32_t try = ret;
1245
1246 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001247 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001248 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001249 *ofs += ret;
1250 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001251 if (ret < try)
1252 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001253 }
1254 if (!h1m->curr_len) {
1255 h1m->state = H1_MSG_CHUNK_CRLF;
1256 goto new_chunk;
1257 }
1258 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001259 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001260 }
1261 else {
1262 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1263 * is no body. Switch the message in DONE state
1264 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001265 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001266 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001267 }
1268 }
1269 else {
1270 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001271 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001272 if (ret > b_contig_data(buf, *ofs))
1273 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001274
Christopher Faulet9768c262018-10-22 09:34:31 +02001275 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001276 int32_t try = ret;
1277
1278 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001279
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001280 *ofs += ret;
1281 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001282 if (ret < try)
1283 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001284 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001285 }
1286
1287 end:
1288 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001289 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001290 h1s->cs->flags |= CS_FL_EOI;
1291 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001292 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001293 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001294 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 return 0;
1296 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001297 /* update htx->extra, only when the body length is known */
1298 if (h1m->flags & H1_MF_XFER_LEN)
1299 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 return total;
1301}
1302
1303/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001304 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1305 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1306 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1307 * responsible to update the parser state <h1m>.
1308 */
1309static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1310 struct buffer *buf, size_t *ofs, size_t max)
1311{
1312 struct http_hdr hdrs[MAX_HTTP_HDR];
1313 struct h1m tlr_h1m;
1314 int ret = 0;
1315
1316 if (!max || !b_data(buf))
1317 goto end;
1318
1319 /* Realing input buffer if necessary */
1320 if (b_peek(buf, *ofs) > b_tail(buf))
1321 b_slow_realign(buf, trash.area, 0);
1322
1323 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1324 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1325 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1326 if (ret <= 0) {
1327 /* Incomplete or invalid trailers. If the buffer is full, it's
1328 * an error because traliers are too large to be handled by the
1329 * parser. */
1330 if (ret < 0 || (!ret && !buf_room_for_htx_data(buf)))
1331 goto error;
1332 goto end;
1333 }
1334
1335 /* messages trailers fully parsed. */
1336 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1337 if (htx_is_empty(htx))
1338 goto error;
1339 ret = 0;
1340 goto end;
1341 }
1342
1343 if (!htx_add_all_trailers(htx, hdrs))
1344 goto error;
1345
1346 *ofs += ret;
1347 h1s->flags |= H1S_F_HAVE_I_TLR;
1348 end:
1349 return ret;
1350
1351 error:
1352 h1m->err_state = h1m->state;
1353 h1m->err_pos = h1m->next;
1354 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1355 h1s->cs->flags |= CS_FL_EOI;
1356 htx->flags |= HTX_FL_PARSING_ERROR;
1357 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1358 ret = 0;
1359 goto end;
1360}
1361
1362/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001363 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001364 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1365 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001366 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001367static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001368{
Christopher Faulet539e0292018-11-19 10:40:09 +01001369 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001370 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001371 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001372 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001373 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001374 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001375
Christopher Faulet539e0292018-11-19 10:40:09 +01001376 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001377
Christopher Fauletf2824e62018-10-01 12:12:37 +02001378 if (!conn_is_back(h1c->conn)) {
1379 h1m = &h1s->req;
1380 errflag = H1S_F_REQ_ERROR;
1381 }
1382 else {
1383 h1m = &h1s->res;
1384 errflag = H1S_F_RES_ERROR;
1385 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001386
Christopher Faulet74027762019-02-26 14:45:05 +01001387 data = htx->data;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001388 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001389 size_t used = htx_used_space(htx);
1390
Christopher Faulet129817b2018-09-20 16:14:40 +02001391 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001392 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001393 if (!ret)
1394 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001395 if ((h1m->flags & H1_MF_RESP) &&
1396 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1397 h1m_init_res(&h1s->res);
1398 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1399 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001400 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001401 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001402 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001403 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001404 if (!ret)
1405 break;
1406 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001407 else if (h1m->state == H1_MSG_TRAILERS) {
1408 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1409 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1410 if (!ret)
1411 break;
1412 }
1413 if (!h1_process_eom(h1s, h1m, htx, count))
1414 break;
1415 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001416 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001417 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1418 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001419 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001420 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001421 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001422 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001423 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001424 if (!ret)
1425 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001426 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001427 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001428 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001429 break;
1430 }
1431
Christopher Faulet30db3d72019-05-17 15:35:33 +02001432 count -= htx_used_space(htx) - used;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001433 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001434
Christopher Faulet47365272018-10-31 17:40:50 +01001435 if (h1s->flags & errflag)
1436 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001437
Christopher Faulet539e0292018-11-19 10:40:09 +01001438 b_del(&h1c->ibuf, total);
1439
1440 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001441 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001442 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001443 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001444 h1c->flags &= ~H1C_F_IN_FULL;
1445 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001446 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001447
Christopher Fauletcf56b992018-12-11 16:12:31 +01001448 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1449
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001450 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001451 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001452 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001453 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001454
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001455 if ((h1s->flags & H1S_F_REOS) && (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001456 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001457 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001458 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001459 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001460
Christopher Faulet30db3d72019-05-17 15:35:33 +02001461 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001462
1463 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001464 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001465 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001466 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001467}
1468
Christopher Faulet129817b2018-09-20 16:14:40 +02001469/*
1470 * Process outgoing data. It parses data and transfer them from the channel buffer into
1471 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1472 * 0 if it couldn't proceed.
1473 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001474static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1475{
Christopher Faulet129817b2018-09-20 16:14:40 +02001476 struct h1s *h1s = h1c->h1s;
1477 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 struct htx *chn_htx;
1479 struct htx_blk *blk;
1480 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001481 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001482 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001483
Christopher Faulet47365272018-10-31 17:40:50 +01001484 if (!count)
1485 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001486
Christopher Faulet94b2c762019-05-24 15:28:57 +02001487 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001488 if (htx_is_empty(chn_htx))
1489 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001490
Christopher Faulet51dbc942018-09-13 09:05:15 +02001491 if (!h1_get_buf(h1c, &h1c->obuf)) {
1492 h1c->flags |= H1C_F_OUT_ALLOC;
1493 goto end;
1494 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001495
Christopher Fauletf2824e62018-10-01 12:12:37 +02001496 if (!conn_is_back(h1c->conn)) {
1497 h1m = &h1s->res;
1498 errflag = H1S_F_RES_ERROR;
1499 }
1500 else {
1501 h1m = &h1s->req;
1502 errflag = H1S_F_REQ_ERROR;
1503 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001504
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001505 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001506 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001507
1508 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001509
Willy Tarreau3815b222018-12-11 19:50:43 +01001510 /* Perform some optimizations to reduce the number of buffer copies.
1511 * First, if the mux's buffer is empty and the htx area contains
1512 * exactly one data block of the same size as the requested count,
1513 * then it's possible to simply swap the caller's buffer with the
1514 * mux's output buffer and adjust offsets and length to match the
1515 * entire DATA HTX block in the middle. In this case we perform a
1516 * true zero-copy operation from end-to-end. This is the situation
1517 * that happens all the time with large files. Second, if this is not
1518 * possible, but the mux's output buffer is empty, we still have an
1519 * opportunity to avoid the copy to the intermediary buffer, by making
1520 * the intermediary buffer's area point to the output buffer's area.
1521 * In this case we want to skip the HTX header to make sure that copies
1522 * remain aligned and that this operation remains possible all the
1523 * time. This goes for headers, data blocks and any data extracted from
1524 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001525 */
1526 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001527 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001528
1529 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001530 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001531 htx_get_blk_value(chn_htx, blk).len == count) {
1532 void *old_area = h1c->obuf.area;
1533
1534 h1c->obuf.area = buf->area;
1535 h1c->obuf.data = count;
1536
1537 buf->area = old_area;
1538 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001539
1540 /* The message is chunked. We need to emit the chunk
1541 * size. We have at least the size of the struct htx to
1542 * write the chunk envelope. It should be enough.
1543 */
1544 if (h1m->flags & H1_MF_CHNK) {
1545 h1_emit_chunk_size(&h1c->obuf, count);
1546 h1_emit_chunk_crlf(&h1c->obuf);
1547 }
1548
Willy Tarreau3815b222018-12-11 19:50:43 +01001549 total += count;
1550 goto out;
1551 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001552 tmp->area = h1c->obuf.area + h1c->obuf.head;
1553 }
1554
Christopher Faulet9768c262018-10-22 09:34:31 +02001555 tmp->size = b_room(&h1c->obuf);
1556
Christopher Fauletb2e84162018-12-06 11:39:49 +01001557 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001558 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001559 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001560 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001562 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001563
Christopher Fauletb2e84162018-12-06 11:39:49 +01001564 vlen = sz;
1565 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001566 if (type != HTX_BLK_DATA)
Christopher Fauletb2e84162018-12-06 11:39:49 +01001567 goto copy;
1568 vlen = count;
1569 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001570
Christopher Faulet94b2c762019-05-24 15:28:57 +02001571 if (type == HTX_BLK_UNUSED)
1572 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001573
Christopher Faulet94b2c762019-05-24 15:28:57 +02001574 switch (h1m->state) {
1575 case H1_MSG_RQBEFORE:
1576 if (type != HTX_BLK_REQ_SL)
1577 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001578 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001579 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001580 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001581 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001582 goto copy;
1583 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001584 if (sl->flags & HTX_SL_F_BODYLESS)
1585 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001586 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001587 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001588
Christopher Faulet94b2c762019-05-24 15:28:57 +02001589 case H1_MSG_RPBEFORE:
1590 if (type != HTX_BLK_RES_SL)
1591 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001592 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001593 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001594 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001595 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001596 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001597 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001598 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001599 if (sl->info.res.status < 200 &&
1600 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001601 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001602 h1m->state = H1_MSG_HDR_FIRST;
1603 break;
1604
Christopher Faulet94b2c762019-05-24 15:28:57 +02001605 case H1_MSG_HDR_FIRST:
1606 case H1_MSG_HDR_NAME:
1607 case H1_MSG_HDR_L2_LWS:
1608 if (type == HTX_BLK_EOH)
1609 goto last_lf;
1610 if (type != HTX_BLK_HDR)
1611 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001612 h1m->state = H1_MSG_HDR_NAME;
1613 n = htx_get_blk_name(chn_htx, blk);
1614 v = htx_get_blk_value(chn_htx, blk);
1615
1616 if (isteqi(n, ist("transfer-encoding")))
1617 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001618 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001619 /* Only skip C-L header with invalid value. */
1620 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001621 goto skip_hdr;
1622 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001623 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001624 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001625 if (!v.len)
1626 goto skip_hdr;
1627 }
1628
Christopher Fauletc59ff232018-12-03 13:58:44 +01001629 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001630 goto copy;
1631 skip_hdr:
1632 h1m->state = H1_MSG_HDR_L2_LWS;
1633 break;
1634
Christopher Faulet94b2c762019-05-24 15:28:57 +02001635 case H1_MSG_LAST_LF:
1636 if (type != HTX_BLK_EOH)
1637 goto error;
1638 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001639 h1m->state = H1_MSG_LAST_LF;
1640 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001641 /* There is no "Connection:" header and
1642 * it the conn_mode must be
1643 * processed. So do it */
1644 n = ist("Connection");
1645 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001646 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001647 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001648 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001649 goto copy;
1650 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001651 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001652 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001653
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001654 if ((h1s->meth != HTTP_METH_CONNECT &&
1655 (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 +01001656 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1657 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1658 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1659 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1660 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001661 /* chunking needed but header not seen */
1662 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1663 goto copy;
1664 h1m->flags |= H1_MF_CHNK;
1665 }
1666
Christopher Faulet9768c262018-10-22 09:34:31 +02001667 if (!chunk_memcat(tmp, "\r\n", 2))
1668 goto copy;
1669
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001670 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1671 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1672 h1_set_req_tunnel_mode(h1s);
1673 }
1674 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1675 /* a successfull reply to a CONNECT or a protocol switching is sent
1676 * to the client . Switch the response to tunnel mode. */
1677 h1_set_res_tunnel_mode(h1s);
1678 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001679 else if ((h1m->flags & H1_MF_RESP) &&
1680 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1681 h1m_init_res(&h1s->res);
1682 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001683 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001684 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001685 else
1686 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 break;
1688
Christopher Faulet94b2c762019-05-24 15:28:57 +02001689 case H1_MSG_DATA:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001690 if (type == HTX_BLK_EOM) {
1691 /* Chunked message without explicit trailers */
1692 if (h1m->flags & H1_MF_CHNK) {
1693 if (!chunk_memcat(tmp, "0\r\n\r\n", 5))
1694 goto copy;
1695 }
1696 goto done;
1697 }
1698 else if (type == HTX_BLK_EOD)
1699 break;
1700 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet94b2c762019-05-24 15:28:57 +02001701 if (!chunk_memcat(tmp, "0\r\n", 3))
1702 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001703 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001704 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001705 else if (type != HTX_BLK_DATA)
1706 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001707 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001708 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001709 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001710 goto copy;
1711 break;
1712
Christopher Faulet94b2c762019-05-24 15:28:57 +02001713 case H1_MSG_TRAILERS:
1714 if (type == HTX_BLK_EOM)
1715 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001716 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001717 goto error;
1718 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001719 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001720 if (type == HTX_BLK_EOT) {
1721 if (!chunk_memcat(tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001722 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001723 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001724 else { // HTX_BLK_TLR
1725 n = htx_get_blk_name(chn_htx, blk);
1726 v = htx_get_blk_value(chn_htx, blk);
1727 if (!htx_hdr_to_h1(n, v, tmp))
1728 goto copy;
1729 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001730 break;
1731
Christopher Faulet94b2c762019-05-24 15:28:57 +02001732 case H1_MSG_DONE:
1733 if (type != HTX_BLK_EOM)
1734 goto error;
1735 done:
1736 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001737 break;
1738
Christopher Faulet9768c262018-10-22 09:34:31 +02001739 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001740 error:
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001741 h1s->flags |= errflag;
Christopher Faulet9768c262018-10-22 09:34:31 +02001742 break;
1743 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001744
1745 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001746 total += vlen;
1747 count -= vlen;
1748 if (sz == vlen)
1749 blk = htx_remove_blk(chn_htx, blk);
1750 else {
1751 htx_cut_data_blk(chn_htx, blk, vlen);
1752 break;
1753 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001754 }
1755
Christopher Faulet9768c262018-10-22 09:34:31 +02001756 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001757 /* when the output buffer is empty, tmp shares the same area so that we
1758 * only have to update pointers and lengths.
1759 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001760 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001761 h1c->obuf.data = tmp->data;
1762 else
1763 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001764
Willy Tarreau3815b222018-12-11 19:50:43 +01001765 htx_to_buf(chn_htx, buf);
1766 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001767 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001768 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001769 end:
1770 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001771}
1772
Christopher Faulet51dbc942018-09-13 09:05:15 +02001773/*********************************************************/
1774/* functions below are I/O callbacks from the connection */
1775/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001776static void h1_wake_stream_for_recv(struct h1s *h1s)
1777{
1778 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001779 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001780 tasklet_wakeup(h1s->recv_wait->task);
1781 h1s->recv_wait = NULL;
1782 }
1783}
1784static void h1_wake_stream_for_send(struct h1s *h1s)
1785{
1786 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001787 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001788 tasklet_wakeup(h1s->send_wait->task);
1789 h1s->send_wait = NULL;
1790 }
1791}
1792
Christopher Faulet51dbc942018-09-13 09:05:15 +02001793/*
1794 * Attempt to read data, and subscribe if none available
1795 */
1796static int h1_recv(struct h1c *h1c)
1797{
1798 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001799 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001800 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001801 int rcvd = 0;
1802
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001803 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001804 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001805
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001806 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1807 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1808 return 0;
1809 }
1810
Olivier Houchard75159a92018-12-03 18:46:09 +01001811 if (!h1_recv_allowed(h1c)) {
1812 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001813 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001814 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001815
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001816 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1817 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001818 goto end;
1819 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001820
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001821 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001822 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001823 h1_wake_stream_for_recv(h1s);
1824 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001825 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001826 }
1827
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001828 /*
1829 * If we only have a small amount of data, realign it,
1830 * it's probably cheaper than doing 2 recv() calls.
1831 */
1832 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1833 b_slow_realign(&h1c->ibuf, trash.area, 0);
1834
Willy Tarreau45f2b892018-12-05 07:59:27 +01001835 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001836 if (max) {
1837 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001838
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001839 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001840 if (!b_data(&h1c->ibuf)) {
1841 /* try to pre-align the buffer like the rxbufs will be
1842 * to optimize memory copies.
1843 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001844 h1c->ibuf.head = sizeof(struct htx);
1845 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001846 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001847 }
Christopher Faulet47365272018-10-31 17:40:50 +01001848 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001849 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001850 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001851 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001852 if (h1s->csinfo.t_idle == -1)
1853 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1854 }
Christopher Faulet47365272018-10-31 17:40:50 +01001855 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001856
Christopher Fauletcf56b992018-12-11 16:12:31 +01001857 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001858 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001859 goto end;
1860 }
1861
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001862 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001863
Christopher Faulet9768c262018-10-22 09:34:31 +02001864 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001865 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1866 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001867
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001868 if (conn_xprt_read0_pending(conn) && h1s) {
1869 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001870 rcvd = 1;
1871 }
1872
Christopher Faulet51dbc942018-09-13 09:05:15 +02001873 if (!b_data(&h1c->ibuf))
1874 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001875 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001876 h1c->flags |= H1C_F_IN_FULL;
1877 return rcvd;
1878}
1879
1880
1881/*
1882 * Try to send data if possible
1883 */
1884static int h1_send(struct h1c *h1c)
1885{
1886 struct connection *conn = h1c->conn;
1887 unsigned int flags = 0;
1888 size_t ret;
1889 int sent = 0;
1890
1891 if (conn->flags & CO_FL_ERROR)
1892 return 0;
1893
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001894 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001895 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001896 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001897 return 0;
1898 }
1899
Christopher Faulet51dbc942018-09-13 09:05:15 +02001900 if (!b_data(&h1c->obuf))
1901 goto end;
1902
1903 if (h1c->flags & H1C_F_OUT_FULL)
1904 flags |= CO_SFL_MSG_MORE;
1905
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001906 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001907 if (ret > 0) {
1908 h1c->flags &= ~H1C_F_OUT_FULL;
1909 b_del(&h1c->obuf, ret);
1910 sent = 1;
1911 }
1912
Christopher Faulet145aa472018-12-06 10:56:20 +01001913 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1914 /* error or output closed, nothing to send, clear the buffer to release it */
1915 b_reset(&h1c->obuf);
1916 }
1917
Christopher Faulet51dbc942018-09-13 09:05:15 +02001918 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001919 if (!(h1c->flags & H1C_F_OUT_FULL))
1920 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001921
Christopher Faulet51dbc942018-09-13 09:05:15 +02001922 /* We're done, no more to send */
1923 if (!b_data(&h1c->obuf)) {
1924 h1_release_buf(h1c, &h1c->obuf);
1925 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001926 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001927 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001928 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001929 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001930
1931 return sent;
1932}
1933
Christopher Faulet51dbc942018-09-13 09:05:15 +02001934
1935/* callback called on any event by the connection handler.
1936 * It applies changes and returns zero, or < 0 if it wants immediate
1937 * destruction of the connection.
1938 */
1939static int h1_process(struct h1c * h1c)
1940{
1941 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001942 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001943
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001944 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001945 return -1;
1946
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001947 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001948 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1949 goto end;
1950 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001951 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001952 }
1953
Christopher Fauletfeb11742018-11-29 15:12:34 +01001954 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001955 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001956 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001957 conn_xprt_read0_pending(conn))
1958 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001959 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001960 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001961 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001962 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001963 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001964 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001965 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001966 }
1967
Christopher Fauletfeb11742018-11-29 15:12:34 +01001968 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1969 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1970
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001971 if (conn_xprt_read0_pending(conn))
1972 h1s->flags |= H1S_F_REOS;
1973
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001974 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001975 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001976 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001977 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001978 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01001979 h1s->cs->data_cb->wake(h1s->cs);
1980 }
Christopher Faulet47365272018-10-31 17:40:50 +01001981 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001982 if (h1c->task) {
1983 h1c->task->expire = TICK_ETERNITY;
1984 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001985 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 +01001986 ? h1c->shut_timeout
1987 : h1c->timeout));
1988 task_queue(h1c->task);
1989 }
1990 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001991 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001992
1993 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02001994 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01001995 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001996}
1997
1998static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1999{
2000 struct h1c *h1c = ctx;
2001 int ret = 0;
2002
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002003 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002004 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002005 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002006 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002007 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002008 h1_process(h1c);
2009 return NULL;
2010}
2011
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002012static void h1_reset(struct connection *conn)
2013{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002014 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002015
2016 /* Reset the flags, and let the mux know we're waiting for a connection */
2017 h1c->flags = H1C_F_CS_WAIT_CONN;
2018}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002019
2020static int h1_wake(struct connection *conn)
2021{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002022 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002023 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002024
Christopher Faulet539e0292018-11-19 10:40:09 +01002025 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002026 ret = h1_process(h1c);
2027 if (ret == 0) {
2028 struct h1s *h1s = h1c->h1s;
2029
2030 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2031 ret = h1s->cs->data_cb->wake(h1s->cs);
2032 }
2033 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002034}
2035
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002036/* Connection timeout management. The principle is that if there's no receipt
2037 * nor sending for a certain amount of time, the connection is closed.
2038 */
2039static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2040{
2041 struct h1c *h1c = context;
2042 int expired = tick_is_expired(t->expire, now_ms);
2043
2044 if (!expired && h1c)
2045 return t;
2046
Olivier Houchard3f795f72019-04-17 22:51:06 +02002047 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002048
2049 if (!h1c) {
2050 /* resources were already deleted */
2051 return NULL;
2052 }
2053
2054 h1c->task = NULL;
2055 /* If a stream is still attached to the mux, just set an error and wait
2056 * for the stream's timeout. Otherwise, release the mux. This is only ok
2057 * because same timeouts are used.
2058 */
2059 if (h1c->h1s && h1c->h1s->cs)
2060 h1c->flags |= H1C_F_CS_ERROR;
2061 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002062 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002063 return NULL;
2064}
2065
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066/*******************************************/
2067/* functions below are used by the streams */
2068/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002069
Christopher Faulet51dbc942018-09-13 09:05:15 +02002070/*
2071 * Attach a new stream to a connection
2072 * (Used for outgoing connections)
2073 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002074static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002075{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002076 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002077 struct conn_stream *cs = NULL;
2078 struct h1s *h1s;
2079
2080 if (h1c->flags & H1C_F_CS_ERROR)
2081 goto end;
2082
2083 cs = cs_new(h1c->conn);
2084 if (!cs)
2085 goto end;
2086
Olivier Houchardf502aca2018-12-14 19:42:40 +01002087 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002088 if (h1s == NULL)
2089 goto end;
2090
2091 return cs;
2092 end:
2093 cs_free(cs);
2094 return NULL;
2095}
2096
2097/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2098 * this mux, it's easy as we can only store a single conn_stream.
2099 */
2100static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2101{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002102 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002103 struct h1s *h1s = h1c->h1s;
2104
2105 if (h1s)
2106 return h1s->cs;
2107
2108 return NULL;
2109}
2110
Christopher Faulet73c12072019-04-08 11:23:22 +02002111static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002112{
Christopher Faulet73c12072019-04-08 11:23:22 +02002113 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002114
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002115 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002116 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002117}
2118
2119/*
2120 * Detach the stream from the connection and possibly release the connection.
2121 */
2122static void h1_detach(struct conn_stream *cs)
2123{
2124 struct h1s *h1s = cs->ctx;
2125 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002126 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002127 int has_keepalive;
2128 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129
2130 cs->ctx = NULL;
2131 if (!h1s)
2132 return;
2133
Olivier Houchardf502aca2018-12-14 19:42:40 +01002134 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002135 h1c = h1s->h1c;
2136 h1s->cs = NULL;
2137
Olivier Houchard8a786902018-12-15 16:05:40 +01002138 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2139 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2140 h1s_destroy(h1s);
2141
2142 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002143 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002144 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002145 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002146 h1c->conn->flags |= CO_FL_PRIVATE;
2147
Olivier Houchard44d59142018-12-13 18:46:22 +01002148 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002149 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002150 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2151 h1c->conn->owner = NULL;
2152 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2153 /* The server doesn't want it, let's kill the connection right away */
2154 h1c->conn->mux->destroy(h1c->conn);
2155 else
2156 tasklet_wakeup(h1c->wait_event.task);
2157 return;
2158
2159 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002160 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002161 if (h1c->conn->owner == sess) {
2162 int ret = session_check_idle_conn(sess, h1c->conn);
2163 if (ret == -1)
2164 /* The connection got destroyed, let's leave */
2165 return;
2166 else if (ret == 1) {
2167 /* The connection was added to the server list,
2168 * wake the task so we can subscribe to events
2169 */
2170 tasklet_wakeup(h1c->wait_event.task);
2171 return;
2172 }
2173 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002174 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002175 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002176 struct server *srv = objt_server(h1c->conn->target);
2177
2178 if (srv) {
2179 if (h1c->conn->flags & CO_FL_PRIVATE)
2180 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002181 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002182 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2183 else
2184 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2185 }
2186 }
2187 }
2188
Christopher Faulet51dbc942018-09-13 09:05:15 +02002189 /* We don't want to close right now unless the connection is in error */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002190 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002191 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002192 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002193 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002194 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002195 if (h1c->task) {
2196 h1c->task->expire = TICK_ETERNITY;
2197 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002198 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 +01002199 ? h1c->shut_timeout
2200 : h1c->timeout));
2201 task_queue(h1c->task);
2202 }
2203 }
2204 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002205}
2206
2207
2208static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2209{
2210 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002211 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002212
2213 if (!h1s)
2214 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002215 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002216
Christopher Faulet7f366362019-04-08 10:51:20 +02002217 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2218 goto do_shutr;
2219
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002220 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002221 return;
2222
Christopher Faulet7f366362019-04-08 10:51:20 +02002223 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002224 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2225 if (cs->flags & CS_FL_SHR)
2226 return;
2227 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002228 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2229 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002230 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 +02002231 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002232}
2233
2234static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2235{
2236 struct h1s *h1s = cs->ctx;
2237 struct h1c *h1c;
2238
2239 if (!h1s)
2240 return;
2241 h1c = h1s->h1c;
2242
Christopher Faulet7f366362019-04-08 10:51:20 +02002243 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2244 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002245
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002246 if ((h1c->flags & H1C_F_UPG_H2C) ||
2247 ((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 +02002248 return;
2249
Christopher Faulet7f366362019-04-08 10:51:20 +02002250 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002251 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2252 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2253 return;
2254
Christopher Faulet666a0c42019-01-08 11:12:04 +01002255 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002256}
2257
Christopher Faulet666a0c42019-01-08 11:12:04 +01002258static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002259{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002260 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002261
Christopher Faulet666a0c42019-01-08 11:12:04 +01002262 conn_xprt_shutw(conn);
2263 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2264 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2265 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002266}
2267
2268/* Called from the upper layer, to unsubscribe to events */
2269static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2270{
2271 struct wait_event *sw;
2272 struct h1s *h1s = cs->ctx;
2273
2274 if (!h1s)
2275 return 0;
2276
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002277 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002278 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002279 BUG_ON(h1s->recv_wait != sw);
2280 sw->events &= ~SUB_RETRY_RECV;
2281 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002282 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002283 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002284 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002285 BUG_ON(h1s->send_wait != sw);
2286 sw->events &= ~SUB_RETRY_SEND;
2287 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002288 }
2289 return 0;
2290}
2291
2292/* Called from the upper layer, to subscribe to events, such as being able to send */
2293static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2294{
2295 struct wait_event *sw;
2296 struct h1s *h1s = cs->ctx;
2297
2298 if (!h1s)
2299 return -1;
2300
2301 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002302 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002303 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002304 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2305 sw->events |= SUB_RETRY_RECV;
2306 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002307 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002308 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002309 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002310 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2311 sw->events |= SUB_RETRY_SEND;
2312 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002313 return 0;
2314 default:
2315 break;
2316 }
2317 return -1;
2318}
2319
2320/* Called from the upper layer, to receive data */
2321static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2322{
2323 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002324 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002325 size_t ret = 0;
2326
Christopher Faulet539e0292018-11-19 10:40:09 +01002327 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002328 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002329
Christopher Faulete18777b2019-04-16 16:46:36 +02002330 if (flags & CO_RFL_BUF_FLUSH) {
2331 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2332
2333 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2334 h1s->flags |= H1S_F_BUF_FLUSH;
2335 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002336 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2337 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002338 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002339 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002340 }
2341 return ret;
2342}
2343
2344
2345/* Called from the upper layer, to send data */
2346static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2347{
2348 struct h1s *h1s = cs->ctx;
2349 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002350 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002351
2352 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002353 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354
2355 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002356 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2357 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002358
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002359 while (total != count) {
2360 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002361
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002362 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2363 ret = h1_process_output(h1c, buf, count);
2364 if (!ret)
2365 break;
2366 total += ret;
2367 if (!h1_send(h1c))
2368 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002370
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002371 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002372}
2373
Willy Tarreaue5733232019-05-22 19:24:06 +02002374#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002375/* Send and get, using splicing */
2376static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2377{
2378 struct h1s *h1s = cs->ctx;
2379 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2380 int ret = 0;
2381
Christopher Faulet1146f972019-05-29 14:35:24 +02002382 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002383 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002384 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2385 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002386 goto end;
2387 }
2388
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002389 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002390 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002391 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002392 }
2393
2394 h1s->flags &= ~H1S_F_BUF_FLUSH;
2395 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002396 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2397 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002398 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002399 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002400 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002401 if (!h1m->curr_len)
2402 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2403 }
2404
Christopher Faulet1be55f92018-10-02 15:59:23 +02002405 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002406 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002407 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002408 if (!pipe->data)
2409 cs->flags |= CS_FL_EOS;
2410 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002411 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002412}
2413
2414static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2415{
2416 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002417 int ret = 0;
2418
2419 if (b_data(&h1s->h1c->obuf))
2420 goto end;
2421
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002422 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002423 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002424 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002425 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002426 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002427 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002428 return ret;
2429}
2430#endif
2431
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002432/* for debugging with CLI's "show fd" command */
2433static void h1_show_fd(struct buffer *msg, struct connection *conn)
2434{
2435 struct h1c *h1c = conn->ctx;
2436 struct h1s *h1s = h1c->h1s;
2437
Christopher Fauletf376a312019-01-04 15:16:06 +01002438 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2439 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002440 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2441 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2442 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2443 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2444
2445 if (h1s) {
2446 char *method;
2447
2448 if (h1s->meth < HTTP_METH_OTHER)
2449 method = http_known_methods[h1s->meth].ptr;
2450 else
2451 method = "UNKNOWN";
2452 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2453 " .meth=%s status=%d",
2454 h1s, h1s->flags,
2455 h1m_state_str(h1s->req.state),
2456 h1m_state_str(h1s->res.state), method, h1s->status);
2457 if (h1s->cs)
2458 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2459 h1s->cs->flags, h1s->cs->data);
2460 }
2461}
2462
Christopher Faulet51dbc942018-09-13 09:05:15 +02002463/****************************************/
2464/* MUX initialization and instanciation */
2465/****************************************/
2466
2467/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002468static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002469 .init = h1_init,
2470 .wake = h1_wake,
2471 .attach = h1_attach,
2472 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002473 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002474 .detach = h1_detach,
2475 .destroy = h1_destroy,
2476 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002477 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002478 .rcv_buf = h1_rcv_buf,
2479 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002480#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002481 .rcv_pipe = h1_rcv_pipe,
2482 .snd_pipe = h1_snd_pipe,
2483#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002484 .subscribe = h1_subscribe,
2485 .unsubscribe = h1_unsubscribe,
2486 .shutr = h1_shutr,
2487 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002488 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002489 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002490 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002491 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002492};
2493
2494
2495/* this mux registers default HTX proto */
2496static struct mux_proto_list mux_proto_htx =
2497{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2498
Willy Tarreau0108d902018-11-25 19:14:37 +01002499INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2500
Christopher Faulet51dbc942018-09-13 09:05:15 +02002501/*
2502 * Local variables:
2503 * c-indent-level: 8
2504 * c-basic-offset: 8
2505 * End:
2506 */