blob: 0b76ef4492c3e7391fe5a8cefe9e8e87fe924c74 [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_EOD 0x00000400 /* Set during input process to know the last empty chunk was processed */
70#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
71#define H1S_F_HAVE_O_EOD 0x00001000 /* Set during output process to know the last empty chunk was processed */
72#define H1S_F_HAVE_O_TLR 0x00002000 /* Set during output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073
74/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020075struct h1c {
76 struct connection *conn;
77 struct proxy *px;
78 uint32_t flags; /* Connection flags: H1C_F_* */
79
80 struct buffer ibuf; /* Input buffer to store data before parsing */
81 struct buffer obuf; /* Output buffer to store data after reformatting */
82
83 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
84 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
85
86 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010087 struct task *task; /* timeout management task */
88 int timeout; /* idle timeout duration in ticks */
89 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020090};
91
92/* H1 stream descriptor */
93struct h1s {
94 struct h1c *h1c;
95 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010096 struct cs_info csinfo; /* CS info, only used for client connections */
97 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098
Christopher Faulet51dbc942018-09-13 09:05:15 +020099 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
100 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200101
Olivier Houchardf502aca2018-12-14 19:42:40 +0100102 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200103 struct h1m req;
104 struct h1m res;
105
106 enum http_meth_t meth; /* HTTP resquest method */
107 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200108};
109
110/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100111DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
112DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200113
Christopher Faulet51dbc942018-09-13 09:05:15 +0200114static int h1_recv(struct h1c *h1c);
115static int h1_send(struct h1c *h1c);
116static int h1_process(struct h1c *h1c);
117static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100118static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100119static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200120
121/*****************************************************/
122/* functions below are for dynamic buffer management */
123/*****************************************************/
124/*
125 * Indicates whether or not the we may call the h1_recv() function to
126 * attempt to receive data into the buffer and/or parse pending data. The
127 * condition is a bit complex due to some API limits for now. The rules are the
128 * following :
129 * - if an error or a shutdown was detected on the connection and the buffer
130 * is empty, we must not attempt to receive
131 * - if the input buffer failed to be allocated, we must not try to receive
132 * and we know there is nothing pending
133 * - if no flag indicates a blocking condition, we may attempt to receive,
134 * regardless of whether the input buffer is full or not, so that only de
135 * receiving part decides whether or not to block. This is needed because
136 * the connection API indeed prevents us from re-enabling receipt that is
137 * already enabled in a polled state, so we must always immediately stop as
138 * soon as the mux can't proceed so as never to hit an end of read with data
139 * pending in the buffers.
140 * - otherwise must may not attempt to receive
141 */
142static inline int h1_recv_allowed(const struct h1c *h1c)
143{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100144 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200145 return 0;
146
Christopher Fauletcf56b992018-12-11 16:12:31 +0100147 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
148 return 0;
149
Christopher Fauletcb55f482018-12-10 11:56:47 +0100150 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200151 return 1;
152
153 return 0;
154}
155
156/*
157 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
158 * flags are used to figure what buffer was requested. It returns 1 if the
159 * allocation succeeds, in which case the connection is woken up, or 0 if it's
160 * impossible to wake up and we prefer to be woken up later.
161 */
162static int h1_buf_available(void *target)
163{
164 struct h1c *h1c = target;
165
166 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
167 h1c->flags &= ~H1C_F_IN_ALLOC;
168 if (h1_recv_allowed(h1c))
169 tasklet_wakeup(h1c->wait_event.task);
170 return 1;
171 }
172
173 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
174 h1c->flags &= ~H1C_F_OUT_ALLOC;
175 tasklet_wakeup(h1c->wait_event.task);
176 return 1;
177 }
178
Christopher Faulet51dbc942018-09-13 09:05:15 +0200179 return 0;
180}
181
182/*
183 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
184 */
185static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
186{
187 struct buffer *buf = NULL;
188
189 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
190 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
191 h1c->buf_wait.target = h1c;
192 h1c->buf_wait.wakeup_cb = h1_buf_available;
193 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
194 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
195 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
196 __conn_xprt_stop_recv(h1c->conn);
197 }
198 return buf;
199}
200
201/*
202 * Release a buffer, if any, and try to wake up entities waiting in the buffer
203 * wait queue.
204 */
205static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
206{
207 if (bptr->size) {
208 b_free(bptr);
209 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
210 }
211}
212
Willy Tarreau00f18a32019-01-26 12:19:01 +0100213/* returns the number of streams in use on a connection to figure if it's
214 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
215 * as the caller will want to know if it was the last one after a detach().
216 */
217static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200218{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100219 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200220
Willy Tarreau00f18a32019-01-26 12:19:01 +0100221 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200222}
223
Willy Tarreau00f18a32019-01-26 12:19:01 +0100224/* returns the number of streams still available on a connection */
225static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100226{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100227 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100228}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200229
Willy Tarreau00f18a32019-01-26 12:19:01 +0100230
Christopher Faulet51dbc942018-09-13 09:05:15 +0200231/*****************************************************************/
232/* functions below are dedicated to the mux setup and management */
233/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200234
235/* returns non-zero if there are input data pending for stream h1s. */
236static inline size_t h1s_data_pending(const struct h1s *h1s)
237{
238 const struct h1m *h1m;
239
240 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
241 if (h1m->state == H1_MSG_DONE)
242 return 0; // data not for this stream (e.g. pipelining)
243
244 return b_data(&h1s->h1c->ibuf);
245}
246
Christopher Faulet47365272018-10-31 17:40:50 +0100247static struct conn_stream *h1s_new_cs(struct h1s *h1s)
248{
249 struct conn_stream *cs;
250
251 cs = cs_new(h1s->h1c->conn);
252 if (!cs)
253 goto err;
254 h1s->cs = cs;
255 cs->ctx = h1s;
256
257 if (h1s->flags & H1S_F_NOT_FIRST)
258 cs->flags |= CS_FL_NOT_FIRST;
259
260 if (stream_create_from_cs(cs) < 0)
261 goto err;
262 return cs;
263
264 err:
265 cs_free(cs);
266 h1s->cs = NULL;
267 return NULL;
268}
269
Olivier Houchardf502aca2018-12-14 19:42:40 +0100270static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200271{
272 struct h1s *h1s;
273
274 h1s = pool_alloc(pool_head_h1s);
275 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100276 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200277
278 h1s->h1c = h1c;
279 h1c->h1s = h1s;
280
Olivier Houchardf502aca2018-12-14 19:42:40 +0100281 h1s->sess = sess;
282
Christopher Faulet51dbc942018-09-13 09:05:15 +0200283 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100284 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200285
286 h1s->recv_wait = NULL;
287 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200288
289 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100290 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200291
Christopher Faulet129817b2018-09-20 16:14:40 +0200292 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100293 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200294
295 h1s->status = 0;
296 h1s->meth = HTTP_METH_OTHER;
297
Christopher Faulet47365272018-10-31 17:40:50 +0100298 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
299 h1s->flags |= H1S_F_NOT_FIRST;
300 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
301
Christopher Faulet129817b2018-09-20 16:14:40 +0200302 if (!conn_is_back(h1c->conn)) {
303 if (h1c->px->options2 & PR_O2_REQBUG_OK)
304 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200305
306 /* For frontend connections we should always have a session */
307 if (!sess)
308 sess = h1c->conn->owner;
309
310 h1s->csinfo.create_date = sess->accept_date;
311 h1s->csinfo.tv_create = sess->tv_accept;
312 h1s->csinfo.t_handshake = sess->t_handshake;
313 h1s->csinfo.t_idle = -1;
Christopher Faulet129817b2018-09-20 16:14:40 +0200314 }
315 else {
316 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
317 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200318
Christopher Fauletfeb11742018-11-29 15:12:34 +0100319 h1s->csinfo.create_date = date;
320 h1s->csinfo.tv_create = now;
321 h1s->csinfo.t_handshake = 0;
322 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200323 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100324
Christopher Faulete9b70722019-04-08 10:46:02 +0200325 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
326 * create a new one.
327 */
328 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200329 cs->ctx = h1s;
330 h1s->cs = cs;
331 }
Christopher Faulet47365272018-10-31 17:40:50 +0100332 else {
333 cs = h1s_new_cs(h1s);
334 if (!cs)
335 goto fail;
336 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200337 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100338
339 fail:
340 pool_free(pool_head_h1s, h1s);
341 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200342}
343
344static void h1s_destroy(struct h1s *h1s)
345{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200346 if (h1s) {
347 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200348
Christopher Fauletf2824e62018-10-01 12:12:37 +0200349 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200350
Christopher Fauletf2824e62018-10-01 12:12:37 +0200351 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100352 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200353 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100354 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200355
Christopher Fauletcb55f482018-12-10 11:56:47 +0100356 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100357 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
358 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
359 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200360 pool_free(pool_head_h1s, h1s);
361 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200362}
363
Christopher Fauletfeb11742018-11-29 15:12:34 +0100364static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
365{
366 struct h1s *h1s = cs->ctx;
367
368 if (h1s && !conn_is_back(cs->conn))
369 return &h1s->csinfo;
370 return NULL;
371}
372
Christopher Faulet51dbc942018-09-13 09:05:15 +0200373/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200374 * Initialize the mux once it's attached. It is expected that conn->ctx points
375 * to the existing conn_stream (for outgoing connections or for incoming onces
376 * during a mux upgrade) or NULL (for incoming ones during the connexion
377 * establishment). <input> is always used as Input buffer and may contain
378 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
379 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200381static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
382 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200383{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200384 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100385 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200386
387 h1c = pool_alloc(pool_head_h1c);
388 if (!h1c)
389 goto fail_h1c;
390 h1c->conn = conn;
391 h1c->px = proxy;
392
393 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200394 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200395 h1c->obuf = BUF_NULL;
396 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200397 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200398
Christopher Faulet51dbc942018-09-13 09:05:15 +0200399 LIST_INIT(&h1c->buf_wait.list);
400 h1c->wait_event.task = tasklet_new();
401 if (!h1c->wait_event.task)
402 goto fail;
403 h1c->wait_event.task->process = h1_io_cb;
404 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100405 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200406
Christopher Faulete9b70722019-04-08 10:46:02 +0200407 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100408 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
409 if (tick_isset(proxy->timeout.serverfin))
410 h1c->shut_timeout = proxy->timeout.serverfin;
411 } else {
412 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
413 if (tick_isset(proxy->timeout.clientfin))
414 h1c->shut_timeout = proxy->timeout.clientfin;
415 }
416 if (tick_isset(h1c->timeout)) {
417 t = task_new(tid_bit);
418 if (!t)
419 goto fail;
420
421 h1c->task = t;
422 t->process = h1_timeout_task;
423 t->context = h1c;
424 t->expire = tick_add(now_ms, h1c->timeout);
425 }
426
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200427 if (!(conn->flags & CO_FL_CONNECTED))
428 h1c->flags |= H1C_F_CS_WAIT_CONN;
429
Christopher Fauletf2824e62018-10-01 12:12:37 +0200430 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100431 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200432 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200433
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100434 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200435
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100436
437 if (t)
438 task_queue(t);
439
Christopher Faulet51dbc942018-09-13 09:05:15 +0200440 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100441 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200442
443 /* mux->wake will be called soon to complete the operation */
444 return 0;
445
446 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200447 task_destroy(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100448 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449 tasklet_free(h1c->wait_event.task);
450 pool_free(pool_head_h1c, h1c);
451 fail_h1c:
452 return -1;
453}
454
Christopher Faulet73c12072019-04-08 11:23:22 +0200455/* release function. This one should be called to free all resources allocated
456 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200457 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200458static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200459{
Christopher Faulet61840e72019-04-15 09:33:32 +0200460 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200461
Christopher Faulet51dbc942018-09-13 09:05:15 +0200462 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200463 /* The connection must be aattached to this mux to be released */
464 if (h1c->conn && h1c->conn->ctx == h1c)
465 conn = h1c->conn;
466
467 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200468 h1c->flags &= ~H1C_F_UPG_H2C;
469 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
470 /* connection successfully upgraded to H2, this
471 * mux was already released */
472 return;
473 }
474 sess_log(conn->owner); /* Log if the upgrade failed */
475 }
476
Christopher Faulet61840e72019-04-15 09:33:32 +0200477
Christopher Faulet51dbc942018-09-13 09:05:15 +0200478 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
479 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
480 LIST_DEL(&h1c->buf_wait.list);
481 LIST_INIT(&h1c->buf_wait.list);
482 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
483 }
484
485 h1_release_buf(h1c, &h1c->ibuf);
486 h1_release_buf(h1c, &h1c->obuf);
487
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100488 if (h1c->task) {
489 h1c->task->context = NULL;
490 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
491 h1c->task = NULL;
492 }
493
Christopher Faulet51dbc942018-09-13 09:05:15 +0200494 if (h1c->wait_event.task)
495 tasklet_free(h1c->wait_event.task);
496
Christopher Fauletf2824e62018-10-01 12:12:37 +0200497 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200498 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100499 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200500 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200501 pool_free(pool_head_h1c, h1c);
502 }
503
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200504 if (conn) {
505 conn->mux = NULL;
506 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200507
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200508 conn_stop_tracking(conn);
509 conn_full_close(conn);
510 if (conn->destroy_cb)
511 conn->destroy_cb(conn);
512 conn_free(conn);
513 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200514}
515
516/******************************************************/
517/* functions below are for the H1 protocol processing */
518/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200519/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
520 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200521 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100522static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200523{
Christopher Faulet570d1612018-11-26 11:13:57 +0100524 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200525
Christopher Faulet570d1612018-11-26 11:13:57 +0100526 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200527 (*(p + 5) > '1' ||
528 (*(p + 5) == '1' && *(p + 7) >= '1')))
529 h1m->flags |= H1_MF_VER_11;
530}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200531
Christopher Faulet9768c262018-10-22 09:34:31 +0200532/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
533 * greater or equal to 1.1
534 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100535static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200536{
Christopher Faulet570d1612018-11-26 11:13:57 +0100537 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200538
Christopher Faulet570d1612018-11-26 11:13:57 +0100539 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200540 (*(p + 5) > '1' ||
541 (*(p + 5) == '1' && *(p + 7) >= '1')))
542 h1m->flags |= H1_MF_VER_11;
543}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200544
Christopher Faulet9768c262018-10-22 09:34:31 +0200545/*
546 * Check the validity of the request version. If the version is valid, it
547 * returns 1. Otherwise, it returns 0.
548 */
549static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
550{
551 struct h1c *h1c = h1s->h1c;
552
553 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
554 * exactly one digit "." one digit. This check may be disabled using
555 * option accept-invalid-http-request.
556 */
557 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
558 if (sl.rq.v.len != 8)
559 return 0;
560
561 if (*(sl.rq.v.ptr + 4) != '/' ||
562 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
563 *(sl.rq.v.ptr + 6) != '.' ||
564 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
565 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200566 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200567 else if (!sl.rq.v.len) {
568 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200569
Christopher Faulet9768c262018-10-22 09:34:31 +0200570 /* RFC 1945 allows only GET for HTTP/0.9 requests */
571 if (sl.rq.meth != HTTP_METH_GET)
572 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200573
Christopher Faulet9768c262018-10-22 09:34:31 +0200574 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
575 if (!sl.rq.u.len)
576 return 0;
577
578 /* Add HTTP version */
579 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100580 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200581 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100582
583 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100584 ((*(sl.rq.v.ptr + 5) > '1') ||
585 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100586 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200587 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200588}
589
Christopher Faulet9768c262018-10-22 09:34:31 +0200590/*
591 * Check the validity of the response version. If the version is valid, it
592 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200593 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200594static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200595{
Christopher Faulet9768c262018-10-22 09:34:31 +0200596 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200597
Christopher Faulet9768c262018-10-22 09:34:31 +0200598 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
599 * exactly one digit "." one digit. This check may be disabled using
600 * option accept-invalid-http-request.
601 */
602 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
603 if (sl.st.v.len != 8)
604 return 0;
605
606 if (*(sl.st.v.ptr + 4) != '/' ||
607 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
608 *(sl.st.v.ptr + 6) != '.' ||
609 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
610 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200611 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100612
613 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100614 ((*(sl.st.v.ptr + 5) > '1') ||
615 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100616 h1m->flags |= H1_MF_VER_11;
617
Christopher Faulet9768c262018-10-22 09:34:31 +0200618 return 1;
619}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200620
621/* Deduce the connection mode of the client connection, depending on the
622 * configuration and the H1 message flags. This function is called twice, the
623 * first time when the request is parsed and the second time when the response
624 * is parsed.
625 */
626static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
627{
628 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200629
630 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100631 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200632 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100633 h1s->status == 101) {
634 /* Either we've established an explicit tunnel, or we're
635 * switching the protocol. In both cases, we're very unlikely to
636 * understand the next protocols. We have to switch to tunnel
637 * mode, so that we transfer the request and responses then let
638 * this protocol pass unmodified. When we later implement
639 * specific parsers for such protocols, we'll want to check the
640 * Upgrade header which contains information about that protocol
641 * for responses with status 101 (eg: see RFC2817 about TLS).
642 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200643 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100644 }
645 else if (h1s->flags & H1S_F_WANT_KAL) {
646 /* By default the client is in KAL mode. CLOSE mode mean
647 * it is imposed by the client itself. So only change
648 * KAL mode here. */
649 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
650 /* no length known or explicit close => close */
651 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
652 }
653 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
654 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
655 /* no explict keep-alive and option httpclose => close */
656 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
657 }
658 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200659 }
660 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100661 /* Input direction: first pass */
662 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
663 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200664 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100665 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200666 }
667
668 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
669 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
670 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
671}
672
673/* Deduce the connection mode of the client connection, depending on the
674 * configuration and the H1 message flags. This function is called twice, the
675 * first time when the request is parsed and the second time when the response
676 * is parsed.
677 */
678static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
679{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100680 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100681 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100682 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200683
Christopher Fauletf2824e62018-10-01 12:12:37 +0200684 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100685 /* Input direction: second pass */
686
Christopher Fauletf2824e62018-10-01 12:12:37 +0200687 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100688 h1s->status == 101) {
689 /* Either we've established an explicit tunnel, or we're
690 * switching the protocol. In both cases, we're very unlikely to
691 * understand the next protocols. We have to switch to tunnel
692 * mode, so that we transfer the request and responses then let
693 * this protocol pass unmodified. When we later implement
694 * specific parsers for such protocols, we'll want to check the
695 * Upgrade header which contains information about that protocol
696 * for responses with status 101 (eg: see RFC2817 about TLS).
697 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200698 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100699 }
700 else if (h1s->flags & H1S_F_WANT_KAL) {
701 /* By default the server is in KAL mode. CLOSE mode mean
702 * it is imposed by haproxy itself. So only change KAL
703 * mode here. */
704 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
705 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
706 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
707 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
708 }
709 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200710 }
Christopher Faulet70033782018-12-05 13:50:11 +0100711 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100712 /* Output direction: first pass */
713 if (h1m->flags & H1_MF_CONN_CLO) {
714 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100715 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100716 }
717 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
718 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
719 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
720 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
721 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
722 /* no explicit keep-alive option httpclose/server-close => close */
723 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
724 }
Christopher Faulet70033782018-12-05 13:50:11 +0100725 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200726
727 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
728 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
729 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200730}
731
Christopher Fauletb992af02019-03-28 15:42:24 +0100732static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200733{
734 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200735
736 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
737 * token is found
738 */
739 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200740 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200741
742 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100743 if (!(h1m->flags & H1_MF_VER_11))
744 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200745 }
746 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100747 if (h1m->flags & H1_MF_VER_11)
748 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200749 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200750}
751
Christopher Fauletb992af02019-03-28 15:42:24 +0100752static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200753{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200754 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
755 * token is found
756 */
757 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200758 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200759
760 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100761 if (!(h1m->flags & H1_MF_VER_11) ||
762 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
763 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200764 }
765 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100766 if (h1m->flags & H1_MF_VER_11)
767 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200768 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200769}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200770
Christopher Fauletb992af02019-03-28 15:42:24 +0100771static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200772{
Christopher Fauletb992af02019-03-28 15:42:24 +0100773 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200774 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100775 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200776 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200777}
778
Christopher Fauletb992af02019-03-28 15:42:24 +0100779static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
780{
781 if (!conn_is_back(h1s->h1c->conn))
782 h1_set_cli_conn_mode(h1s, h1m);
783 else
784 h1_set_srv_conn_mode(h1s, h1m);
785
786 if (!(h1m->flags & H1_MF_RESP))
787 h1_update_req_conn_value(h1s, h1m, conn_val);
788 else
789 h1_update_res_conn_value(h1s, h1m, conn_val);
790}
Christopher Faulete44769b2018-11-29 23:01:45 +0100791
792/* Append the description of what is present in error snapshot <es> into <out>.
793 * The description must be small enough to always fit in a buffer. The output
794 * buffer may be the trash so the trash must not be used inside this function.
795 */
796static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
797{
798 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100799 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
800 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
801 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
802 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
803 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
804 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100805}
806/*
807 * Capture a bad request or response and archive it in the proxy's structure.
808 * By default it tries to report the error position as h1m->err_pos. However if
809 * this one is not set, it will then report h1m->next, which is the last known
810 * parsing point. The function is able to deal with wrapping buffers. It always
811 * displays buffers as a contiguous area starting at buf->p. The direction is
812 * determined thanks to the h1m's flags.
813 */
814static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
815 struct h1m *h1m, struct buffer *buf)
816{
817 struct session *sess = h1c->conn->owner;
818 struct proxy *proxy = h1c->px;
819 struct proxy *other_end = sess->fe;
820 union error_snapshot_ctx ctx;
821
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100822 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100823 other_end = si_strm(h1s->cs->data)->be;
824
825 /* http-specific part now */
826 ctx.h1.state = h1m->state;
827 ctx.h1.c_flags = h1c->flags;
828 ctx.h1.s_flags = h1s->flags;
829 ctx.h1.m_flags = h1m->flags;
830 ctx.h1.m_clen = h1m->curr_len;
831 ctx.h1.m_blen = h1m->body_len;
832
833 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
834 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100835 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
836 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100837}
838
Christopher Fauletadb22202018-12-12 10:32:09 +0100839/* Emit the chunksize followed by a CRLF in front of data of the buffer
840 * <buf>. It goes backwards and starts with the byte before the buffer's
841 * head. The caller is responsible for ensuring there is enough room left before
842 * the buffer's head for the string.
843 */
844static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
845{
846 char *beg, *end;
847
848 beg = end = b_head(buf);
849 *--beg = '\n';
850 *--beg = '\r';
851 do {
852 *--beg = hextab[chksz & 0xF];
853 } while (chksz >>= 4);
854 buf->head -= (end - beg);
855 b_add(buf, end - beg);
856}
857
858/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
859 * ensuring there is enough room left in the buffer for the string. */
860static void h1_emit_chunk_crlf(struct buffer *buf)
861{
862 *(b_peek(buf, b_data(buf))) = '\r';
863 *(b_peek(buf, b_data(buf) + 1)) = '\n';
864 b_add(buf, 2);
865}
866
Christopher Faulet129817b2018-09-20 16:14:40 +0200867/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100868 * Switch the request to tunnel mode. This function must only be called for
869 * CONNECT requests. On the client side, the mux is mark as busy on input,
870 * waiting the response.
871 */
872static void h1_set_req_tunnel_mode(struct h1s *h1s)
873{
874 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
875 h1s->req.state = H1_MSG_TUNNEL;
876 if (!conn_is_back(h1s->h1c->conn))
877 h1s->h1c->flags |= H1C_F_IN_BUSY;
878}
879
880/*
881 * Switch the response to tunnel mode. This function must only be called on
882 * successfull replies to CONNECT requests or on protocol switching. On the
883 * server side, if the request is not finished, the mux is mark as busy on
884 * input. Otherwise the request is also switch to tunnel mode.
885 */
886static void h1_set_res_tunnel_mode(struct h1s *h1s)
887{
888 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
889 h1s->res.state = H1_MSG_TUNNEL;
890 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
891 h1s->h1c->flags |= H1C_F_IN_BUSY;
892 else {
893 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
894 h1s->req.state = H1_MSG_TUNNEL;
895 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
896 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
897 tasklet_wakeup(h1s->h1c->wait_event.task);
898 }
899 }
900}
901
Christopher Faulet30db3d72019-05-17 15:35:33 +0200902/* Estimate the size of the HTX request after the parsing. */
903static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
904{
905 size_t sz;
906 int i;
907
908 /* size of the HTX start-line */
909 sz = sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
910
911 /* size of all HTX headers */
912 for (i = 0; hdrs[i].n.len; i++)
913 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
914
915 /* size of the EOH */
916 sz += sizeof(struct htx_blk) + 1;
917
918 /* size of the EOM */
919 if (h1m->state == H1_MSG_DONE)
920 sz += sizeof(struct htx_blk) + 1;
921
922 return sz;
923}
924
925/* Estimate the size of the HTX response after the parsing. */
926static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
927{
928 size_t sz;
929 int i;
930
931 /* size of the HTX start-line */
932 sz = sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len;
933
934 /* size of all HTX headers */
935 for (i = 0; hdrs[i].n.len; i++)
936 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
937
938 /* size of the EOH */
939 sz += sizeof(struct htx_blk) + 1;
940
941 /* size of the EOM */
942 if (h1m->state == H1_MSG_DONE)
943 sz += sizeof(struct htx_blk) + 1;
944
945 return sz;
946}
947
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100948/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200949 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200950 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
951 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800952 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200953 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200954static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200955 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200956{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200957 struct htx_sl *sl;
Christopher Faulet129817b2018-09-20 16:14:40 +0200958 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100959 union h1_sl h1sl;
960 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200961 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +0200962 int ret = 0;
963
Christopher Faulet30db3d72019-05-17 15:35:33 +0200964 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100965 goto end;
966
Christopher Faulet129817b2018-09-20 16:14:40 +0200967 /* Realing input buffer if necessary */
968 if (b_head(buf) + b_data(buf) > b_wrap(buf))
969 b_slow_realign(buf, trash.area, 0);
970
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200971 if (!(h1m->flags & H1_MF_RESP)) {
972 /* Try to match H2 preface before parsing the request headers. */
973 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
974 if (ret > 0)
975 goto h2c_upgrade;
976 }
977
Christopher Faulet30db3d72019-05-17 15:35:33 +0200978 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100979 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200980 if (ret <= 0) {
981 /* Incomplete or invalid message. If the buffer is full, it's an
982 * error because headers are too large to be handled by the
983 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200984 if (ret < 0 || (!ret && b_full(buf)))
985 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200986 goto end;
987 }
988
989 /* messages headers fully parsed, do some checks to prepare the body
990 * parsing.
991 */
992
Christopher Faulet9768c262018-10-22 09:34:31 +0200993 /* Save the request's method or the response's status, check if the body
994 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200995 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100996 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200997
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100998 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +0200999 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001000
1001 if (h1s->meth == HTTP_METH_CONNECT) {
1002 /* Switch CONNECT requests to tunnel mode */
1003 h1_set_req_tunnel_mode(h1s);
1004 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001005
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001006 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1007 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001008 h1m->err_state = h1m->state;
1009 goto vsn_error;
1010 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001011 }
1012 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001013 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001014
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001015 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1016 h1s->status == 101) {
1017 /* Switch successfull replies to CONNECT requests and
1018 * protocol switching to tunnel mode. */
1019 h1_set_res_tunnel_mode(h1s);
1020 }
1021 else if ((h1s->meth == HTTP_METH_HEAD) ||
1022 (h1s->status >= 100 && h1s->status < 200) ||
1023 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001024 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1026 h1m->flags |= H1_MF_XFER_LEN;
1027 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001028 }
1029 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001030 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001031 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001032 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001033 else {
1034 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001035 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001036 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001037
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001038 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1039 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001040 h1m->err_state = h1m->state;
1041 goto vsn_error;
1042 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001043 }
1044
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001045 /* Set HTX start-line flags */
1046 if (h1m->flags & H1_MF_VER_11)
1047 flags |= HTX_SL_F_VER_11;
1048 if (h1m->flags & H1_MF_XFER_ENC)
1049 flags |= HTX_SL_F_XFER_ENC;
1050 if (h1m->flags & H1_MF_XFER_LEN) {
1051 flags |= HTX_SL_F_XFER_LEN;
1052 if (h1m->flags & H1_MF_CHNK)
1053 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001054 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001055 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001056 if (h1m->body_len == 0)
1057 flags |= HTX_SL_F_BODYLESS;
1058 }
1059 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001060 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001061 }
1062
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001063 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001064 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001065 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1066 if (htx_is_empty(htx))
1067 goto error;
1068 h1m_init_req(h1m);
1069 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1070 ret = 0;
1071 goto end;
1072 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001073
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001074 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1075 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001076 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001077 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001078 }
1079 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001080 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1081 if (htx_is_empty(htx))
1082 goto error;
1083 h1m_init_res(h1m);
1084 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1085 ret = 0;
1086 goto end;
1087 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001088
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001089 flags |= HTX_SL_F_IS_RESP;
1090 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1091 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001093 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001094 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001095
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001096 /* Set bytes used in the HTX mesage for the headers now */
1097 sl->hdrs_bytes = htx_used_space(htx) - used;
1098
Christopher Fauletb992af02019-03-28 15:42:24 +01001099 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001100
1101 /* If body length cannot be determined, set htx->extra to
1102 * ULLONG_MAX. This value is impossible in other cases.
1103 */
1104 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001105 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001106 end:
1107 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001108
1109 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001110 h1m->err_state = h1m->state;
1111 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001112 vsn_error:
1113 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001114 h1s->cs->flags |= CS_FL_EOI;
1115 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001116 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001117 ret = 0;
1118 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001119
1120 h2c_upgrade:
1121 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001122 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001123 htx->flags |= HTX_FL_UPGRADE;
1124 ret = 0;
1125 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001126}
1127
1128/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001129 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1130 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001131 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001132 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001133 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001134static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001135 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001136 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001137{
1138 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001139 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001140
Christopher Faulet129817b2018-09-20 16:14:40 +02001141 if (h1m->flags & H1_MF_XFER_LEN) {
1142 if (h1m->flags & H1_MF_CLEN) {
1143 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001144 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001145 if ((uint64_t)ret > h1m->curr_len)
1146 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001147 if (ret > b_contig_data(buf, *ofs))
1148 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001149
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001151 /* very often with large files we'll face the following
1152 * situation :
1153 * - htx is empty and points to <htxbuf>
1154 * - ret == buf->data
1155 * - buf->head == sizeof(struct htx)
1156 * => we can swap the buffers and place an htx header into
1157 * the target buffer instead
1158 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001159 int32_t try = ret;
1160
Willy Tarreau78f548f2018-12-05 10:02:39 +01001161 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1162 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1163 void *raw_area = buf->area;
1164 void *htx_area = htxbuf->area;
1165 struct htx_blk *blk;
1166
1167 buf->area = htx_area;
1168 htxbuf->area = raw_area;
1169 htx = (struct htx *)htxbuf->area;
1170 htx->size = htxbuf->size - sizeof(*htx);
1171 htx_reset(htx);
1172 b_set_data(htxbuf, b_size(htxbuf));
1173
1174 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1175 blk->info += ret;
1176 /* nothing else to do, the old buffer now contains an
1177 * empty pre-initialized HTX header
1178 */
1179 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001180 else {
1181 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1182 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001183 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001184 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001185 *ofs += ret;
1186 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001187 if (ret < try)
1188 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001189 }
1190
1191 if (!h1m->curr_len) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001192 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001193 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001194 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001195 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet9768c262018-10-22 09:34:31 +02001196 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001197 }
1198 else if (h1m->flags & H1_MF_CHNK) {
1199 new_chunk:
1200 /* te:chunked : parse chunks */
1201 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001202 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001203 if (ret <= 0)
1204 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001205 h1m->state = H1_MSG_CHUNK_SIZE;
1206
Christopher Fauletf2824e62018-10-01 12:12:37 +02001207 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001208 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 }
1210
1211 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1212 unsigned int chksz;
1213
Christopher Faulet30db3d72019-05-17 15:35:33 +02001214 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001215 if (ret <= 0)
1216 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001217 if (!chksz) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001218 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOD))
Christopher Faulet9768c262018-10-22 09:34:31 +02001219 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001220 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001221 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 Faulet129817b2018-09-20 16:14:40 +02001231 }
1232
1233 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001234 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001235 if ((uint64_t)ret > h1m->curr_len)
1236 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 if (ret > b_contig_data(buf, *ofs))
1238 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001239
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001241 int32_t try = ret;
1242
1243 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001244 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001245 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001246 *ofs += ret;
1247 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001248 if (ret < try)
1249 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001250 }
1251 if (!h1m->curr_len) {
1252 h1m->state = H1_MSG_CHUNK_CRLF;
1253 goto new_chunk;
1254 }
1255 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001256 }
1257
1258 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001259 /* Trailers were alread parsed, only the EOM
1260 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001261 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001262 goto skip_tlr_parsing;
1263
Christopher Faulet30db3d72019-05-17 15:35:33 +02001264 ret = htx_get_max_blksz(htx, max);
1265 ret = h1_measure_trailers(buf, *ofs, ret);
1266 if (ret <= 0) {
1267 if (!ret && b_full(buf))
1268 ret = -1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001269 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001270 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001271
1272 /* Realing input buffer if tailers wrap. For now
1273 * this is a workaroung. Because trailers are
1274 * not split on CRLF, like headers, there is no
1275 * way to know where to split it when trailers
1276 * wrap. This is a limitation of
1277 * h1_measure_trailers.
1278 */
1279 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1280 b_slow_realign(buf, trash.area, 0);
1281
1282 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1283 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001284 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001285 max -= sizeof(struct htx_blk) + ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001286 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001287 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001288
Christopher Faulet17276482018-11-22 11:44:35 +01001289 skip_tlr_parsing:
Christopher Faulet30db3d72019-05-17 15:35:33 +02001290 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001291 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001292 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001293 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001294 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 }
1296 }
1297 else {
1298 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1299 * is no body. Switch the message in DONE state
1300 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001301 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001302 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001303 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001304 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001305 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001306 }
1307 }
1308 else {
1309 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001310 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001311 if (ret > b_contig_data(buf, *ofs))
1312 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001313
Christopher Faulet9768c262018-10-22 09:34:31 +02001314 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001315 int32_t try = ret;
1316
1317 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001318
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001319 *ofs += ret;
1320 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001321 if (ret < try)
1322 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001323 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001324 }
1325
1326 end:
1327 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001328 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001329 h1s->cs->flags |= CS_FL_EOI;
1330 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001331 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001332 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001333 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001334 return 0;
1335 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001336 /* update htx->extra, only when the body length is known */
1337 if (h1m->flags & H1_MF_XFER_LEN)
1338 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001339 return total;
1340}
1341
1342/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001343 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001344 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1345 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001346 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001347static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001348{
Christopher Faulet539e0292018-11-19 10:40:09 +01001349 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001350 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001351 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001352 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001353 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001354 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001355
Christopher Faulet539e0292018-11-19 10:40:09 +01001356 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001357
Christopher Fauletf2824e62018-10-01 12:12:37 +02001358 if (!conn_is_back(h1c->conn)) {
1359 h1m = &h1s->req;
1360 errflag = H1S_F_REQ_ERROR;
1361 }
1362 else {
1363 h1m = &h1s->res;
1364 errflag = H1S_F_RES_ERROR;
1365 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001366
Christopher Faulet74027762019-02-26 14:45:05 +01001367 data = htx->data;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001368 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001369 size_t used = htx_used_space(htx);
1370
Christopher Faulet129817b2018-09-20 16:14:40 +02001371 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001372 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001373 if (!ret)
1374 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001375 if ((h1m->flags & H1_MF_RESP) &&
1376 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1377 h1m_init_res(&h1s->res);
1378 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1379 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001380 }
1381 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001382 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001383 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001384 if (!ret)
1385 break;
1386 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001387 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001388 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1389 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001390 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001391 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001392 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001393 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001394 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001395 if (!ret)
1396 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001397 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001398 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001399 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001400 break;
1401 }
1402
Christopher Faulet30db3d72019-05-17 15:35:33 +02001403 count -= htx_used_space(htx) - used;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001404 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001405
Christopher Faulet47365272018-10-31 17:40:50 +01001406 if (h1s->flags & errflag)
1407 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001408
Christopher Faulet539e0292018-11-19 10:40:09 +01001409 b_del(&h1c->ibuf, total);
1410
1411 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001412 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001413 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001414 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001415 h1c->flags &= ~H1C_F_IN_FULL;
1416 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001417 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001418
Christopher Fauletcf56b992018-12-11 16:12:31 +01001419 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1420
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001421 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001422 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001423 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001424 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001425
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001426 if ((h1s->flags & H1S_F_REOS) && (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001427 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001428 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001429 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001430 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001431
Christopher Faulet30db3d72019-05-17 15:35:33 +02001432 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001433
1434 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001435 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001436 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001437 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001438}
1439
Christopher Faulet129817b2018-09-20 16:14:40 +02001440/*
1441 * Process outgoing data. It parses data and transfer them from the channel buffer into
1442 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1443 * 0 if it couldn't proceed.
1444 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001445static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1446{
Christopher Faulet129817b2018-09-20 16:14:40 +02001447 struct h1s *h1s = h1c->h1s;
1448 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001449 struct htx *chn_htx;
1450 struct htx_blk *blk;
1451 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001452 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001453 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001454 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001455
Christopher Faulet47365272018-10-31 17:40:50 +01001456 if (!count)
1457 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001458
Christopher Faulet9768c262018-10-22 09:34:31 +02001459 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001460 if (htx_is_empty(chn_htx))
1461 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001462
Christopher Faulet51dbc942018-09-13 09:05:15 +02001463 if (!h1_get_buf(h1c, &h1c->obuf)) {
1464 h1c->flags |= H1C_F_OUT_ALLOC;
1465 goto end;
1466 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001467
Christopher Fauletf2824e62018-10-01 12:12:37 +02001468 if (!conn_is_back(h1c->conn)) {
1469 h1m = &h1s->res;
1470 errflag = H1S_F_RES_ERROR;
1471 }
1472 else {
1473 h1m = &h1s->req;
1474 errflag = H1S_F_REQ_ERROR;
1475 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001476
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001477 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001478 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001479
1480 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001481
Willy Tarreau3815b222018-12-11 19:50:43 +01001482 /* Perform some optimizations to reduce the number of buffer copies.
1483 * First, if the mux's buffer is empty and the htx area contains
1484 * exactly one data block of the same size as the requested count,
1485 * then it's possible to simply swap the caller's buffer with the
1486 * mux's output buffer and adjust offsets and length to match the
1487 * entire DATA HTX block in the middle. In this case we perform a
1488 * true zero-copy operation from end-to-end. This is the situation
1489 * that happens all the time with large files. Second, if this is not
1490 * possible, but the mux's output buffer is empty, we still have an
1491 * opportunity to avoid the copy to the intermediary buffer, by making
1492 * the intermediary buffer's area point to the output buffer's area.
1493 * In this case we want to skip the HTX header to make sure that copies
1494 * remain aligned and that this operation remains possible all the
1495 * time. This goes for headers, data blocks and any data extracted from
1496 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001497 */
1498 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001499 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001500
1501 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001502 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001503 htx_get_blk_value(chn_htx, blk).len == count) {
1504 void *old_area = h1c->obuf.area;
1505
1506 h1c->obuf.area = buf->area;
1507 h1c->obuf.data = count;
1508
1509 buf->area = old_area;
1510 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001511
1512 /* The message is chunked. We need to emit the chunk
1513 * size. We have at least the size of the struct htx to
1514 * write the chunk envelope. It should be enough.
1515 */
1516 if (h1m->flags & H1_MF_CHNK) {
1517 h1_emit_chunk_size(&h1c->obuf, count);
1518 h1_emit_chunk_crlf(&h1c->obuf);
1519 }
1520
Willy Tarreau3815b222018-12-11 19:50:43 +01001521 total += count;
1522 goto out;
1523 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001524 tmp->area = h1c->obuf.area + h1c->obuf.head;
1525 }
1526
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 tmp->size = b_room(&h1c->obuf);
1528
Christopher Fauletb2e84162018-12-06 11:39:49 +01001529 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001530 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001532 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001533 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001534 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001535
Christopher Fauletb2e84162018-12-06 11:39:49 +01001536 vlen = sz;
1537 if (vlen > count) {
1538 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1539 goto copy;
1540 vlen = count;
1541 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001542
Christopher Fauletb2e84162018-12-06 11:39:49 +01001543 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001544 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001545 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001546
1547 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001548 h1m_init_req(h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001549 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +02001550 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001551 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001552 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001553 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 goto copy;
1555 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001556 if (sl->flags & HTX_SL_F_BODYLESS)
1557 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001558 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001559 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001560
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001562 h1m_init_res(h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001563 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +02001564 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001565 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001566 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001567 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001568 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001569 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001570 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001571 if (sl->info.res.status < 200 &&
1572 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1573 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001574 h1m->state = H1_MSG_HDR_FIRST;
1575 break;
1576
1577 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001578 h1m->state = H1_MSG_HDR_NAME;
1579 n = htx_get_blk_name(chn_htx, blk);
1580 v = htx_get_blk_value(chn_htx, blk);
1581
1582 if (isteqi(n, ist("transfer-encoding")))
1583 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001584 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001585 /* Only skip C-L header with invalid value. */
1586 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001587 goto skip_hdr;
1588 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001589 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001590 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001591 if (!v.len)
1592 goto skip_hdr;
1593 }
1594
Christopher Fauletc59ff232018-12-03 13:58:44 +01001595 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001596 goto copy;
1597 skip_hdr:
1598 h1m->state = H1_MSG_HDR_L2_LWS;
1599 break;
1600
Christopher Faulet9768c262018-10-22 09:34:31 +02001601 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001602 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001603 /* There is no "Connection:" header and
1604 * it the conn_mode must be
1605 * processed. So do it */
1606 n = ist("Connection");
1607 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001608 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001609 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001610 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001611 goto copy;
1612 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001613 process_conn_mode = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001614 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001615
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001616 if ((h1s->meth != HTTP_METH_CONNECT &&
1617 (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 +01001618 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1619 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1620 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1621 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1622 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001623 /* chunking needed but header not seen */
1624 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1625 goto copy;
1626 h1m->flags |= H1_MF_CHNK;
1627 }
1628
Christopher Faulet9768c262018-10-22 09:34:31 +02001629 h1m->state = H1_MSG_LAST_LF;
1630 if (!chunk_memcat(tmp, "\r\n", 2))
1631 goto copy;
1632
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001633 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1634 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1635 h1_set_req_tunnel_mode(h1s);
1636 }
1637 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1638 /* a successfull reply to a CONNECT or a protocol switching is sent
1639 * to the client . Switch the response to tunnel mode. */
1640 h1_set_res_tunnel_mode(h1s);
1641 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001642 else if ((h1m->flags & H1_MF_RESP) &&
1643 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1644 h1m_init_res(&h1s->res);
1645 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1646 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001647 else
1648 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 break;
1650
1651 case HTX_BLK_DATA:
1652 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001653 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001654 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001655 goto copy;
1656 break;
1657
1658 case HTX_BLK_EOD:
1659 if (!chunk_memcat(tmp, "0\r\n", 3))
1660 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001661 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001662 h1m->state = H1_MSG_TRAILERS;
1663 break;
1664
1665 case HTX_BLK_TLR:
Willy Tarreau34d23482019-01-03 17:46:56 +01001666 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001667 if (!chunk_memcat(tmp, "0\r\n", 3))
1668 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001669 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001670 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001671 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001672 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001673 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001675 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001676 break;
1677
1678 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001679 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001680 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001681 if (!chunk_memcat(tmp, "0\r\n", 3))
1682 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001683 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001684 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001685 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001686 if (!chunk_memcat(tmp, "\r\n", 2))
1687 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001688 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001689 }
1690 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 h1m->state = H1_MSG_DONE;
1692 break;
1693
Christopher Faulet9768c262018-10-22 09:34:31 +02001694 default:
1695 h1m->flags |= errflag;
1696 break;
1697 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001698 total += vlen;
1699 count -= vlen;
1700 if (sz == vlen)
1701 blk = htx_remove_blk(chn_htx, blk);
1702 else {
1703 htx_cut_data_blk(chn_htx, blk, vlen);
1704 break;
1705 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001706 }
1707
Christopher Faulet9768c262018-10-22 09:34:31 +02001708 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001709 /* when the output buffer is empty, tmp shares the same area so that we
1710 * only have to update pointers and lengths.
1711 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001712 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001713 h1c->obuf.data = tmp->data;
1714 else
1715 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001716
Willy Tarreau3815b222018-12-11 19:50:43 +01001717 htx_to_buf(chn_htx, buf);
1718 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001719 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001720 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001721 end:
1722 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001723}
1724
Christopher Faulet51dbc942018-09-13 09:05:15 +02001725/*********************************************************/
1726/* functions below are I/O callbacks from the connection */
1727/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001728static void h1_wake_stream_for_recv(struct h1s *h1s)
1729{
1730 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001731 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001732 tasklet_wakeup(h1s->recv_wait->task);
1733 h1s->recv_wait = NULL;
1734 }
1735}
1736static void h1_wake_stream_for_send(struct h1s *h1s)
1737{
1738 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001739 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001740 tasklet_wakeup(h1s->send_wait->task);
1741 h1s->send_wait = NULL;
1742 }
1743}
1744
Christopher Faulet51dbc942018-09-13 09:05:15 +02001745/*
1746 * Attempt to read data, and subscribe if none available
1747 */
1748static int h1_recv(struct h1c *h1c)
1749{
1750 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001751 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001752 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001753 int rcvd = 0;
1754
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001755 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001756 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001757
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001758 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1759 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1760 return 0;
1761 }
1762
Olivier Houchard75159a92018-12-03 18:46:09 +01001763 if (!h1_recv_allowed(h1c)) {
1764 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001765 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001766 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001767
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001768 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1769 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001770 goto end;
1771 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001772
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001773 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001774 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001775 h1_wake_stream_for_recv(h1s);
1776 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001777 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001778 }
1779
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001780 /*
1781 * If we only have a small amount of data, realign it,
1782 * it's probably cheaper than doing 2 recv() calls.
1783 */
1784 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1785 b_slow_realign(&h1c->ibuf, trash.area, 0);
1786
Willy Tarreau45f2b892018-12-05 07:59:27 +01001787 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001788 if (max) {
1789 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001790
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001791 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001792 if (!b_data(&h1c->ibuf)) {
1793 /* try to pre-align the buffer like the rxbufs will be
1794 * to optimize memory copies.
1795 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001796 h1c->ibuf.head = sizeof(struct htx);
1797 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001798 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001799 }
Christopher Faulet47365272018-10-31 17:40:50 +01001800 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001801 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001802 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001803 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001804 if (h1s->csinfo.t_idle == -1)
1805 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1806 }
Christopher Faulet47365272018-10-31 17:40:50 +01001807 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001808
Christopher Fauletcf56b992018-12-11 16:12:31 +01001809 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001810 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001811 goto end;
1812 }
1813
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001814 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001815
Christopher Faulet9768c262018-10-22 09:34:31 +02001816 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001817 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1818 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001819
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001820 if (conn_xprt_read0_pending(conn) && h1s) {
1821 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001822 rcvd = 1;
1823 }
1824
Christopher Faulet51dbc942018-09-13 09:05:15 +02001825 if (!b_data(&h1c->ibuf))
1826 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001827 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001828 h1c->flags |= H1C_F_IN_FULL;
1829 return rcvd;
1830}
1831
1832
1833/*
1834 * Try to send data if possible
1835 */
1836static int h1_send(struct h1c *h1c)
1837{
1838 struct connection *conn = h1c->conn;
1839 unsigned int flags = 0;
1840 size_t ret;
1841 int sent = 0;
1842
1843 if (conn->flags & CO_FL_ERROR)
1844 return 0;
1845
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001846 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001847 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001848 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001849 return 0;
1850 }
1851
Christopher Faulet51dbc942018-09-13 09:05:15 +02001852 if (!b_data(&h1c->obuf))
1853 goto end;
1854
1855 if (h1c->flags & H1C_F_OUT_FULL)
1856 flags |= CO_SFL_MSG_MORE;
1857
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001858 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001859 if (ret > 0) {
1860 h1c->flags &= ~H1C_F_OUT_FULL;
1861 b_del(&h1c->obuf, ret);
1862 sent = 1;
1863 }
1864
Christopher Faulet145aa472018-12-06 10:56:20 +01001865 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1866 /* error or output closed, nothing to send, clear the buffer to release it */
1867 b_reset(&h1c->obuf);
1868 }
1869
Christopher Faulet51dbc942018-09-13 09:05:15 +02001870 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001871 if (!(h1c->flags & H1C_F_OUT_FULL))
1872 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001873
Christopher Faulet51dbc942018-09-13 09:05:15 +02001874 /* We're done, no more to send */
1875 if (!b_data(&h1c->obuf)) {
1876 h1_release_buf(h1c, &h1c->obuf);
1877 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001878 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001879 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001880 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001881 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001882
1883 return sent;
1884}
1885
Christopher Faulet51dbc942018-09-13 09:05:15 +02001886
1887/* callback called on any event by the connection handler.
1888 * It applies changes and returns zero, or < 0 if it wants immediate
1889 * destruction of the connection.
1890 */
1891static int h1_process(struct h1c * h1c)
1892{
1893 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001894 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001895
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001896 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001897 return -1;
1898
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001899 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001900 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1901 goto end;
1902 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001903 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001904 }
1905
Christopher Fauletfeb11742018-11-29 15:12:34 +01001906 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001907 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001908 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001909 conn_xprt_read0_pending(conn))
1910 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001911 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001912 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001913 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001914 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001915 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001916 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001917 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001918 }
1919
Christopher Fauletfeb11742018-11-29 15:12:34 +01001920 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1921 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1922
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001923 if (conn_xprt_read0_pending(conn))
1924 h1s->flags |= H1S_F_REOS;
1925
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001926 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001927 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001928 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001929 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001930 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01001931 h1s->cs->data_cb->wake(h1s->cs);
1932 }
Christopher Faulet47365272018-10-31 17:40:50 +01001933 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001934 if (h1c->task) {
1935 h1c->task->expire = TICK_ETERNITY;
1936 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001937 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 +01001938 ? h1c->shut_timeout
1939 : h1c->timeout));
1940 task_queue(h1c->task);
1941 }
1942 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001943 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001944
1945 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02001946 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01001947 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001948}
1949
1950static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1951{
1952 struct h1c *h1c = ctx;
1953 int ret = 0;
1954
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001955 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001956 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001957 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001958 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001959 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001960 h1_process(h1c);
1961 return NULL;
1962}
1963
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001964static void h1_reset(struct connection *conn)
1965{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001966 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001967
1968 /* Reset the flags, and let the mux know we're waiting for a connection */
1969 h1c->flags = H1C_F_CS_WAIT_CONN;
1970}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001971
1972static int h1_wake(struct connection *conn)
1973{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001974 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001975 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001976
Christopher Faulet539e0292018-11-19 10:40:09 +01001977 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001978 ret = h1_process(h1c);
1979 if (ret == 0) {
1980 struct h1s *h1s = h1c->h1s;
1981
1982 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1983 ret = h1s->cs->data_cb->wake(h1s->cs);
1984 }
1985 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986}
1987
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001988/* Connection timeout management. The principle is that if there's no receipt
1989 * nor sending for a certain amount of time, the connection is closed.
1990 */
1991static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1992{
1993 struct h1c *h1c = context;
1994 int expired = tick_is_expired(t->expire, now_ms);
1995
1996 if (!expired && h1c)
1997 return t;
1998
Olivier Houchard3f795f72019-04-17 22:51:06 +02001999 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002000
2001 if (!h1c) {
2002 /* resources were already deleted */
2003 return NULL;
2004 }
2005
2006 h1c->task = NULL;
2007 /* If a stream is still attached to the mux, just set an error and wait
2008 * for the stream's timeout. Otherwise, release the mux. This is only ok
2009 * because same timeouts are used.
2010 */
2011 if (h1c->h1s && h1c->h1s->cs)
2012 h1c->flags |= H1C_F_CS_ERROR;
2013 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002014 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002015 return NULL;
2016}
2017
Christopher Faulet51dbc942018-09-13 09:05:15 +02002018/*******************************************/
2019/* functions below are used by the streams */
2020/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002021
Christopher Faulet51dbc942018-09-13 09:05:15 +02002022/*
2023 * Attach a new stream to a connection
2024 * (Used for outgoing connections)
2025 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002026static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002027{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002028 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002029 struct conn_stream *cs = NULL;
2030 struct h1s *h1s;
2031
2032 if (h1c->flags & H1C_F_CS_ERROR)
2033 goto end;
2034
2035 cs = cs_new(h1c->conn);
2036 if (!cs)
2037 goto end;
2038
Olivier Houchardf502aca2018-12-14 19:42:40 +01002039 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002040 if (h1s == NULL)
2041 goto end;
2042
2043 return cs;
2044 end:
2045 cs_free(cs);
2046 return NULL;
2047}
2048
2049/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2050 * this mux, it's easy as we can only store a single conn_stream.
2051 */
2052static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2053{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002054 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002055 struct h1s *h1s = h1c->h1s;
2056
2057 if (h1s)
2058 return h1s->cs;
2059
2060 return NULL;
2061}
2062
Christopher Faulet73c12072019-04-08 11:23:22 +02002063static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002064{
Christopher Faulet73c12072019-04-08 11:23:22 +02002065 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002067 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002068 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002069}
2070
2071/*
2072 * Detach the stream from the connection and possibly release the connection.
2073 */
2074static void h1_detach(struct conn_stream *cs)
2075{
2076 struct h1s *h1s = cs->ctx;
2077 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002078 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002079 int has_keepalive;
2080 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002081
2082 cs->ctx = NULL;
2083 if (!h1s)
2084 return;
2085
Olivier Houchardf502aca2018-12-14 19:42:40 +01002086 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002087 h1c = h1s->h1c;
2088 h1s->cs = NULL;
2089
Olivier Houchard8a786902018-12-15 16:05:40 +01002090 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2091 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2092 h1s_destroy(h1s);
2093
2094 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002095 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002096 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002097 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002098 h1c->conn->flags |= CO_FL_PRIVATE;
2099
Olivier Houchard44d59142018-12-13 18:46:22 +01002100 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002101 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002102 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2103 h1c->conn->owner = NULL;
2104 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2105 /* The server doesn't want it, let's kill the connection right away */
2106 h1c->conn->mux->destroy(h1c->conn);
2107 else
2108 tasklet_wakeup(h1c->wait_event.task);
2109 return;
2110
2111 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002112 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002113 if (h1c->conn->owner == sess) {
2114 int ret = session_check_idle_conn(sess, h1c->conn);
2115 if (ret == -1)
2116 /* The connection got destroyed, let's leave */
2117 return;
2118 else if (ret == 1) {
2119 /* The connection was added to the server list,
2120 * wake the task so we can subscribe to events
2121 */
2122 tasklet_wakeup(h1c->wait_event.task);
2123 return;
2124 }
2125 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002126 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002127 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002128 struct server *srv = objt_server(h1c->conn->target);
2129
2130 if (srv) {
2131 if (h1c->conn->flags & CO_FL_PRIVATE)
2132 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002133 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002134 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2135 else
2136 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2137 }
2138 }
2139 }
2140
Christopher Faulet51dbc942018-09-13 09:05:15 +02002141 /* We don't want to close right now unless the connection is in error */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002142 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002143 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002144 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002145 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002146 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002147 if (h1c->task) {
2148 h1c->task->expire = TICK_ETERNITY;
2149 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002150 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 +01002151 ? h1c->shut_timeout
2152 : h1c->timeout));
2153 task_queue(h1c->task);
2154 }
2155 }
2156 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002157}
2158
2159
2160static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2161{
2162 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002163 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002164
2165 if (!h1s)
2166 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002167 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002168
Christopher Faulet7f366362019-04-08 10:51:20 +02002169 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2170 goto do_shutr;
2171
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002172 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002173 return;
2174
Christopher Faulet7f366362019-04-08 10:51:20 +02002175 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002176 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2177 if (cs->flags & CS_FL_SHR)
2178 return;
2179 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002180 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2181 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002182 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 +02002183 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002184}
2185
2186static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2187{
2188 struct h1s *h1s = cs->ctx;
2189 struct h1c *h1c;
2190
2191 if (!h1s)
2192 return;
2193 h1c = h1s->h1c;
2194
Christopher Faulet7f366362019-04-08 10:51:20 +02002195 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2196 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002197
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002198 if ((h1c->flags & H1C_F_UPG_H2C) ||
2199 ((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 +02002200 return;
2201
Christopher Faulet7f366362019-04-08 10:51:20 +02002202 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002203 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2204 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2205 return;
2206
Christopher Faulet666a0c42019-01-08 11:12:04 +01002207 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208}
2209
Christopher Faulet666a0c42019-01-08 11:12:04 +01002210static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002211{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002212 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002213
Christopher Faulet666a0c42019-01-08 11:12:04 +01002214 conn_xprt_shutw(conn);
2215 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2216 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2217 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002218}
2219
2220/* Called from the upper layer, to unsubscribe to events */
2221static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2222{
2223 struct wait_event *sw;
2224 struct h1s *h1s = cs->ctx;
2225
2226 if (!h1s)
2227 return 0;
2228
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002229 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002230 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002231 BUG_ON(h1s->recv_wait != sw);
2232 sw->events &= ~SUB_RETRY_RECV;
2233 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002234 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002235 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002236 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002237 BUG_ON(h1s->send_wait != sw);
2238 sw->events &= ~SUB_RETRY_SEND;
2239 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002240 }
2241 return 0;
2242}
2243
2244/* Called from the upper layer, to subscribe to events, such as being able to send */
2245static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2246{
2247 struct wait_event *sw;
2248 struct h1s *h1s = cs->ctx;
2249
2250 if (!h1s)
2251 return -1;
2252
2253 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002254 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002255 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002256 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2257 sw->events |= SUB_RETRY_RECV;
2258 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002259 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002260 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002261 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002262 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2263 sw->events |= SUB_RETRY_SEND;
2264 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002265 return 0;
2266 default:
2267 break;
2268 }
2269 return -1;
2270}
2271
2272/* Called from the upper layer, to receive data */
2273static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2274{
2275 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002276 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002277 size_t ret = 0;
2278
Christopher Faulet539e0292018-11-19 10:40:09 +01002279 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002280 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002281
Christopher Faulete18777b2019-04-16 16:46:36 +02002282 if (flags & CO_RFL_BUF_FLUSH) {
2283 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2284
2285 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2286 h1s->flags |= H1S_F_BUF_FLUSH;
2287 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002288 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2289 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002290 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002291 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002292 }
2293 return ret;
2294}
2295
2296
2297/* Called from the upper layer, to send data */
2298static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2299{
2300 struct h1s *h1s = cs->ctx;
2301 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002302 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002303
2304 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002305 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002306
2307 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002308 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2309 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002310
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002311 while (total != count) {
2312 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002313
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002314 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2315 ret = h1_process_output(h1c, buf, count);
2316 if (!ret)
2317 break;
2318 total += ret;
2319 if (!h1_send(h1c))
2320 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002321 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002322
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002323 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002324}
2325
Willy Tarreaue5733232019-05-22 19:24:06 +02002326#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002327/* Send and get, using splicing */
2328static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2329{
2330 struct h1s *h1s = cs->ctx;
2331 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2332 int ret = 0;
2333
Christopher Faulet1146f972019-05-29 14:35:24 +02002334 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002335 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002336 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2337 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002338 goto end;
2339 }
2340
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002341 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002342 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002343 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002344 }
2345
2346 h1s->flags &= ~H1S_F_BUF_FLUSH;
2347 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002348 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2349 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002350 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002351 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002352 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002353 if (!h1m->curr_len)
2354 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2355 }
2356
Christopher Faulet1be55f92018-10-02 15:59:23 +02002357 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002358 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002359 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002360 if (!pipe->data)
2361 cs->flags |= CS_FL_EOS;
2362 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002363 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002364}
2365
2366static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2367{
2368 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002369 int ret = 0;
2370
2371 if (b_data(&h1s->h1c->obuf))
2372 goto end;
2373
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002374 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002375 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002376 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002377 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002378 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002379 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002380 return ret;
2381}
2382#endif
2383
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002384/* for debugging with CLI's "show fd" command */
2385static void h1_show_fd(struct buffer *msg, struct connection *conn)
2386{
2387 struct h1c *h1c = conn->ctx;
2388 struct h1s *h1s = h1c->h1s;
2389
Christopher Fauletf376a312019-01-04 15:16:06 +01002390 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2391 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002392 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2393 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2394 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2395 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2396
2397 if (h1s) {
2398 char *method;
2399
2400 if (h1s->meth < HTTP_METH_OTHER)
2401 method = http_known_methods[h1s->meth].ptr;
2402 else
2403 method = "UNKNOWN";
2404 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2405 " .meth=%s status=%d",
2406 h1s, h1s->flags,
2407 h1m_state_str(h1s->req.state),
2408 h1m_state_str(h1s->res.state), method, h1s->status);
2409 if (h1s->cs)
2410 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2411 h1s->cs->flags, h1s->cs->data);
2412 }
2413}
2414
Christopher Faulet51dbc942018-09-13 09:05:15 +02002415/****************************************/
2416/* MUX initialization and instanciation */
2417/****************************************/
2418
2419/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002420static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002421 .init = h1_init,
2422 .wake = h1_wake,
2423 .attach = h1_attach,
2424 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002425 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002426 .detach = h1_detach,
2427 .destroy = h1_destroy,
2428 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002429 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002430 .rcv_buf = h1_rcv_buf,
2431 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002432#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002433 .rcv_pipe = h1_rcv_pipe,
2434 .snd_pipe = h1_snd_pipe,
2435#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002436 .subscribe = h1_subscribe,
2437 .unsubscribe = h1_unsubscribe,
2438 .shutr = h1_shutr,
2439 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002440 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002441 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002442 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002443 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002444};
2445
2446
2447/* this mux registers default HTX proto */
2448static struct mux_proto_list mux_proto_htx =
2449{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2450
Willy Tarreau0108d902018-11-25 19:14:37 +01002451INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2452
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453/*
2454 * Local variables:
2455 * c-indent-level: 8
2456 * c-basic-offset: 8
2457 * End:
2458 */