blob: c25a0b9cb75b849801854ee84700c4922e66c58c [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Christopher Faulet0ef372a2019-04-08 10:57:20 +020015#include <common/h2.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Christopher Faulet1be55f92018-10-02 15:59:23 +020019#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020020#include <types/proxy.h>
21#include <types/session.h>
22
Christopher Faulet51dbc942018-09-13 09:05:15 +020023#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020024#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020025#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010026#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020027#include <proto/stream.h>
28#include <proto/stream_interface.h>
29
30/*
31 * H1 Connection flags (32 bits)
32 */
33#define H1C_F_NONE 0x00000000
34
35/* Flags indicating why writing output data are blocked */
36#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
37#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
38/* 0x00000004 - 0x00000008 unused */
39
40/* Flags indicating why reading input data are blocked. */
41#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
42#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010043#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010044/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020045
46#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
47#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010048#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020049#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020050
Christopher Fauletf2824e62018-10-01 12:12:37 +020051#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020052#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020053
Christopher Faulet51dbc942018-09-13 09:05:15 +020054/*
55 * H1 Stream flags (32 bits)
56 */
Christopher Faulet129817b2018-09-20 16:14:40 +020057#define H1S_F_NONE 0x00000000
58#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020059#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
60#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020061#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020062#define H1S_F_WANT_KAL 0x00000010
63#define H1S_F_WANT_TUN 0x00000020
64#define H1S_F_WANT_CLO 0x00000040
65#define H1S_F_WANT_MSK 0x00000070
66#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010067#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010068#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010069#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau62038152019-08-23 09:29:29 +020070#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
71/* 0x00002000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020072#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073
74/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020075struct h1c {
76 struct connection *conn;
77 struct proxy *px;
78 uint32_t flags; /* Connection flags: H1C_F_* */
79
80 struct buffer ibuf; /* Input buffer to store data before parsing */
81 struct buffer obuf; /* Output buffer to store data after reformatting */
82
83 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
84 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
85
86 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010087 struct task *task; /* timeout management task */
88 int timeout; /* idle timeout duration in ticks */
89 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020090};
91
92/* H1 stream descriptor */
93struct h1s {
94 struct h1c *h1c;
95 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010096 struct cs_info csinfo; /* CS info, only used for client connections */
97 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098
Christopher Faulet51dbc942018-09-13 09:05:15 +020099 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
100 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200101
Olivier Houchardf502aca2018-12-14 19:42:40 +0100102 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200103 struct h1m req;
104 struct h1m res;
105
106 enum http_meth_t meth; /* HTTP resquest method */
107 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200108};
109
110/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100111DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
112DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200113
Christopher Faulet51dbc942018-09-13 09:05:15 +0200114static int h1_recv(struct h1c *h1c);
115static int h1_send(struct h1c *h1c);
116static int h1_process(struct h1c *h1c);
117static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100118static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100119static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200120
121/*****************************************************/
122/* functions below are for dynamic buffer management */
123/*****************************************************/
124/*
125 * Indicates whether or not the we may call the h1_recv() function to
126 * attempt to receive data into the buffer and/or parse pending data. The
127 * condition is a bit complex due to some API limits for now. The rules are the
128 * following :
129 * - if an error or a shutdown was detected on the connection and the buffer
130 * is empty, we must not attempt to receive
131 * - if the input buffer failed to be allocated, we must not try to receive
132 * and we know there is nothing pending
133 * - if no flag indicates a blocking condition, we may attempt to receive,
134 * regardless of whether the input buffer is full or not, so that only de
135 * receiving part decides whether or not to block. This is needed because
136 * the connection API indeed prevents us from re-enabling receipt that is
137 * already enabled in a polled state, so we must always immediately stop as
138 * soon as the mux can't proceed so as never to hit an end of read with data
139 * pending in the buffers.
140 * - otherwise must may not attempt to receive
141 */
142static inline int h1_recv_allowed(const struct h1c *h1c)
143{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100144 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200145 return 0;
146
Christopher Fauletcf56b992018-12-11 16:12:31 +0100147 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
148 return 0;
149
Christopher Fauletcb55f482018-12-10 11:56:47 +0100150 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200151 return 1;
152
153 return 0;
154}
155
156/*
157 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
158 * flags are used to figure what buffer was requested. It returns 1 if the
159 * allocation succeeds, in which case the connection is woken up, or 0 if it's
160 * impossible to wake up and we prefer to be woken up later.
161 */
162static int h1_buf_available(void *target)
163{
164 struct h1c *h1c = target;
165
166 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
167 h1c->flags &= ~H1C_F_IN_ALLOC;
168 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200169 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200170 return 1;
171 }
172
173 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
174 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200175 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200176 return 1;
177 }
178
Christopher Faulet51dbc942018-09-13 09:05:15 +0200179 return 0;
180}
181
182/*
183 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
184 */
185static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
186{
187 struct buffer *buf = NULL;
188
189 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
190 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
191 h1c->buf_wait.target = h1c;
192 h1c->buf_wait.wakeup_cb = h1_buf_available;
193 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
194 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
195 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
196 __conn_xprt_stop_recv(h1c->conn);
197 }
198 return buf;
199}
200
201/*
202 * Release a buffer, if any, and try to wake up entities waiting in the buffer
203 * wait queue.
204 */
205static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
206{
207 if (bptr->size) {
208 b_free(bptr);
209 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
210 }
211}
212
Willy Tarreau00f18a32019-01-26 12:19:01 +0100213/* returns the number of streams in use on a connection to figure if it's
214 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
215 * as the caller will want to know if it was the last one after a detach().
216 */
217static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200218{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100219 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200220
Willy Tarreau00f18a32019-01-26 12:19:01 +0100221 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200222}
223
Willy Tarreau00f18a32019-01-26 12:19:01 +0100224/* returns the number of streams still available on a connection */
225static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100226{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100227 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100228}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200229
Willy Tarreau00f18a32019-01-26 12:19:01 +0100230
Christopher Faulet51dbc942018-09-13 09:05:15 +0200231/*****************************************************************/
232/* functions below are dedicated to the mux setup and management */
233/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200234
235/* returns non-zero if there are input data pending for stream h1s. */
236static inline size_t h1s_data_pending(const struct h1s *h1s)
237{
238 const struct h1m *h1m;
239
240 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
241 if (h1m->state == H1_MSG_DONE)
242 return 0; // data not for this stream (e.g. pipelining)
243
244 return b_data(&h1s->h1c->ibuf);
245}
246
Christopher Faulet47365272018-10-31 17:40:50 +0100247static struct conn_stream *h1s_new_cs(struct h1s *h1s)
248{
249 struct conn_stream *cs;
250
251 cs = cs_new(h1s->h1c->conn);
252 if (!cs)
253 goto err;
254 h1s->cs = cs;
255 cs->ctx = h1s;
256
257 if (h1s->flags & H1S_F_NOT_FIRST)
258 cs->flags |= CS_FL_NOT_FIRST;
259
260 if (stream_create_from_cs(cs) < 0)
261 goto err;
262 return cs;
263
264 err:
265 cs_free(cs);
266 h1s->cs = NULL;
267 return NULL;
268}
269
Olivier Houchardf502aca2018-12-14 19:42:40 +0100270static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200271{
272 struct h1s *h1s;
273
274 h1s = pool_alloc(pool_head_h1s);
275 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100276 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200277
278 h1s->h1c = h1c;
279 h1c->h1s = h1s;
280
Olivier Houchardf502aca2018-12-14 19:42:40 +0100281 h1s->sess = sess;
282
Christopher Faulet51dbc942018-09-13 09:05:15 +0200283 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100284 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200285
286 h1s->recv_wait = NULL;
287 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200288
289 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100290 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200291
Christopher Faulet129817b2018-09-20 16:14:40 +0200292 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100293 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200294
295 h1s->status = 0;
296 h1s->meth = HTTP_METH_OTHER;
297
Christopher Faulet47365272018-10-31 17:40:50 +0100298 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
299 h1s->flags |= H1S_F_NOT_FIRST;
300 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
301
Christopher Faulet129817b2018-09-20 16:14:40 +0200302 if (!conn_is_back(h1c->conn)) {
303 if (h1c->px->options2 & PR_O2_REQBUG_OK)
304 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200305
306 /* For frontend connections we should always have a session */
307 if (!sess)
308 sess = h1c->conn->owner;
309
Dave Pirottecf67ec52019-07-10 13:57:38 +0000310 /* Timers for subsequent sessions on the same HTTP 1.x connection
311 * measure from `now`, not from the connection accept time */
312 if (h1s->flags & H1S_F_NOT_FIRST) {
313 h1s->csinfo.create_date = date;
314 h1s->csinfo.tv_create = now;
315 h1s->csinfo.t_handshake = 0;
316 h1s->csinfo.t_idle = -1;
317 }
318 else {
319 h1s->csinfo.create_date = sess->accept_date;
320 h1s->csinfo.tv_create = sess->tv_accept;
321 h1s->csinfo.t_handshake = sess->t_handshake;
322 h1s->csinfo.t_idle = -1;
323 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200324 }
325 else {
326 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
327 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200328
Christopher Fauletfeb11742018-11-29 15:12:34 +0100329 h1s->csinfo.create_date = date;
330 h1s->csinfo.tv_create = now;
331 h1s->csinfo.t_handshake = 0;
332 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200333 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100334
Christopher Faulete9b70722019-04-08 10:46:02 +0200335 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
336 * create a new one.
337 */
338 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200339 cs->ctx = h1s;
340 h1s->cs = cs;
341 }
Christopher Faulet47365272018-10-31 17:40:50 +0100342 else {
343 cs = h1s_new_cs(h1s);
344 if (!cs)
345 goto fail;
346 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100348
349 fail:
350 pool_free(pool_head_h1s, h1s);
351 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200352}
353
354static void h1s_destroy(struct h1s *h1s)
355{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200356 if (h1s) {
357 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200358
Christopher Fauletf2824e62018-10-01 12:12:37 +0200359 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360
Christopher Fauletf2824e62018-10-01 12:12:37 +0200361 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100362 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200363 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100364 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200365
Christopher Fauletcb55f482018-12-10 11:56:47 +0100366 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100367 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
368 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
369 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200370 pool_free(pool_head_h1s, h1s);
371 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372}
373
Christopher Fauletfeb11742018-11-29 15:12:34 +0100374static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
375{
376 struct h1s *h1s = cs->ctx;
377
378 if (h1s && !conn_is_back(cs->conn))
379 return &h1s->csinfo;
380 return NULL;
381}
382
Christopher Faulet51dbc942018-09-13 09:05:15 +0200383/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200384 * Initialize the mux once it's attached. It is expected that conn->ctx points
385 * to the existing conn_stream (for outgoing connections or for incoming onces
386 * during a mux upgrade) or NULL (for incoming ones during the connexion
387 * establishment). <input> is always used as Input buffer and may contain
388 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
389 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200390 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200391static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
392 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200393{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200394 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100395 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200396
397 h1c = pool_alloc(pool_head_h1c);
398 if (!h1c)
399 goto fail_h1c;
400 h1c->conn = conn;
401 h1c->px = proxy;
402
403 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200404 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200405 h1c->obuf = BUF_NULL;
406 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200407 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200408
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200410 h1c->wait_event.tasklet = tasklet_new();
411 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200413 h1c->wait_event.tasklet->process = h1_io_cb;
414 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100415 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200416
Christopher Faulete9b70722019-04-08 10:46:02 +0200417 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100418 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
419 if (tick_isset(proxy->timeout.serverfin))
420 h1c->shut_timeout = proxy->timeout.serverfin;
421 } else {
422 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
423 if (tick_isset(proxy->timeout.clientfin))
424 h1c->shut_timeout = proxy->timeout.clientfin;
425 }
426 if (tick_isset(h1c->timeout)) {
427 t = task_new(tid_bit);
428 if (!t)
429 goto fail;
430
431 h1c->task = t;
432 t->process = h1_timeout_task;
433 t->context = h1c;
434 t->expire = tick_add(now_ms, h1c->timeout);
435 }
436
Olivier Houchard985234d2019-06-13 17:54:33 +0200437 if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200438 h1c->flags |= H1C_F_CS_WAIT_CONN;
439
Christopher Fauletf2824e62018-10-01 12:12:37 +0200440 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100441 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200442 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200443
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100444 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200445
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100446
447 if (t)
448 task_queue(t);
449
Christopher Faulet51dbc942018-09-13 09:05:15 +0200450 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200451 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200452
453 /* mux->wake will be called soon to complete the operation */
454 return 0;
455
456 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200457 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200458 if (h1c->wait_event.tasklet)
459 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460 pool_free(pool_head_h1c, h1c);
461 fail_h1c:
462 return -1;
463}
464
Christopher Faulet73c12072019-04-08 11:23:22 +0200465/* release function. This one should be called to free all resources allocated
466 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200467 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200468static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200469{
Christopher Faulet61840e72019-04-15 09:33:32 +0200470 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200471
Christopher Faulet51dbc942018-09-13 09:05:15 +0200472 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200473 /* The connection must be aattached to this mux to be released */
474 if (h1c->conn && h1c->conn->ctx == h1c)
475 conn = h1c->conn;
476
477 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200478 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard13f556e2019-07-03 13:08:18 +0200479 /* Make sure we're no longer subscribed to anything */
480 if (h1c->wait_event.events)
481 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
482 h1c->wait_event.events, &h1c->wait_event);
Olivier Houchard45c44372019-06-11 14:06:23 +0200483 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200484 /* connection successfully upgraded to H2, this
485 * mux was already released */
486 return;
487 }
488 sess_log(conn->owner); /* Log if the upgrade failed */
489 }
490
Olivier Houchard45c44372019-06-11 14:06:23 +0200491
Christopher Faulet51dbc942018-09-13 09:05:15 +0200492 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
493 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
494 LIST_DEL(&h1c->buf_wait.list);
495 LIST_INIT(&h1c->buf_wait.list);
496 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
497 }
498
499 h1_release_buf(h1c, &h1c->ibuf);
500 h1_release_buf(h1c, &h1c->obuf);
501
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100502 if (h1c->task) {
503 h1c->task->context = NULL;
504 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
505 h1c->task = NULL;
506 }
507
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200508 if (h1c->wait_event.tasklet)
509 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200510
Christopher Fauletf2824e62018-10-01 12:12:37 +0200511 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200512 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100513 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200514 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200515 pool_free(pool_head_h1c, h1c);
516 }
517
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200518 if (conn) {
519 conn->mux = NULL;
520 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200521
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200522 conn_stop_tracking(conn);
523 conn_full_close(conn);
524 if (conn->destroy_cb)
525 conn->destroy_cb(conn);
526 conn_free(conn);
527 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200528}
529
530/******************************************************/
531/* functions below are for the H1 protocol processing */
532/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200533/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
534 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200535 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100536static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200537{
Christopher Faulet570d1612018-11-26 11:13:57 +0100538 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200539
Christopher Faulet570d1612018-11-26 11:13:57 +0100540 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200541 (*(p + 5) > '1' ||
542 (*(p + 5) == '1' && *(p + 7) >= '1')))
543 h1m->flags |= H1_MF_VER_11;
544}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200545
Christopher Faulet9768c262018-10-22 09:34:31 +0200546/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
547 * greater or equal to 1.1
548 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100549static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200550{
Christopher Faulet570d1612018-11-26 11:13:57 +0100551 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200552
Christopher Faulet570d1612018-11-26 11:13:57 +0100553 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200554 (*(p + 5) > '1' ||
555 (*(p + 5) == '1' && *(p + 7) >= '1')))
556 h1m->flags |= H1_MF_VER_11;
557}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200558
Christopher Faulet9768c262018-10-22 09:34:31 +0200559/*
560 * Check the validity of the request version. If the version is valid, it
561 * returns 1. Otherwise, it returns 0.
562 */
563static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
564{
565 struct h1c *h1c = h1s->h1c;
566
567 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
568 * exactly one digit "." one digit. This check may be disabled using
569 * option accept-invalid-http-request.
570 */
571 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
572 if (sl.rq.v.len != 8)
573 return 0;
574
575 if (*(sl.rq.v.ptr + 4) != '/' ||
576 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
577 *(sl.rq.v.ptr + 6) != '.' ||
578 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
579 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200580 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200581 else if (!sl.rq.v.len) {
582 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200583
Christopher Faulet9768c262018-10-22 09:34:31 +0200584 /* RFC 1945 allows only GET for HTTP/0.9 requests */
585 if (sl.rq.meth != HTTP_METH_GET)
586 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200587
Christopher Faulet9768c262018-10-22 09:34:31 +0200588 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
589 if (!sl.rq.u.len)
590 return 0;
591
592 /* Add HTTP version */
593 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100594 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200595 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100596
597 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100598 ((*(sl.rq.v.ptr + 5) > '1') ||
599 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100600 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200601 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200602}
603
Christopher Faulet9768c262018-10-22 09:34:31 +0200604/*
605 * Check the validity of the response version. If the version is valid, it
606 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200607 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200608static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200609{
Christopher Faulet9768c262018-10-22 09:34:31 +0200610 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200611
Christopher Faulet9768c262018-10-22 09:34:31 +0200612 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
613 * exactly one digit "." one digit. This check may be disabled using
614 * option accept-invalid-http-request.
615 */
616 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
617 if (sl.st.v.len != 8)
618 return 0;
619
620 if (*(sl.st.v.ptr + 4) != '/' ||
621 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
622 *(sl.st.v.ptr + 6) != '.' ||
623 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
624 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200625 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100626
627 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100628 ((*(sl.st.v.ptr + 5) > '1') ||
629 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100630 h1m->flags |= H1_MF_VER_11;
631
Christopher Faulet9768c262018-10-22 09:34:31 +0200632 return 1;
633}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200634
635/* Deduce the connection mode of the client connection, depending on the
636 * configuration and the H1 message flags. This function is called twice, the
637 * first time when the request is parsed and the second time when the response
638 * is parsed.
639 */
640static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
641{
642 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200643
644 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100645 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200646 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100647 h1s->status == 101) {
648 /* Either we've established an explicit tunnel, or we're
649 * switching the protocol. In both cases, we're very unlikely to
650 * understand the next protocols. We have to switch to tunnel
651 * mode, so that we transfer the request and responses then let
652 * this protocol pass unmodified. When we later implement
653 * specific parsers for such protocols, we'll want to check the
654 * Upgrade header which contains information about that protocol
655 * for responses with status 101 (eg: see RFC2817 about TLS).
656 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200657 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100658 }
659 else if (h1s->flags & H1S_F_WANT_KAL) {
660 /* By default the client is in KAL mode. CLOSE mode mean
661 * it is imposed by the client itself. So only change
662 * KAL mode here. */
663 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
664 /* no length known or explicit close => close */
665 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
666 }
667 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
668 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
669 /* no explict keep-alive and option httpclose => close */
670 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
671 }
672 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673 }
674 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100675 /* Input direction: first pass */
676 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
677 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200678 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100679 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200680 }
681
682 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
683 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
684 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
685}
686
687/* Deduce the connection mode of the client connection, depending on the
688 * configuration and the H1 message flags. This function is called twice, the
689 * first time when the request is parsed and the second time when the response
690 * is parsed.
691 */
692static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
693{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100694 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100695 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100696 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200697
Christopher Fauletf2824e62018-10-01 12:12:37 +0200698 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100699 /* Input direction: second pass */
700
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100702 h1s->status == 101) {
703 /* Either we've established an explicit tunnel, or we're
704 * switching the protocol. In both cases, we're very unlikely to
705 * understand the next protocols. We have to switch to tunnel
706 * mode, so that we transfer the request and responses then let
707 * this protocol pass unmodified. When we later implement
708 * specific parsers for such protocols, we'll want to check the
709 * Upgrade header which contains information about that protocol
710 * for responses with status 101 (eg: see RFC2817 about TLS).
711 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200712 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100713 }
714 else if (h1s->flags & H1S_F_WANT_KAL) {
715 /* By default the server is in KAL mode. CLOSE mode mean
716 * it is imposed by haproxy itself. So only change KAL
717 * mode here. */
718 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
719 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
720 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
721 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
722 }
723 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724 }
Christopher Faulet70033782018-12-05 13:50:11 +0100725 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100726 /* Output direction: first pass */
727 if (h1m->flags & H1_MF_CONN_CLO) {
728 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100729 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100730 }
731 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
732 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
733 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
734 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
735 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
736 /* no explicit keep-alive option httpclose/server-close => close */
737 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
738 }
Christopher Faulet70033782018-12-05 13:50:11 +0100739 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200740
741 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
742 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
743 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200744}
745
Christopher Fauletb992af02019-03-28 15:42:24 +0100746static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200747{
748 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200749
750 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
751 * token is found
752 */
753 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200754 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200755
756 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100757 if (!(h1m->flags & H1_MF_VER_11))
758 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200759 }
760 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100761 if (h1m->flags & H1_MF_VER_11)
762 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200763 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200764}
765
Christopher Fauletb992af02019-03-28 15:42:24 +0100766static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200767{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200768 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
769 * token is found
770 */
771 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200772 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200773
774 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100775 if (!(h1m->flags & H1_MF_VER_11) ||
776 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
777 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200778 }
779 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100780 if (h1m->flags & H1_MF_VER_11)
781 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200782 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200783}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200784
Christopher Fauletb992af02019-03-28 15:42:24 +0100785static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200786{
Christopher Fauletb992af02019-03-28 15:42:24 +0100787 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200788 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100789 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200790 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200791}
792
Christopher Fauletb992af02019-03-28 15:42:24 +0100793static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
794{
795 if (!conn_is_back(h1s->h1c->conn))
796 h1_set_cli_conn_mode(h1s, h1m);
797 else
798 h1_set_srv_conn_mode(h1s, h1m);
799
800 if (!(h1m->flags & H1_MF_RESP))
801 h1_update_req_conn_value(h1s, h1m, conn_val);
802 else
803 h1_update_res_conn_value(h1s, h1m, conn_val);
804}
Christopher Faulete44769b2018-11-29 23:01:45 +0100805
806/* Append the description of what is present in error snapshot <es> into <out>.
807 * The description must be small enough to always fit in a buffer. The output
808 * buffer may be the trash so the trash must not be used inside this function.
809 */
810static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
811{
812 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100813 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
814 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
815 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
816 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
817 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
818 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100819}
820/*
821 * Capture a bad request or response and archive it in the proxy's structure.
822 * By default it tries to report the error position as h1m->err_pos. However if
823 * this one is not set, it will then report h1m->next, which is the last known
824 * parsing point. The function is able to deal with wrapping buffers. It always
825 * displays buffers as a contiguous area starting at buf->p. The direction is
826 * determined thanks to the h1m's flags.
827 */
828static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
829 struct h1m *h1m, struct buffer *buf)
830{
831 struct session *sess = h1c->conn->owner;
832 struct proxy *proxy = h1c->px;
833 struct proxy *other_end = sess->fe;
834 union error_snapshot_ctx ctx;
835
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100836 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100837 other_end = si_strm(h1s->cs->data)->be;
838
839 /* http-specific part now */
840 ctx.h1.state = h1m->state;
841 ctx.h1.c_flags = h1c->flags;
842 ctx.h1.s_flags = h1s->flags;
843 ctx.h1.m_flags = h1m->flags;
844 ctx.h1.m_clen = h1m->curr_len;
845 ctx.h1.m_blen = h1m->body_len;
846
847 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
848 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100849 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
850 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100851}
852
Christopher Fauletadb22202018-12-12 10:32:09 +0100853/* Emit the chunksize followed by a CRLF in front of data of the buffer
854 * <buf>. It goes backwards and starts with the byte before the buffer's
855 * head. The caller is responsible for ensuring there is enough room left before
856 * the buffer's head for the string.
857 */
858static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
859{
860 char *beg, *end;
861
862 beg = end = b_head(buf);
863 *--beg = '\n';
864 *--beg = '\r';
865 do {
866 *--beg = hextab[chksz & 0xF];
867 } while (chksz >>= 4);
868 buf->head -= (end - beg);
869 b_add(buf, end - beg);
870}
871
872/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
873 * ensuring there is enough room left in the buffer for the string. */
874static void h1_emit_chunk_crlf(struct buffer *buf)
875{
876 *(b_peek(buf, b_data(buf))) = '\r';
877 *(b_peek(buf, b_data(buf) + 1)) = '\n';
878 b_add(buf, 2);
879}
880
Christopher Faulet129817b2018-09-20 16:14:40 +0200881/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100882 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletac4778c2019-11-15 11:14:23 +0100883 * CONNECT requests. On the client side, if the response is not finished, the
884 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100885 */
886static void h1_set_req_tunnel_mode(struct h1s *h1s)
887{
888 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
889 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100890 if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100891 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100892 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
893 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
894 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
895 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100896}
897
898/*
899 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletac4778c2019-11-15 11:14:23 +0100900 * successfull replies to CONNECT requests or on protocol switching. In this
901 * last case, this function takes care to switch the request to tunnel mode if
902 * possible. On the server side, if the request is not finished, the mux is mark
903 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100904 */
905static void h1_set_res_tunnel_mode(struct h1s *h1s)
906{
Christopher Fauletac4778c2019-11-15 11:14:23 +0100907 /* On protocol switching, switch the request to tunnel mode if it is in
908 * DONE state. Otherwise we will wait the end of the request to switch
909 * it in tunnel mode.
910 */
911 if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
912 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
913 h1s->req.state = H1_MSG_TUNNEL;
914 }
915
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100916 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
917 h1s->res.state = H1_MSG_TUNNEL;
918 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
919 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100920 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
921 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
922 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100923 }
924}
925
Christopher Faulet82f01602019-05-24 16:50:16 +0200926/* Estimate the size of the HTX headers after the parsing, including the EOH. */
927static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
928{
929 size_t sz = 0;
930 int i;
931
932 for (i = 0; hdrs[i].n.len; i++)
933 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
934 sz += sizeof(struct htx_blk) + 1;
935 return sz;
936}
937
Christopher Faulet30db3d72019-05-17 15:35:33 +0200938/* Estimate the size of the HTX request after the parsing. */
939static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
940{
941 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200942
943 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +0200944 sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200945 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200946 return sz;
947}
948
949/* Estimate the size of the HTX response after the parsing. */
950static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
951{
952 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200953
954 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +0200955 sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200956 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200957 return sz;
958}
959
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100960/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200961 * Add the EOM in the HTX message and switch the message to the DONE state. It
962 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
963 * functions is responsible to update the parser state <h1m>. It also add the
964 * flag CS_FL_EOI on the CS.
965 */
966static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
967{
Willy Tarreau62038152019-08-23 09:29:29 +0200968 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
969 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200970 return 0;
Willy Tarreau62038152019-08-23 09:29:29 +0200971 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200972
Willy Tarreau62038152019-08-23 09:29:29 +0200973 h1s->flags &= ~H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200974 h1m->state = H1_MSG_DONE;
975 h1s->cs->flags |= CS_FL_EOI;
976 return (sizeof(struct htx_blk) + 1);
977}
978
979/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200980 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200981 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
982 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800983 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200984 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200985static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200986 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200987{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200988 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200989 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100990 union h1_sl h1sl;
991 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200992 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +0200993 int ret = 0;
994
Christopher Faulet30db3d72019-05-17 15:35:33 +0200995 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100996 goto end;
997
Christopher Faulet129817b2018-09-20 16:14:40 +0200998 /* Realing input buffer if necessary */
999 if (b_head(buf) + b_data(buf) > b_wrap(buf))
1000 b_slow_realign(buf, trash.area, 0);
1001
Christopher Faulet842f41f2019-09-25 09:10:46 +02001002 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001003 /* Try to match H2 preface before parsing the request headers. */
1004 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1005 if (ret > 0)
1006 goto h2c_upgrade;
1007 }
1008
Christopher Faulet30db3d72019-05-17 15:35:33 +02001009 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001010 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001011 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001012 /* Incomplete or invalid message. If the input buffer only
1013 * contains headers and is full, which is detected by it being
1014 * full and the offset to be zero, it's an error because
1015 * headers are too large to be handled by the parser. */
1016 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001017 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001018 goto end;
1019 }
1020
Christopher Fauletb4bad502019-10-11 14:22:00 +02001021 if (h1m->err_pos >= 0) {
1022 /* Maybe we found an error during the parsing while we were
1023 * configured not to block on that, so we have to capture it
1024 * now.
1025 */
1026 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1027 }
1028
Christopher Faulet129817b2018-09-20 16:14:40 +02001029 /* messages headers fully parsed, do some checks to prepare the body
1030 * parsing.
1031 */
1032
Christopher Faulet9768c262018-10-22 09:34:31 +02001033 /* Save the request's method or the response's status, check if the body
1034 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001035 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001036 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001037
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001038 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001039 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001040
1041 if (h1s->meth == HTTP_METH_CONNECT) {
1042 /* Switch CONNECT requests to tunnel mode */
1043 h1_set_req_tunnel_mode(h1s);
1044 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001045
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001046 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1047 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001048 h1m->err_state = h1m->state;
1049 goto vsn_error;
1050 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001051 }
1052 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001053 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001054
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001055 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1056 h1s->status == 101) {
1057 /* Switch successfull replies to CONNECT requests and
1058 * protocol switching to tunnel mode. */
1059 h1_set_res_tunnel_mode(h1s);
1060 }
1061 else if ((h1s->meth == HTTP_METH_HEAD) ||
1062 (h1s->status >= 100 && h1s->status < 200) ||
1063 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001064 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001065 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1066 h1m->flags |= H1_MF_XFER_LEN;
1067 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001068 }
1069 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001070 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001071 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001072 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001073 else {
1074 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001075 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001076 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001077
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001078 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1079 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001080 h1m->err_state = h1m->state;
1081 goto vsn_error;
1082 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001083 }
1084
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001085 /* Set HTX start-line flags */
1086 if (h1m->flags & H1_MF_VER_11)
1087 flags |= HTX_SL_F_VER_11;
1088 if (h1m->flags & H1_MF_XFER_ENC)
1089 flags |= HTX_SL_F_XFER_ENC;
1090 if (h1m->flags & H1_MF_XFER_LEN) {
1091 flags |= HTX_SL_F_XFER_LEN;
1092 if (h1m->flags & H1_MF_CHNK)
1093 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001094 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001095 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001096 if (h1m->body_len == 0)
1097 flags |= HTX_SL_F_BODYLESS;
1098 }
1099 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001100 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001101 }
1102
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001103 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001104 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001105 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1106 if (htx_is_empty(htx))
1107 goto error;
1108 h1m_init_req(h1m);
1109 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1110 ret = 0;
1111 goto end;
1112 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001113
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001114 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1115 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001116 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001117 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001118
1119 /* Check if the uri contains an explicit scheme and if it is
1120 * "http" or "https". */
1121 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1122 sl->flags |= HTX_SL_F_HAS_SCHM;
1123 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1124 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1125 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001126 }
1127 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001128 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1129 if (htx_is_empty(htx))
1130 goto error;
1131 h1m_init_res(h1m);
1132 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1133 ret = 0;
1134 goto end;
1135 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001136
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001137 flags |= HTX_SL_F_IS_RESP;
1138 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1139 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001140 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001141 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001142 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001143
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001144 /* Set bytes used in the HTX mesage for the headers now */
1145 sl->hdrs_bytes = htx_used_space(htx) - used;
1146
Christopher Fauletb992af02019-03-28 15:42:24 +01001147 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001148
1149 /* If body length cannot be determined, set htx->extra to
1150 * ULLONG_MAX. This value is impossible in other cases.
1151 */
1152 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001154 end:
1155 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001156
1157 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001158 h1m->err_state = h1m->state;
1159 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001160 vsn_error:
1161 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001162 h1s->cs->flags |= CS_FL_EOI;
1163 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001164 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001165 ret = 0;
1166 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001167
1168 h2c_upgrade:
1169 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001170 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001171 htx->flags |= HTX_FL_UPGRADE;
1172 ret = 0;
1173 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001174}
1175
1176/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001177 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1178 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001179 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001180 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001181 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001182static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001183 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001184 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001185{
1186 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001187 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001188
Christopher Faulet129817b2018-09-20 16:14:40 +02001189 if (h1m->flags & H1_MF_XFER_LEN) {
1190 if (h1m->flags & H1_MF_CLEN) {
1191 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001192 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001193 if ((uint64_t)ret > h1m->curr_len)
1194 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001195 if (ret > b_contig_data(buf, *ofs))
1196 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001197
Christopher Faulet9768c262018-10-22 09:34:31 +02001198 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001199 /* very often with large files we'll face the following
1200 * situation :
1201 * - htx is empty and points to <htxbuf>
1202 * - ret == buf->data
1203 * - buf->head == sizeof(struct htx)
1204 * => we can swap the buffers and place an htx header into
1205 * the target buffer instead
1206 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001207 int32_t try = ret;
1208
Willy Tarreau78f548f2018-12-05 10:02:39 +01001209 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1210 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1211 void *raw_area = buf->area;
1212 void *htx_area = htxbuf->area;
1213 struct htx_blk *blk;
1214
1215 buf->area = htx_area;
1216 htxbuf->area = raw_area;
1217 htx = (struct htx *)htxbuf->area;
1218 htx->size = htxbuf->size - sizeof(*htx);
1219 htx_reset(htx);
1220 b_set_data(htxbuf, b_size(htxbuf));
1221
1222 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1223 blk->info += ret;
1224 /* nothing else to do, the old buffer now contains an
1225 * empty pre-initialized HTX header
1226 */
1227 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001228 else {
1229 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1230 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001231 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001232 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001233 *ofs += ret;
1234 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001235 if (ret < try)
1236 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 }
1238
1239 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001240 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001242 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001243 }
1244 else if (h1m->flags & H1_MF_CHNK) {
1245 new_chunk:
1246 /* te:chunked : parse chunks */
1247 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001248 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001249 if (ret <= 0)
1250 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001251 h1m->state = H1_MSG_CHUNK_SIZE;
1252
Christopher Fauletf2824e62018-10-01 12:12:37 +02001253 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001254 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 }
1256
1257 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1258 unsigned int chksz;
1259
Christopher Faulet30db3d72019-05-17 15:35:33 +02001260 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001261 if (ret <= 0)
1262 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001263 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001264
Christopher Faulet129817b2018-09-20 16:14:40 +02001265 h1m->curr_len = chksz;
1266 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001267 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001268 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001269
1270 if (!h1m->curr_len)
1271 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001272 }
1273
1274 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001275 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001276 if ((uint64_t)ret > h1m->curr_len)
1277 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001278 if (ret > b_contig_data(buf, *ofs))
1279 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001280
Christopher Faulet9768c262018-10-22 09:34:31 +02001281 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001282 int32_t try = ret;
1283
1284 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001285 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001286 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001287 *ofs += ret;
1288 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001289 if (ret < try)
1290 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001291 }
1292 if (!h1m->curr_len) {
1293 h1m->state = H1_MSG_CHUNK_CRLF;
1294 goto new_chunk;
1295 }
1296 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001297 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001298 }
1299 else {
1300 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1301 * is no body. Switch the message in DONE state
1302 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001303 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001305 }
1306 }
1307 else {
1308 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001309 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001310 if (ret > b_contig_data(buf, *ofs))
1311 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001312
Christopher Faulet9768c262018-10-22 09:34:31 +02001313 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001314 int32_t try = ret;
1315
1316 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001317
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001318 *ofs += ret;
1319 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001320 if (ret < try)
1321 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001322 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001323 }
1324
1325 end:
1326 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001327 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001328 h1s->cs->flags |= CS_FL_EOI;
1329 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001330 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001331 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001332 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001333 return 0;
1334 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 /* update htx->extra, only when the body length is known */
1336 if (h1m->flags & H1_MF_XFER_LEN)
1337 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001338 return total;
1339}
1340
1341/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001342 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1343 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1344 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1345 * responsible to update the parser state <h1m>.
1346 */
1347static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1348 struct buffer *buf, size_t *ofs, size_t max)
1349{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001350 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001351 struct h1m tlr_h1m;
1352 int ret = 0;
1353
1354 if (!max || !b_data(buf))
1355 goto end;
1356
1357 /* Realing input buffer if necessary */
1358 if (b_peek(buf, *ofs) > b_tail(buf))
1359 b_slow_realign(buf, trash.area, 0);
1360
1361 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1362 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1363 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1364 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001365 /* Incomplete or invalid trailers. If the input buffer only
1366 * contains trailers and is full, which is detected by it being
1367 * full and the offset to be zero, it's an error because
1368 * trailers are too large to be handled by the parser. */
1369 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001370 goto error;
1371 goto end;
1372 }
1373
1374 /* messages trailers fully parsed. */
1375 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1376 if (htx_is_empty(htx))
1377 goto error;
1378 ret = 0;
1379 goto end;
1380 }
1381
1382 if (!htx_add_all_trailers(htx, hdrs))
1383 goto error;
1384
1385 *ofs += ret;
1386 h1s->flags |= H1S_F_HAVE_I_TLR;
1387 end:
1388 return ret;
1389
1390 error:
1391 h1m->err_state = h1m->state;
1392 h1m->err_pos = h1m->next;
1393 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1394 h1s->cs->flags |= CS_FL_EOI;
1395 htx->flags |= HTX_FL_PARSING_ERROR;
1396 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1397 ret = 0;
1398 goto end;
1399}
1400
1401/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001402 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001403 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1404 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001405 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001406static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001407{
Christopher Faulet539e0292018-11-19 10:40:09 +01001408 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001409 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001410 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001411 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001412 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001413 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001414
Christopher Faulet539e0292018-11-19 10:40:09 +01001415 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001416
Christopher Fauletf2824e62018-10-01 12:12:37 +02001417 if (!conn_is_back(h1c->conn)) {
1418 h1m = &h1s->req;
1419 errflag = H1S_F_REQ_ERROR;
1420 }
1421 else {
1422 h1m = &h1s->res;
1423 errflag = H1S_F_RES_ERROR;
1424 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001425
Christopher Faulet74027762019-02-26 14:45:05 +01001426 data = htx->data;
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001427 if (h1s->flags & errflag)
1428 goto end;
1429
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001430 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001431 size_t used = htx_used_space(htx);
1432
Christopher Faulet129817b2018-09-20 16:14:40 +02001433 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001434 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001435 if (!ret)
1436 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001437 if ((h1m->flags & H1_MF_RESP) &&
1438 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1439 h1m_init_res(&h1s->res);
1440 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1441 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001442 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001443 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001444 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001445 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001446 if (!ret)
1447 break;
1448 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001449 else if (h1m->state == H1_MSG_TRAILERS) {
1450 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1451 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1452 if (!ret)
1453 break;
1454 }
Christopher Faulet1021e722019-09-03 21:55:14 +02001455 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001456 break;
1457 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001458 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletac4778c2019-11-15 11:14:23 +01001459 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1460 h1_set_req_tunnel_mode(h1s);
1461 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001462 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001463 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001464 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001465 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001466 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001467 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 if (!ret)
1469 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001470 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001471 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001472 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001473 break;
1474 }
1475
Christopher Faulet30db3d72019-05-17 15:35:33 +02001476 count -= htx_used_space(htx) - used;
Christopher Faulet98e2bc22019-09-03 16:26:15 +02001477 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001478
Christopher Faulet47365272018-10-31 17:40:50 +01001479 if (h1s->flags & errflag)
1480 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001481
Christopher Faulet539e0292018-11-19 10:40:09 +01001482 b_del(&h1c->ibuf, total);
1483
1484 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001485 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001486 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001487 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001488 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001489 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001490 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001491
Christopher Fauletcf56b992018-12-11 16:12:31 +01001492 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1493
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001494 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001495 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001496 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001497 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001498
Willy Tarreau62038152019-08-23 09:29:29 +02001499 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1500 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001501 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001502 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001503 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001504 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001505
Christopher Faulet30db3d72019-05-17 15:35:33 +02001506 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001507
1508 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001509 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001510 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001511 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001512}
1513
Christopher Faulet129817b2018-09-20 16:14:40 +02001514/*
1515 * Process outgoing data. It parses data and transfer them from the channel buffer into
1516 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1517 * 0 if it couldn't proceed.
1518 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001519static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1520{
Christopher Faulet129817b2018-09-20 16:14:40 +02001521 struct h1s *h1s = h1c->h1s;
1522 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 struct htx *chn_htx;
1524 struct htx_blk *blk;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001525 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001526 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001527 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001528
Christopher Faulet47365272018-10-31 17:40:50 +01001529 if (!count)
1530 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001531
Christopher Faulet94b2c762019-05-24 15:28:57 +02001532 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001533 if (htx_is_empty(chn_htx))
1534 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001535
Christopher Faulet51dbc942018-09-13 09:05:15 +02001536 if (!h1_get_buf(h1c, &h1c->obuf)) {
1537 h1c->flags |= H1C_F_OUT_ALLOC;
1538 goto end;
1539 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001540
Christopher Fauletf2824e62018-10-01 12:12:37 +02001541 if (!conn_is_back(h1c->conn)) {
1542 h1m = &h1s->res;
1543 errflag = H1S_F_RES_ERROR;
1544 }
1545 else {
1546 h1m = &h1s->req;
1547 errflag = H1S_F_REQ_ERROR;
1548 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001549
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001550 if (h1s->flags & errflag)
1551 goto end;
1552
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001553 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001554 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001555
Willy Tarreau3815b222018-12-11 19:50:43 +01001556 /* Perform some optimizations to reduce the number of buffer copies.
1557 * First, if the mux's buffer is empty and the htx area contains
1558 * exactly one data block of the same size as the requested count,
1559 * then it's possible to simply swap the caller's buffer with the
1560 * mux's output buffer and adjust offsets and length to match the
1561 * entire DATA HTX block in the middle. In this case we perform a
1562 * true zero-copy operation from end-to-end. This is the situation
1563 * that happens all the time with large files. Second, if this is not
1564 * possible, but the mux's output buffer is empty, we still have an
1565 * opportunity to avoid the copy to the intermediary buffer, by making
1566 * the intermediary buffer's area point to the output buffer's area.
1567 * In this case we want to skip the HTX header to make sure that copies
1568 * remain aligned and that this operation remains possible all the
1569 * time. This goes for headers, data blocks and any data extracted from
1570 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001571 */
1572 if (!b_data(&h1c->obuf)) {
Willy Tarreau3815b222018-12-11 19:50:43 +01001573 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001574 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001575 htx_get_blk_value(chn_htx, blk).len == count) {
1576 void *old_area = h1c->obuf.area;
1577
1578 h1c->obuf.area = buf->area;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001579 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001580 h1c->obuf.data = count;
1581
1582 buf->area = old_area;
1583 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001584
1585 /* The message is chunked. We need to emit the chunk
1586 * size. We have at least the size of the struct htx to
1587 * write the chunk envelope. It should be enough.
1588 */
1589 if (h1m->flags & H1_MF_CHNK) {
1590 h1_emit_chunk_size(&h1c->obuf, count);
1591 h1_emit_chunk_crlf(&h1c->obuf);
1592 }
1593
Willy Tarreau3815b222018-12-11 19:50:43 +01001594 total += count;
1595 goto out;
1596 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001597 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001598 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001599 else
1600 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001601
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001602 tmp.data = 0;
1603 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001604 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001605 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001606 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001607 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001608 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001609 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001610
Christopher Fauletb2e84162018-12-06 11:39:49 +01001611 vlen = sz;
1612 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001613 if (type != HTX_BLK_DATA)
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001614 goto full;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001615 vlen = count;
1616 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001617
Christopher Faulet94b2c762019-05-24 15:28:57 +02001618 if (type == HTX_BLK_UNUSED)
1619 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001620
Christopher Faulet94b2c762019-05-24 15:28:57 +02001621 switch (h1m->state) {
1622 case H1_MSG_RQBEFORE:
1623 if (type != HTX_BLK_REQ_SL)
1624 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001625 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001626 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001627 h1_parse_req_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001628 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001629 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001630 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001631 if (sl->flags & HTX_SL_F_BODYLESS)
1632 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001633 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001634 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001635
Christopher Faulet94b2c762019-05-24 15:28:57 +02001636 case H1_MSG_RPBEFORE:
1637 if (type != HTX_BLK_RES_SL)
1638 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001639 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001640 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001641 h1_parse_res_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001642 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001643 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001644 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001646 if (sl->info.res.status < 200 &&
1647 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001648 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 h1m->state = H1_MSG_HDR_FIRST;
1650 break;
1651
Christopher Faulet94b2c762019-05-24 15:28:57 +02001652 case H1_MSG_HDR_FIRST:
1653 case H1_MSG_HDR_NAME:
1654 case H1_MSG_HDR_L2_LWS:
1655 if (type == HTX_BLK_EOH)
1656 goto last_lf;
1657 if (type != HTX_BLK_HDR)
1658 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001659 h1m->state = H1_MSG_HDR_NAME;
1660 n = htx_get_blk_name(chn_htx, blk);
1661 v = htx_get_blk_value(chn_htx, blk);
1662
1663 if (isteqi(n, ist("transfer-encoding")))
1664 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001665 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001666 /* Only skip C-L header with invalid value. */
1667 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001668 goto skip_hdr;
1669 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001670 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001671 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001672 if (!v.len)
1673 goto skip_hdr;
1674 }
1675
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001676 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001677 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001678 skip_hdr:
1679 h1m->state = H1_MSG_HDR_L2_LWS;
1680 break;
1681
Christopher Faulet94b2c762019-05-24 15:28:57 +02001682 case H1_MSG_LAST_LF:
1683 if (type != HTX_BLK_EOH)
1684 goto error;
1685 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001686 h1m->state = H1_MSG_LAST_LF;
1687 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001688 /* There is no "Connection:" header and
1689 * it the conn_mode must be
1690 * processed. So do it */
Christopher Faulet661bfc32019-06-17 14:07:46 +02001691 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001692 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001693 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001694 if (v.len) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001695 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001696 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001697 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001698 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001699 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001700
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001701 if ((h1s->meth != HTTP_METH_CONNECT &&
1702 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001703 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1704 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1705 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1706 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1707 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001708 /* chunking needed but header not seen */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001709 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001710 goto full;
Willy Tarreau4710d202019-01-03 17:39:54 +01001711 h1m->flags |= H1_MF_CHNK;
1712 }
1713
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001714 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001715 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001716
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001717 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1718 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1719 h1_set_req_tunnel_mode(h1s);
1720 }
Christopher Fauletac4778c2019-11-15 11:14:23 +01001721 else if ((h1m->flags & H1_MF_RESP) &&
1722 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001723 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletac4778c2019-11-15 11:14:23 +01001724 * to the client. Switch the response to tunnel mode.
1725 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001726 h1_set_res_tunnel_mode(h1s);
1727 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001728 else if ((h1m->flags & H1_MF_RESP) &&
1729 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1730 h1m_init_res(&h1s->res);
1731 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001732 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001733 }
Christopher Faulet33d58b52019-07-01 16:17:30 +02001734 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1735 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001736 else
1737 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001738 break;
1739
Christopher Faulet94b2c762019-05-24 15:28:57 +02001740 case H1_MSG_DATA:
Christopher Fauletaaa25062019-07-04 17:12:12 +02001741 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001742 if (type == HTX_BLK_EOM) {
1743 /* Chunked message without explicit trailers */
1744 if (h1m->flags & H1_MF_CHNK) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001745 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001746 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001747 }
1748 goto done;
1749 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001750 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet52131682019-06-27 17:40:14 +02001751 /* If the message is not chunked, never
1752 * add the last chunk. */
1753 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001754 goto full;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001755 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001756 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001757 else if (type != HTX_BLK_DATA)
1758 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001759 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001760 v.len = vlen;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001761 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001762 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001763 break;
1764
Christopher Faulet94b2c762019-05-24 15:28:57 +02001765 case H1_MSG_TRAILERS:
1766 if (type == HTX_BLK_EOM)
1767 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001768 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001769 goto error;
1770 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001771 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet52131682019-06-27 17:40:14 +02001772 /* If the message is not chunked, ignore
1773 * trailers. It may happen with H2 messages. */
1774 if (!(h1m->flags & H1_MF_CHNK))
1775 break;
1776
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001777 if (type == HTX_BLK_EOT) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001778 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001779 goto full;
Christopher Faulet32188212018-11-20 18:21:43 +01001780 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001781 else { // HTX_BLK_TLR
1782 n = htx_get_blk_name(chn_htx, blk);
1783 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001784 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001785 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001786 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001787 break;
1788
Christopher Faulet94b2c762019-05-24 15:28:57 +02001789 case H1_MSG_DONE:
1790 if (type != HTX_BLK_EOM)
1791 goto error;
1792 done:
1793 h1m->state = H1_MSG_DONE;
Christopher Fauletac4778c2019-11-15 11:14:23 +01001794 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1795 h1_set_req_tunnel_mode(h1s);
1796 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001797 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1798 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1799 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001800 break;
1801
Christopher Faulet9768c262018-10-22 09:34:31 +02001802 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001803 error:
Christopher Fauletc431df82019-06-26 15:16:28 +02001804 /* Unexpected error during output processing */
1805 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001806 h1s->flags |= errflag;
Christopher Fauletc431df82019-06-26 15:16:28 +02001807 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001808 break;
1809 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001810
1811 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001812 total += vlen;
1813 count -= vlen;
1814 if (sz == vlen)
1815 blk = htx_remove_blk(chn_htx, blk);
1816 else {
1817 htx_cut_data_blk(chn_htx, blk, vlen);
1818 break;
1819 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001820 }
1821
Christopher Faulet9768c262018-10-22 09:34:31 +02001822 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001823 /* when the output buffer is empty, tmp shares the same area so that we
1824 * only have to update pointers and lengths.
1825 */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001826 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1827 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001828 else
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001829 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001830
Willy Tarreau3815b222018-12-11 19:50:43 +01001831 htx_to_buf(chn_htx, buf);
1832 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001833 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001834 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001835 end:
1836 return total;
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001837
1838 full:
1839 h1c->flags |= H1C_F_OUT_FULL;
1840 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001841}
1842
Christopher Faulet51dbc942018-09-13 09:05:15 +02001843/*********************************************************/
1844/* functions below are I/O callbacks from the connection */
1845/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001846static void h1_wake_stream_for_recv(struct h1s *h1s)
1847{
1848 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001849 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001850 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001851 h1s->recv_wait = NULL;
1852 }
1853}
1854static void h1_wake_stream_for_send(struct h1s *h1s)
1855{
1856 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001857 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001858 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001859 h1s->send_wait = NULL;
1860 }
1861}
1862
Christopher Faulet51dbc942018-09-13 09:05:15 +02001863/*
1864 * Attempt to read data, and subscribe if none available
1865 */
1866static int h1_recv(struct h1c *h1c)
1867{
1868 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001869 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001870 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001871 int rcvd = 0;
1872
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001873 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001874 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001875
Olivier Houchard5b06cb32019-08-13 15:21:59 +02001876 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN)
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001877 return 0;
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001878
Olivier Houchard75159a92018-12-03 18:46:09 +01001879 if (!h1_recv_allowed(h1c)) {
1880 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001881 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001882 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001883
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001884 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1885 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001886 goto end;
1887 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001888
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001889 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001890 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001891 h1_wake_stream_for_recv(h1s);
1892 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001893 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001894 }
1895
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001896 /*
1897 * If we only have a small amount of data, realign it,
1898 * it's probably cheaper than doing 2 recv() calls.
1899 */
1900 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1901 b_slow_realign(&h1c->ibuf, trash.area, 0);
1902
Willy Tarreau45f2b892018-12-05 07:59:27 +01001903 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001904 if (max) {
1905 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001906
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001907 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001908 if (!b_data(&h1c->ibuf)) {
1909 /* try to pre-align the buffer like the rxbufs will be
1910 * to optimize memory copies.
1911 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001912 h1c->ibuf.head = sizeof(struct htx);
1913 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001914 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001915 }
Christopher Faulet47365272018-10-31 17:40:50 +01001916 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001917 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001918 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001919 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001920 if (h1s->csinfo.t_idle == -1)
1921 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1922 }
Christopher Faulet47365272018-10-31 17:40:50 +01001923 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001924
Christopher Fauletcf56b992018-12-11 16:12:31 +01001925 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001926 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001927 goto end;
1928 }
1929
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001930 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931
Christopher Faulet9768c262018-10-22 09:34:31 +02001932 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001933 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1934 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001935
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001936 if (conn_xprt_read0_pending(conn) && h1s) {
1937 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001938 rcvd = 1;
1939 }
1940
Christopher Faulet51dbc942018-09-13 09:05:15 +02001941 if (!b_data(&h1c->ibuf))
1942 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001943 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001944 h1c->flags |= H1C_F_IN_FULL;
1945 return rcvd;
1946}
1947
1948
1949/*
1950 * Try to send data if possible
1951 */
1952static int h1_send(struct h1c *h1c)
1953{
1954 struct connection *conn = h1c->conn;
1955 unsigned int flags = 0;
1956 size_t ret;
1957 int sent = 0;
1958
1959 if (conn->flags & CO_FL_ERROR)
1960 return 0;
1961
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001962 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001963 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001964 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001965 return 0;
1966 }
1967
Christopher Faulet51dbc942018-09-13 09:05:15 +02001968 if (!b_data(&h1c->obuf))
1969 goto end;
1970
1971 if (h1c->flags & H1C_F_OUT_FULL)
1972 flags |= CO_SFL_MSG_MORE;
1973
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001974 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001975 if (ret > 0) {
1976 h1c->flags &= ~H1C_F_OUT_FULL;
1977 b_del(&h1c->obuf, ret);
1978 sent = 1;
1979 }
1980
Christopher Faulet145aa472018-12-06 10:56:20 +01001981 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1982 /* error or output closed, nothing to send, clear the buffer to release it */
1983 b_reset(&h1c->obuf);
1984 }
1985
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001987 if (!(h1c->flags & H1C_F_OUT_FULL))
1988 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001989
Christopher Faulet51dbc942018-09-13 09:05:15 +02001990 /* We're done, no more to send */
1991 if (!b_data(&h1c->obuf)) {
1992 h1_release_buf(h1c, &h1c->obuf);
1993 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001994 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001995 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001996 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001997 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001998
1999 return sent;
2000}
2001
Christopher Faulet51dbc942018-09-13 09:05:15 +02002002
2003/* callback called on any event by the connection handler.
2004 * It applies changes and returns zero, or < 0 if it wants immediate
2005 * destruction of the connection.
2006 */
2007static int h1_process(struct h1c * h1c)
2008{
2009 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002010 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002011
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002012 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002013 return -1;
2014
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002015 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02002016 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02002017 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01002018 goto end;
2019 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01002020 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002021 }
2022
Christopher Fauletfeb11742018-11-29 15:12:34 +01002023 if (!h1s) {
Christopher Faulet470438c2019-07-11 15:40:25 +02002024 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002025 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002026 conn_xprt_read0_pending(conn))
2027 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002028 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002029 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002030 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002031 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002032 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002033 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002034 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002035 }
2036
Christopher Fauletfeb11742018-11-29 15:12:34 +01002037 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2038 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2039
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002040 if (conn_xprt_read0_pending(conn))
2041 h1s->flags |= H1S_F_REOS;
2042
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002043 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002044 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002045 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002046 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002047 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002048 h1s->cs->data_cb->wake(h1s->cs);
2049 }
Christopher Faulet47365272018-10-31 17:40:50 +01002050 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002051 if (h1c->task) {
2052 h1c->task->expire = TICK_ETERNITY;
2053 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002054 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 +01002055 ? h1c->shut_timeout
2056 : h1c->timeout));
2057 task_queue(h1c->task);
2058 }
2059 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002060 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002061
2062 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002063 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002064 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002065}
2066
2067static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2068{
2069 struct h1c *h1c = ctx;
2070 int ret = 0;
2071
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002072 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002073 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002074 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002075 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002076 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002077 h1_process(h1c);
2078 return NULL;
2079}
2080
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002081static void h1_reset(struct connection *conn)
2082{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002083 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002084
2085 /* Reset the flags, and let the mux know we're waiting for a connection */
2086 h1c->flags = H1C_F_CS_WAIT_CONN;
2087}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002088
2089static int h1_wake(struct connection *conn)
2090{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002091 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002092 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002093
Christopher Faulet539e0292018-11-19 10:40:09 +01002094 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002095 ret = h1_process(h1c);
2096 if (ret == 0) {
2097 struct h1s *h1s = h1c->h1s;
2098
2099 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2100 ret = h1s->cs->data_cb->wake(h1s->cs);
2101 }
2102 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002103}
2104
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002105/* Connection timeout management. The principle is that if there's no receipt
2106 * nor sending for a certain amount of time, the connection is closed.
2107 */
2108static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2109{
2110 struct h1c *h1c = context;
2111 int expired = tick_is_expired(t->expire, now_ms);
2112
2113 if (!expired && h1c)
2114 return t;
2115
Olivier Houchard3f795f72019-04-17 22:51:06 +02002116 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002117
2118 if (!h1c) {
2119 /* resources were already deleted */
2120 return NULL;
2121 }
2122
2123 h1c->task = NULL;
2124 /* If a stream is still attached to the mux, just set an error and wait
2125 * for the stream's timeout. Otherwise, release the mux. This is only ok
2126 * because same timeouts are used.
2127 */
2128 if (h1c->h1s && h1c->h1s->cs)
2129 h1c->flags |= H1C_F_CS_ERROR;
2130 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002131 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002132 return NULL;
2133}
2134
Christopher Faulet51dbc942018-09-13 09:05:15 +02002135/*******************************************/
2136/* functions below are used by the streams */
2137/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002138
Christopher Faulet51dbc942018-09-13 09:05:15 +02002139/*
2140 * Attach a new stream to a connection
2141 * (Used for outgoing connections)
2142 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002143static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002144{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002145 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002146 struct conn_stream *cs = NULL;
2147 struct h1s *h1s;
2148
2149 if (h1c->flags & H1C_F_CS_ERROR)
2150 goto end;
2151
2152 cs = cs_new(h1c->conn);
2153 if (!cs)
2154 goto end;
2155
Olivier Houchardf502aca2018-12-14 19:42:40 +01002156 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002157 if (h1s == NULL)
2158 goto end;
2159
2160 return cs;
2161 end:
2162 cs_free(cs);
2163 return NULL;
2164}
2165
2166/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2167 * this mux, it's easy as we can only store a single conn_stream.
2168 */
2169static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2170{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002171 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002172 struct h1s *h1s = h1c->h1s;
2173
2174 if (h1s)
2175 return h1s->cs;
2176
2177 return NULL;
2178}
2179
Christopher Faulet73c12072019-04-08 11:23:22 +02002180static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002181{
Christopher Faulet73c12072019-04-08 11:23:22 +02002182 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002183
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002184 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002185 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002186}
2187
2188/*
2189 * Detach the stream from the connection and possibly release the connection.
2190 */
2191static void h1_detach(struct conn_stream *cs)
2192{
2193 struct h1s *h1s = cs->ctx;
2194 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002195 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002196 int has_keepalive;
2197 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002198
2199 cs->ctx = NULL;
2200 if (!h1s)
2201 return;
2202
Olivier Houchardf502aca2018-12-14 19:42:40 +01002203 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002204 h1c = h1s->h1c;
2205 h1s->cs = NULL;
2206
Olivier Houchard8a786902018-12-15 16:05:40 +01002207 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2208 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2209 h1s_destroy(h1s);
2210
2211 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002212 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002213 /* If there are any excess server data in the input buffer,
2214 * release it and close the connection ASAP (some data may
2215 * remain in the output buffer). This happens if a server sends
2216 * invalid responses. So in such case, we don't want to reuse
2217 * the connection
Christopher Faulet39e679b2019-07-19 11:34:08 +02002218 */
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002219 if (b_data(&h1c->ibuf)) {
2220 h1_release_buf(h1c, &h1c->ibuf);
2221 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2222 goto release;
2223 }
Christopher Faulet39e679b2019-07-19 11:34:08 +02002224
Christopher Faulet9400a392018-11-23 23:10:39 +01002225 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002226 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002227 h1c->conn->flags |= CO_FL_PRIVATE;
2228
Olivier Houchard44d59142018-12-13 18:46:22 +01002229 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002230 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002231 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2232 h1c->conn->owner = NULL;
2233 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2234 /* The server doesn't want it, let's kill the connection right away */
2235 h1c->conn->mux->destroy(h1c->conn);
2236 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002237 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002238 return;
2239
2240 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002241 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002242 if (h1c->conn->owner == sess) {
2243 int ret = session_check_idle_conn(sess, h1c->conn);
2244 if (ret == -1)
2245 /* The connection got destroyed, let's leave */
2246 return;
2247 else if (ret == 1) {
2248 /* The connection was added to the server list,
2249 * wake the task so we can subscribe to events
2250 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002251 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002252 return;
2253 }
2254 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002255 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002256 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002257 struct server *srv = objt_server(h1c->conn->target);
2258
2259 if (srv) {
2260 if (h1c->conn->flags & CO_FL_PRIVATE)
2261 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002262 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002263 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2264 else
2265 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2266 }
2267 }
2268 }
2269
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002270 release:
Christopher Faulet9fa93f62019-06-28 17:41:42 +02002271 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet470438c2019-07-11 15:40:25 +02002272 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2273 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2274 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2275 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002276 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002277 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002278 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002279 if (h1c->task) {
2280 h1c->task->expire = TICK_ETERNITY;
2281 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002282 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 +01002283 ? h1c->shut_timeout
2284 : h1c->timeout));
2285 task_queue(h1c->task);
2286 }
2287 }
2288 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002289}
2290
2291
2292static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2293{
2294 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002295 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002296
2297 if (!h1s)
2298 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002299 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002300
Christopher Faulet7f366362019-04-08 10:51:20 +02002301 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2302 goto do_shutr;
2303
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002304 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002305 return;
2306
Christopher Faulet7f366362019-04-08 10:51:20 +02002307 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002308 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2309 if (cs->flags & CS_FL_SHR)
2310 return;
2311 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002312 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2313 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002314 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 +02002315 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002316}
2317
2318static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2319{
2320 struct h1s *h1s = cs->ctx;
2321 struct h1c *h1c;
2322
2323 if (!h1s)
2324 return;
2325 h1c = h1s->h1c;
2326
Christopher Faulet7f366362019-04-08 10:51:20 +02002327 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2328 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002329
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002330 if ((h1c->flags & H1C_F_UPG_H2C) ||
2331 ((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 +02002332 return;
2333
Christopher Faulet7f366362019-04-08 10:51:20 +02002334 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002335 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2336 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2337 return;
2338
Christopher Faulet666a0c42019-01-08 11:12:04 +01002339 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002340}
2341
Christopher Faulet666a0c42019-01-08 11:12:04 +01002342static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002343{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002344 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002345
Christopher Faulet666a0c42019-01-08 11:12:04 +01002346 conn_xprt_shutw(conn);
2347 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2348 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2349 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002350}
2351
2352/* Called from the upper layer, to unsubscribe to events */
2353static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2354{
2355 struct wait_event *sw;
2356 struct h1s *h1s = cs->ctx;
2357
2358 if (!h1s)
2359 return 0;
2360
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002361 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002362 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002363 BUG_ON(h1s->recv_wait != sw);
2364 sw->events &= ~SUB_RETRY_RECV;
2365 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002366 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002367 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002368 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002369 BUG_ON(h1s->send_wait != sw);
2370 sw->events &= ~SUB_RETRY_SEND;
2371 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002372 }
2373 return 0;
2374}
2375
2376/* Called from the upper layer, to subscribe to events, such as being able to send */
2377static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2378{
2379 struct wait_event *sw;
2380 struct h1s *h1s = cs->ctx;
2381
2382 if (!h1s)
2383 return -1;
2384
2385 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002386 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002387 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002388 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2389 sw->events |= SUB_RETRY_RECV;
2390 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002392 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002393 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002394 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2395 sw->events |= SUB_RETRY_SEND;
2396 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002397 return 0;
2398 default:
2399 break;
2400 }
2401 return -1;
2402}
2403
2404/* Called from the upper layer, to receive data */
2405static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2406{
2407 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002408 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002409 size_t ret = 0;
2410
Christopher Faulet539e0292018-11-19 10:40:09 +01002411 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002412 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002413
Christopher Faulete18777b2019-04-16 16:46:36 +02002414 if (flags & CO_RFL_BUF_FLUSH) {
2415 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2416
2417 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2418 h1s->flags |= H1S_F_BUF_FLUSH;
2419 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002420 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2421 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002422 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002423 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002424 }
2425 return ret;
2426}
2427
2428
2429/* Called from the upper layer, to send data */
2430static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2431{
2432 struct h1s *h1s = cs->ctx;
2433 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002434 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002435
2436 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002437 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002438
2439 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002440 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2441 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002442
Christopher Fauletc31872f2019-06-04 22:09:36 +02002443 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002444 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002445
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002446 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2447 ret = h1_process_output(h1c, buf, count);
2448 if (!ret)
2449 break;
2450 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002451 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002452 if (!h1_send(h1c))
2453 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002454 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002455
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002456 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002457}
2458
Willy Tarreaue5733232019-05-22 19:24:06 +02002459#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002460/* Send and get, using splicing */
2461static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2462{
2463 struct h1s *h1s = cs->ctx;
2464 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2465 int ret = 0;
2466
Christopher Faulet2c411be2019-11-05 16:24:27 +01002467 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002468 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002469 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2470 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002471 goto end;
2472 }
2473
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002474 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002475 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002476 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002477 }
2478
2479 h1s->flags &= ~H1S_F_BUF_FLUSH;
2480 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002481 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2482 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002483 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002484 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002485 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002486 if (!h1m->curr_len)
2487 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2488 }
2489
Christopher Faulet1be55f92018-10-02 15:59:23 +02002490 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002491 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002492 h1s->flags |= H1S_F_REOS;
Christopher Fauletac4778c2019-11-15 11:14:23 +01002493 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet038ad812019-04-17 11:03:22 +02002494 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002495 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002496}
2497
2498static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2499{
2500 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002501 int ret = 0;
2502
2503 if (b_data(&h1s->h1c->obuf))
2504 goto end;
2505
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002506 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002507 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002508 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002509 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002510 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002511 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002512 return ret;
2513}
2514#endif
2515
Olivier Houchard198d1292019-10-25 16:19:26 +02002516static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2517{
2518 int ret = 0;
2519 switch (mux_ctl) {
2520 case MUX_STATUS:
2521 if (conn->flags & CO_FL_CONNECTED)
2522 ret |= MUX_STATUS_READY;
2523 return ret;
2524 default:
2525 return -1;
2526 }
2527}
2528
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002529/* for debugging with CLI's "show fd" command */
2530static void h1_show_fd(struct buffer *msg, struct connection *conn)
2531{
2532 struct h1c *h1c = conn->ctx;
2533 struct h1s *h1s = h1c->h1s;
2534
Christopher Fauletf376a312019-01-04 15:16:06 +01002535 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2536 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002537 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2538 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2539 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2540 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2541
2542 if (h1s) {
2543 char *method;
2544
2545 if (h1s->meth < HTTP_METH_OTHER)
2546 method = http_known_methods[h1s->meth].ptr;
2547 else
2548 method = "UNKNOWN";
2549 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2550 " .meth=%s status=%d",
2551 h1s, h1s->flags,
2552 h1m_state_str(h1s->req.state),
2553 h1m_state_str(h1s->res.state), method, h1s->status);
2554 if (h1s->cs)
2555 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2556 h1s->cs->flags, h1s->cs->data);
2557 }
2558}
2559
Christopher Faulet51dbc942018-09-13 09:05:15 +02002560/****************************************/
2561/* MUX initialization and instanciation */
2562/****************************************/
2563
2564/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002565static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002566 .init = h1_init,
2567 .wake = h1_wake,
2568 .attach = h1_attach,
2569 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002570 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002571 .detach = h1_detach,
2572 .destroy = h1_destroy,
2573 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002574 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002575 .rcv_buf = h1_rcv_buf,
2576 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002577#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002578 .rcv_pipe = h1_rcv_pipe,
2579 .snd_pipe = h1_snd_pipe,
2580#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002581 .subscribe = h1_subscribe,
2582 .unsubscribe = h1_unsubscribe,
2583 .shutr = h1_shutr,
2584 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002585 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002586 .reset = h1_reset,
Olivier Houchard198d1292019-10-25 16:19:26 +02002587 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002588 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002589 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002590};
2591
2592
2593/* this mux registers default HTX proto */
2594static struct mux_proto_list mux_proto_htx =
2595{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2596
Willy Tarreau0108d902018-11-25 19:14:37 +01002597INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2598
Christopher Faulet51dbc942018-09-13 09:05:15 +02002599/*
2600 * Local variables:
2601 * c-indent-level: 8
2602 * c-basic-offset: 8
2603 * End:
2604 */