blob: 881501eb0cac330cc00c51fa8075d4727b37bd63 [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 */
Christopher Faulet539e0292018-11-19 10:40:09 +010061/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020062#define H1S_F_WANT_KAL 0x00000010
63#define H1S_F_WANT_TUN 0x00000020
64#define H1S_F_WANT_CLO 0x00000040
65#define H1S_F_WANT_MSK 0x00000070
66#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010067#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010068#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010069#define H1S_F_HAVE_I_EOD 0x00000400 /* Set during input process to know the last empty chunk was processed */
70#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
71#define H1S_F_HAVE_O_EOD 0x00001000 /* Set during output process to know the last empty chunk was processed */
72#define H1S_F_HAVE_O_TLR 0x00002000 /* Set during output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073
74/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020075struct h1c {
76 struct connection *conn;
77 struct proxy *px;
78 uint32_t flags; /* Connection flags: H1C_F_* */
79
80 struct buffer ibuf; /* Input buffer to store data before parsing */
81 struct buffer obuf; /* Output buffer to store data after reformatting */
82
83 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
84 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
85
86 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010087 struct task *task; /* timeout management task */
88 int timeout; /* idle timeout duration in ticks */
89 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020090};
91
92/* H1 stream descriptor */
93struct h1s {
94 struct h1c *h1c;
95 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010096 struct cs_info csinfo; /* CS info, only used for client connections */
97 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098
Christopher Faulet51dbc942018-09-13 09:05:15 +020099 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
100 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200101
Olivier Houchardf502aca2018-12-14 19:42:40 +0100102 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200103 struct h1m req;
104 struct h1m res;
105
106 enum http_meth_t meth; /* HTTP resquest method */
107 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200108};
109
110/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100111DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
112DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200113
Christopher Faulet51dbc942018-09-13 09:05:15 +0200114static int h1_recv(struct h1c *h1c);
115static int h1_send(struct h1c *h1c);
116static int h1_process(struct h1c *h1c);
117static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100118static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100119static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200120
121/*****************************************************/
122/* functions below are for dynamic buffer management */
123/*****************************************************/
124/*
125 * Indicates whether or not the we may call the h1_recv() function to
126 * attempt to receive data into the buffer and/or parse pending data. The
127 * condition is a bit complex due to some API limits for now. The rules are the
128 * following :
129 * - if an error or a shutdown was detected on the connection and the buffer
130 * is empty, we must not attempt to receive
131 * - if the input buffer failed to be allocated, we must not try to receive
132 * and we know there is nothing pending
133 * - if no flag indicates a blocking condition, we may attempt to receive,
134 * regardless of whether the input buffer is full or not, so that only de
135 * receiving part decides whether or not to block. This is needed because
136 * the connection API indeed prevents us from re-enabling receipt that is
137 * already enabled in a polled state, so we must always immediately stop as
138 * soon as the mux can't proceed so as never to hit an end of read with data
139 * pending in the buffers.
140 * - otherwise must may not attempt to receive
141 */
142static inline int h1_recv_allowed(const struct h1c *h1c)
143{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100144 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200145 return 0;
146
Christopher Fauletcf56b992018-12-11 16:12:31 +0100147 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
148 return 0;
149
Christopher Fauletcb55f482018-12-10 11:56:47 +0100150 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200151 return 1;
152
153 return 0;
154}
155
156/*
157 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
158 * flags are used to figure what buffer was requested. It returns 1 if the
159 * allocation succeeds, in which case the connection is woken up, or 0 if it's
160 * impossible to wake up and we prefer to be woken up later.
161 */
162static int h1_buf_available(void *target)
163{
164 struct h1c *h1c = target;
165
166 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
167 h1c->flags &= ~H1C_F_IN_ALLOC;
168 if (h1_recv_allowed(h1c))
169 tasklet_wakeup(h1c->wait_event.task);
170 return 1;
171 }
172
173 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
174 h1c->flags &= ~H1C_F_OUT_ALLOC;
175 tasklet_wakeup(h1c->wait_event.task);
176 return 1;
177 }
178
Christopher Faulet51dbc942018-09-13 09:05:15 +0200179 return 0;
180}
181
182/*
183 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
184 */
185static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
186{
187 struct buffer *buf = NULL;
188
189 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
190 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
191 h1c->buf_wait.target = h1c;
192 h1c->buf_wait.wakeup_cb = h1_buf_available;
193 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
194 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
195 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
196 __conn_xprt_stop_recv(h1c->conn);
197 }
198 return buf;
199}
200
201/*
202 * Release a buffer, if any, and try to wake up entities waiting in the buffer
203 * wait queue.
204 */
205static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
206{
207 if (bptr->size) {
208 b_free(bptr);
209 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
210 }
211}
212
Willy Tarreau00f18a32019-01-26 12:19:01 +0100213/* returns the number of streams in use on a connection to figure if it's
214 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
215 * as the caller will want to know if it was the last one after a detach().
216 */
217static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200218{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100219 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200220
Willy Tarreau00f18a32019-01-26 12:19:01 +0100221 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200222}
223
Willy Tarreau00f18a32019-01-26 12:19:01 +0100224/* returns the number of streams still available on a connection */
225static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100226{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100227 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100228}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200229
Willy Tarreau00f18a32019-01-26 12:19:01 +0100230
Christopher Faulet51dbc942018-09-13 09:05:15 +0200231/*****************************************************************/
232/* functions below are dedicated to the mux setup and management */
233/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100234static struct conn_stream *h1s_new_cs(struct h1s *h1s)
235{
236 struct conn_stream *cs;
237
238 cs = cs_new(h1s->h1c->conn);
239 if (!cs)
240 goto err;
241 h1s->cs = cs;
242 cs->ctx = h1s;
243
244 if (h1s->flags & H1S_F_NOT_FIRST)
245 cs->flags |= CS_FL_NOT_FIRST;
246
247 if (stream_create_from_cs(cs) < 0)
248 goto err;
249 return cs;
250
251 err:
252 cs_free(cs);
253 h1s->cs = NULL;
254 return NULL;
255}
256
Olivier Houchardf502aca2018-12-14 19:42:40 +0100257static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200258{
259 struct h1s *h1s;
260
261 h1s = pool_alloc(pool_head_h1s);
262 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100263 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200264
265 h1s->h1c = h1c;
266 h1c->h1s = h1s;
267
Olivier Houchardf502aca2018-12-14 19:42:40 +0100268 h1s->sess = sess;
269
Christopher Faulet51dbc942018-09-13 09:05:15 +0200270 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100271 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200272
273 h1s->recv_wait = NULL;
274 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200275
276 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100277 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200278
Christopher Faulet129817b2018-09-20 16:14:40 +0200279 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100280 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200281
282 h1s->status = 0;
283 h1s->meth = HTTP_METH_OTHER;
284
Christopher Faulet47365272018-10-31 17:40:50 +0100285 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
286 h1s->flags |= H1S_F_NOT_FIRST;
287 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
288
Christopher Faulet129817b2018-09-20 16:14:40 +0200289 if (!conn_is_back(h1c->conn)) {
290 if (h1c->px->options2 & PR_O2_REQBUG_OK)
291 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200292
293 /* For frontend connections we should always have a session */
294 if (!sess)
295 sess = h1c->conn->owner;
296
297 h1s->csinfo.create_date = sess->accept_date;
298 h1s->csinfo.tv_create = sess->tv_accept;
299 h1s->csinfo.t_handshake = sess->t_handshake;
300 h1s->csinfo.t_idle = -1;
Christopher Faulet129817b2018-09-20 16:14:40 +0200301 }
302 else {
303 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
304 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200305
Christopher Fauletfeb11742018-11-29 15:12:34 +0100306 h1s->csinfo.create_date = date;
307 h1s->csinfo.tv_create = now;
308 h1s->csinfo.t_handshake = 0;
309 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200310 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100311
Christopher Faulete9b70722019-04-08 10:46:02 +0200312 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
313 * create a new one.
314 */
315 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200316 cs->ctx = h1s;
317 h1s->cs = cs;
318 }
Christopher Faulet47365272018-10-31 17:40:50 +0100319 else {
320 cs = h1s_new_cs(h1s);
321 if (!cs)
322 goto fail;
323 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200324 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100325
326 fail:
327 pool_free(pool_head_h1s, h1s);
328 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200329}
330
331static void h1s_destroy(struct h1s *h1s)
332{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200333 if (h1s) {
334 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200335
Christopher Fauletf2824e62018-10-01 12:12:37 +0200336 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200337
Christopher Fauletf2824e62018-10-01 12:12:37 +0200338 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100339 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200340 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100341 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200342
Christopher Fauletcb55f482018-12-10 11:56:47 +0100343 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100344 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
345 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
346 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200347 pool_free(pool_head_h1s, h1s);
348 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200349}
350
Christopher Fauletfeb11742018-11-29 15:12:34 +0100351static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
352{
353 struct h1s *h1s = cs->ctx;
354
355 if (h1s && !conn_is_back(cs->conn))
356 return &h1s->csinfo;
357 return NULL;
358}
359
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200361 * Initialize the mux once it's attached. It is expected that conn->ctx points
362 * to the existing conn_stream (for outgoing connections or for incoming onces
363 * during a mux upgrade) or NULL (for incoming ones during the connexion
364 * establishment). <input> is always used as Input buffer and may contain
365 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
366 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200367 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200368static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
369 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200370{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200371 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100372 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200373
374 h1c = pool_alloc(pool_head_h1c);
375 if (!h1c)
376 goto fail_h1c;
377 h1c->conn = conn;
378 h1c->px = proxy;
379
380 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200381 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200382 h1c->obuf = BUF_NULL;
383 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200384 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200385
Christopher Faulet51dbc942018-09-13 09:05:15 +0200386 LIST_INIT(&h1c->buf_wait.list);
387 h1c->wait_event.task = tasklet_new();
388 if (!h1c->wait_event.task)
389 goto fail;
390 h1c->wait_event.task->process = h1_io_cb;
391 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100392 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200393
Christopher Faulete9b70722019-04-08 10:46:02 +0200394 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100395 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
396 if (tick_isset(proxy->timeout.serverfin))
397 h1c->shut_timeout = proxy->timeout.serverfin;
398 } else {
399 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
400 if (tick_isset(proxy->timeout.clientfin))
401 h1c->shut_timeout = proxy->timeout.clientfin;
402 }
403 if (tick_isset(h1c->timeout)) {
404 t = task_new(tid_bit);
405 if (!t)
406 goto fail;
407
408 h1c->task = t;
409 t->process = h1_timeout_task;
410 t->context = h1c;
411 t->expire = tick_add(now_ms, h1c->timeout);
412 }
413
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200414 if (!(conn->flags & CO_FL_CONNECTED))
415 h1c->flags |= H1C_F_CS_WAIT_CONN;
416
Christopher Fauletf2824e62018-10-01 12:12:37 +0200417 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100418 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200419 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100421 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200422
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100423
424 if (t)
425 task_queue(t);
426
Christopher Faulet51dbc942018-09-13 09:05:15 +0200427 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100428 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200429
430 /* mux->wake will be called soon to complete the operation */
431 return 0;
432
433 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200434 task_destroy(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100435 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200436 tasklet_free(h1c->wait_event.task);
437 pool_free(pool_head_h1c, h1c);
438 fail_h1c:
439 return -1;
440}
441
Christopher Faulet73c12072019-04-08 11:23:22 +0200442/* release function. This one should be called to free all resources allocated
443 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200444 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200445static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200446{
Christopher Faulet61840e72019-04-15 09:33:32 +0200447 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200448
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200450 /* The connection must be aattached to this mux to be released */
451 if (h1c->conn && h1c->conn->ctx == h1c)
452 conn = h1c->conn;
453
454 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200455 h1c->flags &= ~H1C_F_UPG_H2C;
456 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
457 /* connection successfully upgraded to H2, this
458 * mux was already released */
459 return;
460 }
461 sess_log(conn->owner); /* Log if the upgrade failed */
462 }
463
Christopher Faulet61840e72019-04-15 09:33:32 +0200464
Christopher Faulet51dbc942018-09-13 09:05:15 +0200465 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
466 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
467 LIST_DEL(&h1c->buf_wait.list);
468 LIST_INIT(&h1c->buf_wait.list);
469 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
470 }
471
472 h1_release_buf(h1c, &h1c->ibuf);
473 h1_release_buf(h1c, &h1c->obuf);
474
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100475 if (h1c->task) {
476 h1c->task->context = NULL;
477 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
478 h1c->task = NULL;
479 }
480
Christopher Faulet51dbc942018-09-13 09:05:15 +0200481 if (h1c->wait_event.task)
482 tasklet_free(h1c->wait_event.task);
483
Christopher Fauletf2824e62018-10-01 12:12:37 +0200484 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200485 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100486 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200487 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200488 pool_free(pool_head_h1c, h1c);
489 }
490
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200491 if (conn) {
492 conn->mux = NULL;
493 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200494
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200495 conn_stop_tracking(conn);
496 conn_full_close(conn);
497 if (conn->destroy_cb)
498 conn->destroy_cb(conn);
499 conn_free(conn);
500 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200501}
502
503/******************************************************/
504/* functions below are for the H1 protocol processing */
505/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200506/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
507 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200508 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100509static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200510{
Christopher Faulet570d1612018-11-26 11:13:57 +0100511 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200512
Christopher Faulet570d1612018-11-26 11:13:57 +0100513 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200514 (*(p + 5) > '1' ||
515 (*(p + 5) == '1' && *(p + 7) >= '1')))
516 h1m->flags |= H1_MF_VER_11;
517}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200518
Christopher Faulet9768c262018-10-22 09:34:31 +0200519/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
520 * greater or equal to 1.1
521 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100522static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200523{
Christopher Faulet570d1612018-11-26 11:13:57 +0100524 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200525
Christopher Faulet570d1612018-11-26 11:13:57 +0100526 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200527 (*(p + 5) > '1' ||
528 (*(p + 5) == '1' && *(p + 7) >= '1')))
529 h1m->flags |= H1_MF_VER_11;
530}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200531
Christopher Faulet9768c262018-10-22 09:34:31 +0200532/*
533 * Check the validity of the request version. If the version is valid, it
534 * returns 1. Otherwise, it returns 0.
535 */
536static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
537{
538 struct h1c *h1c = h1s->h1c;
539
540 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
541 * exactly one digit "." one digit. This check may be disabled using
542 * option accept-invalid-http-request.
543 */
544 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
545 if (sl.rq.v.len != 8)
546 return 0;
547
548 if (*(sl.rq.v.ptr + 4) != '/' ||
549 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
550 *(sl.rq.v.ptr + 6) != '.' ||
551 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
552 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200553 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200554 else if (!sl.rq.v.len) {
555 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200556
Christopher Faulet9768c262018-10-22 09:34:31 +0200557 /* RFC 1945 allows only GET for HTTP/0.9 requests */
558 if (sl.rq.meth != HTTP_METH_GET)
559 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200560
Christopher Faulet9768c262018-10-22 09:34:31 +0200561 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
562 if (!sl.rq.u.len)
563 return 0;
564
565 /* Add HTTP version */
566 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100567 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200568 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100569
570 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100571 ((*(sl.rq.v.ptr + 5) > '1') ||
572 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100573 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200574 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200575}
576
Christopher Faulet9768c262018-10-22 09:34:31 +0200577/*
578 * Check the validity of the response version. If the version is valid, it
579 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200580 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200581static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200582{
Christopher Faulet9768c262018-10-22 09:34:31 +0200583 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200584
Christopher Faulet9768c262018-10-22 09:34:31 +0200585 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
586 * exactly one digit "." one digit. This check may be disabled using
587 * option accept-invalid-http-request.
588 */
589 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
590 if (sl.st.v.len != 8)
591 return 0;
592
593 if (*(sl.st.v.ptr + 4) != '/' ||
594 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
595 *(sl.st.v.ptr + 6) != '.' ||
596 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
597 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200598 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100599
600 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100601 ((*(sl.st.v.ptr + 5) > '1') ||
602 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100603 h1m->flags |= H1_MF_VER_11;
604
Christopher Faulet9768c262018-10-22 09:34:31 +0200605 return 1;
606}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200607
608/* Deduce the connection mode of the client connection, depending on the
609 * configuration and the H1 message flags. This function is called twice, the
610 * first time when the request is parsed and the second time when the response
611 * is parsed.
612 */
613static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
614{
615 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200616
617 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100618 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200619 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100620 h1s->status == 101) {
621 /* Either we've established an explicit tunnel, or we're
622 * switching the protocol. In both cases, we're very unlikely to
623 * understand the next protocols. We have to switch to tunnel
624 * mode, so that we transfer the request and responses then let
625 * this protocol pass unmodified. When we later implement
626 * specific parsers for such protocols, we'll want to check the
627 * Upgrade header which contains information about that protocol
628 * for responses with status 101 (eg: see RFC2817 about TLS).
629 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200630 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100631 }
632 else if (h1s->flags & H1S_F_WANT_KAL) {
633 /* By default the client is in KAL mode. CLOSE mode mean
634 * it is imposed by the client itself. So only change
635 * KAL mode here. */
636 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
637 /* no length known or explicit close => close */
638 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
639 }
640 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
641 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
642 /* no explict keep-alive and option httpclose => close */
643 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
644 }
645 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200646 }
647 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100648 /* Input direction: first pass */
649 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
650 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200651 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100652 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200653 }
654
655 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
656 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
657 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
658}
659
660/* Deduce the connection mode of the client connection, depending on the
661 * configuration and the H1 message flags. This function is called twice, the
662 * first time when the request is parsed and the second time when the response
663 * is parsed.
664 */
665static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
666{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100667 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100668 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100669 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200670
Christopher Fauletf2824e62018-10-01 12:12:37 +0200671 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100672 /* Input direction: second pass */
673
Christopher Fauletf2824e62018-10-01 12:12:37 +0200674 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100675 h1s->status == 101) {
676 /* Either we've established an explicit tunnel, or we're
677 * switching the protocol. In both cases, we're very unlikely to
678 * understand the next protocols. We have to switch to tunnel
679 * mode, so that we transfer the request and responses then let
680 * this protocol pass unmodified. When we later implement
681 * specific parsers for such protocols, we'll want to check the
682 * Upgrade header which contains information about that protocol
683 * for responses with status 101 (eg: see RFC2817 about TLS).
684 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200685 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100686 }
687 else if (h1s->flags & H1S_F_WANT_KAL) {
688 /* By default the server is in KAL mode. CLOSE mode mean
689 * it is imposed by haproxy itself. So only change KAL
690 * mode here. */
691 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
692 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
693 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
694 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
695 }
696 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200697 }
Christopher Faulet70033782018-12-05 13:50:11 +0100698 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100699 /* Output direction: first pass */
700 if (h1m->flags & H1_MF_CONN_CLO) {
701 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100702 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100703 }
704 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
705 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
706 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
707 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
708 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
709 /* no explicit keep-alive option httpclose/server-close => close */
710 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
711 }
Christopher Faulet70033782018-12-05 13:50:11 +0100712 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200713
714 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
715 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
716 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200717}
718
Christopher Fauletb992af02019-03-28 15:42:24 +0100719static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200720{
721 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200722
723 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
724 * token is found
725 */
726 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200727 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200728
729 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100730 if (!(h1m->flags & H1_MF_VER_11))
731 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200732 }
733 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100734 if (h1m->flags & H1_MF_VER_11)
735 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200736 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200737}
738
Christopher Fauletb992af02019-03-28 15:42:24 +0100739static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200740{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200741 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
742 * token is found
743 */
744 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200745 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200746
747 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100748 if (!(h1m->flags & H1_MF_VER_11) ||
749 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
750 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200751 }
752 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100753 if (h1m->flags & H1_MF_VER_11)
754 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200755 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200756}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200757
Christopher Fauletb992af02019-03-28 15:42:24 +0100758static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200759{
Christopher Fauletb992af02019-03-28 15:42:24 +0100760 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200761 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100762 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200763 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200764}
765
Christopher Fauletb992af02019-03-28 15:42:24 +0100766static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
767{
768 if (!conn_is_back(h1s->h1c->conn))
769 h1_set_cli_conn_mode(h1s, h1m);
770 else
771 h1_set_srv_conn_mode(h1s, h1m);
772
773 if (!(h1m->flags & H1_MF_RESP))
774 h1_update_req_conn_value(h1s, h1m, conn_val);
775 else
776 h1_update_res_conn_value(h1s, h1m, conn_val);
777}
Christopher Faulete44769b2018-11-29 23:01:45 +0100778
779/* Append the description of what is present in error snapshot <es> into <out>.
780 * The description must be small enough to always fit in a buffer. The output
781 * buffer may be the trash so the trash must not be used inside this function.
782 */
783static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
784{
785 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100786 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
787 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
788 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
789 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
790 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
791 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100792}
793/*
794 * Capture a bad request or response and archive it in the proxy's structure.
795 * By default it tries to report the error position as h1m->err_pos. However if
796 * this one is not set, it will then report h1m->next, which is the last known
797 * parsing point. The function is able to deal with wrapping buffers. It always
798 * displays buffers as a contiguous area starting at buf->p. The direction is
799 * determined thanks to the h1m's flags.
800 */
801static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
802 struct h1m *h1m, struct buffer *buf)
803{
804 struct session *sess = h1c->conn->owner;
805 struct proxy *proxy = h1c->px;
806 struct proxy *other_end = sess->fe;
807 union error_snapshot_ctx ctx;
808
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100809 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100810 other_end = si_strm(h1s->cs->data)->be;
811
812 /* http-specific part now */
813 ctx.h1.state = h1m->state;
814 ctx.h1.c_flags = h1c->flags;
815 ctx.h1.s_flags = h1s->flags;
816 ctx.h1.m_flags = h1m->flags;
817 ctx.h1.m_clen = h1m->curr_len;
818 ctx.h1.m_blen = h1m->body_len;
819
820 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
821 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100822 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
823 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100824}
825
Christopher Fauletadb22202018-12-12 10:32:09 +0100826/* Emit the chunksize followed by a CRLF in front of data of the buffer
827 * <buf>. It goes backwards and starts with the byte before the buffer's
828 * head. The caller is responsible for ensuring there is enough room left before
829 * the buffer's head for the string.
830 */
831static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
832{
833 char *beg, *end;
834
835 beg = end = b_head(buf);
836 *--beg = '\n';
837 *--beg = '\r';
838 do {
839 *--beg = hextab[chksz & 0xF];
840 } while (chksz >>= 4);
841 buf->head -= (end - beg);
842 b_add(buf, end - beg);
843}
844
845/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
846 * ensuring there is enough room left in the buffer for the string. */
847static void h1_emit_chunk_crlf(struct buffer *buf)
848{
849 *(b_peek(buf, b_data(buf))) = '\r';
850 *(b_peek(buf, b_data(buf) + 1)) = '\n';
851 b_add(buf, 2);
852}
853
Christopher Faulet129817b2018-09-20 16:14:40 +0200854/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100855 * Switch the request to tunnel mode. This function must only be called for
856 * CONNECT requests. On the client side, the mux is mark as busy on input,
857 * waiting the response.
858 */
859static void h1_set_req_tunnel_mode(struct h1s *h1s)
860{
861 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
862 h1s->req.state = H1_MSG_TUNNEL;
863 if (!conn_is_back(h1s->h1c->conn))
864 h1s->h1c->flags |= H1C_F_IN_BUSY;
865}
866
867/*
868 * Switch the response to tunnel mode. This function must only be called on
869 * successfull replies to CONNECT requests or on protocol switching. On the
870 * server side, if the request is not finished, the mux is mark as busy on
871 * input. Otherwise the request is also switch to tunnel mode.
872 */
873static void h1_set_res_tunnel_mode(struct h1s *h1s)
874{
875 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
876 h1s->res.state = H1_MSG_TUNNEL;
877 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
878 h1s->h1c->flags |= H1C_F_IN_BUSY;
879 else {
880 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
881 h1s->req.state = H1_MSG_TUNNEL;
882 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
883 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
884 tasklet_wakeup(h1s->h1c->wait_event.task);
885 }
886 }
887}
888
Christopher Faulet30db3d72019-05-17 15:35:33 +0200889/* Estimate the size of the HTX request after the parsing. */
890static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
891{
892 size_t sz;
893 int i;
894
895 /* size of the HTX start-line */
896 sz = sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
897
898 /* size of all HTX headers */
899 for (i = 0; hdrs[i].n.len; i++)
900 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
901
902 /* size of the EOH */
903 sz += sizeof(struct htx_blk) + 1;
904
905 /* size of the EOM */
906 if (h1m->state == H1_MSG_DONE)
907 sz += sizeof(struct htx_blk) + 1;
908
909 return sz;
910}
911
912/* Estimate the size of the HTX response after the parsing. */
913static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
914{
915 size_t sz;
916 int i;
917
918 /* size of the HTX start-line */
919 sz = sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len;
920
921 /* size of all HTX headers */
922 for (i = 0; hdrs[i].n.len; i++)
923 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
924
925 /* size of the EOH */
926 sz += sizeof(struct htx_blk) + 1;
927
928 /* size of the EOM */
929 if (h1m->state == H1_MSG_DONE)
930 sz += sizeof(struct htx_blk) + 1;
931
932 return sz;
933}
934
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100935/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200936 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200937 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
938 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800939 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200940 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200941static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200942 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200943{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200944 struct htx_sl *sl;
Christopher Faulet129817b2018-09-20 16:14:40 +0200945 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100946 union h1_sl h1sl;
947 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200948 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +0200949 int ret = 0;
950
Christopher Faulet30db3d72019-05-17 15:35:33 +0200951 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100952 goto end;
953
Christopher Faulet129817b2018-09-20 16:14:40 +0200954 /* Realing input buffer if necessary */
955 if (b_head(buf) + b_data(buf) > b_wrap(buf))
956 b_slow_realign(buf, trash.area, 0);
957
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200958 if (!(h1m->flags & H1_MF_RESP)) {
959 /* Try to match H2 preface before parsing the request headers. */
960 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
961 if (ret > 0)
962 goto h2c_upgrade;
963 }
964
Christopher Faulet30db3d72019-05-17 15:35:33 +0200965 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100966 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200967 if (ret <= 0) {
968 /* Incomplete or invalid message. If the buffer is full, it's an
969 * error because headers are too large to be handled by the
970 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200971 if (ret < 0 || (!ret && b_full(buf)))
972 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200973 goto end;
974 }
975
976 /* messages headers fully parsed, do some checks to prepare the body
977 * parsing.
978 */
979
Christopher Faulet9768c262018-10-22 09:34:31 +0200980 /* Save the request's method or the response's status, check if the body
981 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200982 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100983 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200984
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100985 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +0200986 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100987
988 if (h1s->meth == HTTP_METH_CONNECT) {
989 /* Switch CONNECT requests to tunnel mode */
990 h1_set_req_tunnel_mode(h1s);
991 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200992
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100993 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
994 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200995 h1m->err_state = h1m->state;
996 goto vsn_error;
997 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200998 }
999 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001000 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001001
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001002 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1003 h1s->status == 101) {
1004 /* Switch successfull replies to CONNECT requests and
1005 * protocol switching to tunnel mode. */
1006 h1_set_res_tunnel_mode(h1s);
1007 }
1008 else if ((h1s->meth == HTTP_METH_HEAD) ||
1009 (h1s->status >= 100 && h1s->status < 200) ||
1010 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001011 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001012 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1013 h1m->flags |= H1_MF_XFER_LEN;
1014 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001015 }
1016 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001017 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001018 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001019 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001020 else {
1021 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001022 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001023 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001024
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001025 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1026 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001027 h1m->err_state = h1m->state;
1028 goto vsn_error;
1029 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001030 }
1031
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001032 /* Set HTX start-line flags */
1033 if (h1m->flags & H1_MF_VER_11)
1034 flags |= HTX_SL_F_VER_11;
1035 if (h1m->flags & H1_MF_XFER_ENC)
1036 flags |= HTX_SL_F_XFER_ENC;
1037 if (h1m->flags & H1_MF_XFER_LEN) {
1038 flags |= HTX_SL_F_XFER_LEN;
1039 if (h1m->flags & H1_MF_CHNK)
1040 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001041 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001042 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001043 if (h1m->body_len == 0)
1044 flags |= HTX_SL_F_BODYLESS;
1045 }
1046 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001047 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001048 }
1049
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001050 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001051 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001052 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1053 if (htx_is_empty(htx))
1054 goto error;
1055 h1m_init_req(h1m);
1056 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1057 ret = 0;
1058 goto end;
1059 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001060
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001061 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1062 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001063 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001064 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001065 }
1066 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001067 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1068 if (htx_is_empty(htx))
1069 goto error;
1070 h1m_init_res(h1m);
1071 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1072 ret = 0;
1073 goto end;
1074 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001075
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001076 flags |= HTX_SL_F_IS_RESP;
1077 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1078 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001079 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001080 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001081 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001082
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001083 /* Set bytes used in the HTX mesage for the headers now */
1084 sl->hdrs_bytes = htx_used_space(htx) - used;
1085
Christopher Fauletb992af02019-03-28 15:42:24 +01001086 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001087
1088 /* If body length cannot be determined, set htx->extra to
1089 * ULLONG_MAX. This value is impossible in other cases.
1090 */
1091 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001093 end:
1094 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001095
1096 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001097 h1m->err_state = h1m->state;
1098 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 vsn_error:
1100 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001101 h1s->cs->flags |= CS_FL_EOI;
1102 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001103 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001104 ret = 0;
1105 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001106
1107 h2c_upgrade:
1108 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001109 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001110 htx->flags |= HTX_FL_UPGRADE;
1111 ret = 0;
1112 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001113}
1114
1115/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1117 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001118 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001119 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001120 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001121static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001122 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001123 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001124{
1125 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001126 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001127
Christopher Faulet129817b2018-09-20 16:14:40 +02001128 if (h1m->flags & H1_MF_XFER_LEN) {
1129 if (h1m->flags & H1_MF_CLEN) {
1130 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001131 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001132 if ((uint64_t)ret > h1m->curr_len)
1133 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001134 if (ret > b_contig_data(buf, *ofs))
1135 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001136
Christopher Faulet9768c262018-10-22 09:34:31 +02001137 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001138 /* very often with large files we'll face the following
1139 * situation :
1140 * - htx is empty and points to <htxbuf>
1141 * - ret == buf->data
1142 * - buf->head == sizeof(struct htx)
1143 * => we can swap the buffers and place an htx header into
1144 * the target buffer instead
1145 */
1146 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1147 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1148 void *raw_area = buf->area;
1149 void *htx_area = htxbuf->area;
1150 struct htx_blk *blk;
1151
1152 buf->area = htx_area;
1153 htxbuf->area = raw_area;
1154 htx = (struct htx *)htxbuf->area;
1155 htx->size = htxbuf->size - sizeof(*htx);
1156 htx_reset(htx);
1157 b_set_data(htxbuf, b_size(htxbuf));
1158
1159 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1160 blk->info += ret;
1161 /* nothing else to do, the old buffer now contains an
1162 * empty pre-initialized HTX header
1163 */
1164 }
1165 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001166 goto end;
1167 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001168 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001169 *ofs += ret;
1170 total += ret;
1171 }
1172
1173 if (!h1m->curr_len) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001174 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001175 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001176 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001177 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet9768c262018-10-22 09:34:31 +02001178 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001179 }
1180 else if (h1m->flags & H1_MF_CHNK) {
1181 new_chunk:
1182 /* te:chunked : parse chunks */
1183 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001184 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001185 if (ret <= 0)
1186 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001187 h1m->state = H1_MSG_CHUNK_SIZE;
1188
Christopher Fauletf2824e62018-10-01 12:12:37 +02001189 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001190 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001191 }
1192
1193 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1194 unsigned int chksz;
1195
Christopher Faulet30db3d72019-05-17 15:35:33 +02001196 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001197 if (ret <= 0)
1198 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001199 if (!chksz) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001200 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOD))
Christopher Faulet9768c262018-10-22 09:34:31 +02001201 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001202 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001203 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001204 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001205 }
1206 else
1207 h1m->state = H1_MSG_DATA;
1208
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 h1m->curr_len = chksz;
1210 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001211 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001212 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001213 }
1214
1215 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001216 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001217 if ((uint64_t)ret > h1m->curr_len)
1218 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001219 if (ret > b_contig_data(buf, *ofs))
1220 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001221
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 if (ret) {
1223 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1224 goto end;
1225 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001226 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001227 *ofs += ret;
1228 total += ret;
1229 }
1230 if (!h1m->curr_len) {
1231 h1m->state = H1_MSG_CHUNK_CRLF;
1232 goto new_chunk;
1233 }
1234 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001235 }
1236
1237 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001238 /* Trailers were alread parsed, only the EOM
1239 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001240 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001241 goto skip_tlr_parsing;
1242
Christopher Faulet30db3d72019-05-17 15:35:33 +02001243 ret = htx_get_max_blksz(htx, max);
1244 ret = h1_measure_trailers(buf, *ofs, ret);
1245 if (ret <= 0) {
1246 if (!ret && b_full(buf))
1247 ret = -1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001248 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001249 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001250
1251 /* Realing input buffer if tailers wrap. For now
1252 * this is a workaroung. Because trailers are
1253 * not split on CRLF, like headers, there is no
1254 * way to know where to split it when trailers
1255 * wrap. This is a limitation of
1256 * h1_measure_trailers.
1257 */
1258 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1259 b_slow_realign(buf, trash.area, 0);
1260
1261 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1262 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001263 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001264 max -= sizeof(struct htx_blk) + ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001265 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001266 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001267
Christopher Faulet17276482018-11-22 11:44:35 +01001268 skip_tlr_parsing:
Christopher Faulet30db3d72019-05-17 15:35:33 +02001269 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001270 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001271 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001272 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001273 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001274 }
1275 }
1276 else {
1277 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1278 * is no body. Switch the message in DONE state
1279 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001280 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001281 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001282 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001283 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001284 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001285 }
1286 }
1287 else {
1288 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001289 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001290 if (ret > b_contig_data(buf, *ofs))
1291 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001292
Christopher Faulet9768c262018-10-22 09:34:31 +02001293 if (ret) {
1294 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1295 goto end;
1296
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001297 *ofs += ret;
1298 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001299 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 }
1301
1302 end:
1303 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001304 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001305 h1s->cs->flags |= CS_FL_EOI;
1306 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001307 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001308 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001309 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001310 return 0;
1311 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001312 /* update htx->extra, only when the body length is known */
1313 if (h1m->flags & H1_MF_XFER_LEN)
1314 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001315 return total;
1316}
1317
1318/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001319 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001320 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1321 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001322 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001323static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001324{
Christopher Faulet539e0292018-11-19 10:40:09 +01001325 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001326 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001327 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001328 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001329 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001330 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001331
Christopher Faulet539e0292018-11-19 10:40:09 +01001332 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001333
Christopher Fauletf2824e62018-10-01 12:12:37 +02001334 if (!conn_is_back(h1c->conn)) {
1335 h1m = &h1s->req;
1336 errflag = H1S_F_REQ_ERROR;
1337 }
1338 else {
1339 h1m = &h1s->res;
1340 errflag = H1S_F_RES_ERROR;
1341 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001342
Christopher Faulet74027762019-02-26 14:45:05 +01001343 data = htx->data;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001344 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001345 size_t used = htx_used_space(htx);
1346
Christopher Faulet129817b2018-09-20 16:14:40 +02001347 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001348 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001349 if (!ret)
1350 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001351 if ((h1m->flags & H1_MF_RESP) &&
1352 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1353 h1m_init_res(&h1s->res);
1354 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1355 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001356 }
1357 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001358 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001359 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001360 if (!ret)
1361 break;
1362 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001363 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001364 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1365 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001366 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001367 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001368 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001369 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001370 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001371 if (!ret)
1372 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001373 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001374 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001375 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001376 break;
1377 }
1378
Christopher Faulet30db3d72019-05-17 15:35:33 +02001379 count -= htx_used_space(htx) - used;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001380 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001381
Christopher Faulet47365272018-10-31 17:40:50 +01001382 if (h1s->flags & errflag)
1383 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001384
Christopher Faulet539e0292018-11-19 10:40:09 +01001385 b_del(&h1c->ibuf, total);
1386
1387 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001388 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001389 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001390 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001391 h1c->flags &= ~H1C_F_IN_FULL;
1392 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001393 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001394
Christopher Fauletcf56b992018-12-11 16:12:31 +01001395 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1396
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001397 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001398 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletcf56b992018-12-11 16:12:31 +01001399 else if (!htx_is_empty(htx))
1400 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001401
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001402 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1403 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001404 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001405 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001406 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001407
Christopher Faulet30db3d72019-05-17 15:35:33 +02001408 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001409
1410 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001411 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001412 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001413 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001414}
1415
Christopher Faulet129817b2018-09-20 16:14:40 +02001416/*
1417 * Process outgoing data. It parses data and transfer them from the channel buffer into
1418 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1419 * 0 if it couldn't proceed.
1420 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001421static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1422{
Christopher Faulet129817b2018-09-20 16:14:40 +02001423 struct h1s *h1s = h1c->h1s;
1424 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001425 struct htx *chn_htx;
1426 struct htx_blk *blk;
1427 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001428 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001429 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001430 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001431
Christopher Faulet47365272018-10-31 17:40:50 +01001432 if (!count)
1433 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001434
Christopher Faulet9768c262018-10-22 09:34:31 +02001435 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001436 if (htx_is_empty(chn_htx))
1437 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001438
Christopher Faulet51dbc942018-09-13 09:05:15 +02001439 if (!h1_get_buf(h1c, &h1c->obuf)) {
1440 h1c->flags |= H1C_F_OUT_ALLOC;
1441 goto end;
1442 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001443
Christopher Fauletf2824e62018-10-01 12:12:37 +02001444 if (!conn_is_back(h1c->conn)) {
1445 h1m = &h1s->res;
1446 errflag = H1S_F_RES_ERROR;
1447 }
1448 else {
1449 h1m = &h1s->req;
1450 errflag = H1S_F_REQ_ERROR;
1451 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001452
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001453 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001454 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001455
1456 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001457
Willy Tarreau3815b222018-12-11 19:50:43 +01001458 /* Perform some optimizations to reduce the number of buffer copies.
1459 * First, if the mux's buffer is empty and the htx area contains
1460 * exactly one data block of the same size as the requested count,
1461 * then it's possible to simply swap the caller's buffer with the
1462 * mux's output buffer and adjust offsets and length to match the
1463 * entire DATA HTX block in the middle. In this case we perform a
1464 * true zero-copy operation from end-to-end. This is the situation
1465 * that happens all the time with large files. Second, if this is not
1466 * possible, but the mux's output buffer is empty, we still have an
1467 * opportunity to avoid the copy to the intermediary buffer, by making
1468 * the intermediary buffer's area point to the output buffer's area.
1469 * In this case we want to skip the HTX header to make sure that copies
1470 * remain aligned and that this operation remains possible all the
1471 * time. This goes for headers, data blocks and any data extracted from
1472 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001473 */
1474 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001475 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001476
1477 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001478 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001479 htx_get_blk_value(chn_htx, blk).len == count) {
1480 void *old_area = h1c->obuf.area;
1481
1482 h1c->obuf.area = buf->area;
1483 h1c->obuf.data = count;
1484
1485 buf->area = old_area;
1486 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001487
1488 /* The message is chunked. We need to emit the chunk
1489 * size. We have at least the size of the struct htx to
1490 * write the chunk envelope. It should be enough.
1491 */
1492 if (h1m->flags & H1_MF_CHNK) {
1493 h1_emit_chunk_size(&h1c->obuf, count);
1494 h1_emit_chunk_crlf(&h1c->obuf);
1495 }
1496
Willy Tarreau3815b222018-12-11 19:50:43 +01001497 total += count;
1498 goto out;
1499 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001500 tmp->area = h1c->obuf.area + h1c->obuf.head;
1501 }
1502
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 tmp->size = b_room(&h1c->obuf);
1504
Christopher Fauletb2e84162018-12-06 11:39:49 +01001505 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001506 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001507 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001508 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001509 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001510 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001511
Christopher Fauletb2e84162018-12-06 11:39:49 +01001512 vlen = sz;
1513 if (vlen > count) {
1514 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1515 goto copy;
1516 vlen = count;
1517 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001518
Christopher Fauletb2e84162018-12-06 11:39:49 +01001519 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001520 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001521 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001522
1523 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001524 h1m_init_req(h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001525 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +02001526 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001527 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001528 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001529 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001530 goto copy;
1531 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001532 if (sl->flags & HTX_SL_F_BODYLESS)
1533 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001535 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001536
Christopher Faulet9768c262018-10-22 09:34:31 +02001537 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001538 h1m_init_res(h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001539 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +02001540 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001541 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001542 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001543 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001544 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001545 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001546 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001547 if (sl->info.res.status < 200 &&
1548 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1549 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001550 h1m->state = H1_MSG_HDR_FIRST;
1551 break;
1552
1553 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 h1m->state = H1_MSG_HDR_NAME;
1555 n = htx_get_blk_name(chn_htx, blk);
1556 v = htx_get_blk_value(chn_htx, blk);
1557
1558 if (isteqi(n, ist("transfer-encoding")))
1559 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001560 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001561 /* Only skip C-L header with invalid value. */
1562 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001563 goto skip_hdr;
1564 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001565 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001566 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001567 if (!v.len)
1568 goto skip_hdr;
1569 }
1570
Christopher Fauletc59ff232018-12-03 13:58:44 +01001571 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001572 goto copy;
1573 skip_hdr:
1574 h1m->state = H1_MSG_HDR_L2_LWS;
1575 break;
1576
1577 case HTX_BLK_PHDR:
1578 /* not implemented yet */
1579 h1m->flags |= errflag;
1580 break;
1581
1582 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001583 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001584 /* There is no "Connection:" header and
1585 * it the conn_mode must be
1586 * processed. So do it */
1587 n = ist("Connection");
1588 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001589 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001590 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001591 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001592 goto copy;
1593 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001594 process_conn_mode = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001595 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001596
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001597 if ((h1s->meth != HTTP_METH_CONNECT &&
1598 (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 +01001599 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1600 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1601 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1602 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1603 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001604 /* chunking needed but header not seen */
1605 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1606 goto copy;
1607 h1m->flags |= H1_MF_CHNK;
1608 }
1609
Christopher Faulet9768c262018-10-22 09:34:31 +02001610 h1m->state = H1_MSG_LAST_LF;
1611 if (!chunk_memcat(tmp, "\r\n", 2))
1612 goto copy;
1613
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001614 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1615 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1616 h1_set_req_tunnel_mode(h1s);
1617 }
1618 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1619 /* a successfull reply to a CONNECT or a protocol switching is sent
1620 * to the client . Switch the response to tunnel mode. */
1621 h1_set_res_tunnel_mode(h1s);
1622 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001623 else if ((h1m->flags & H1_MF_RESP) &&
1624 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1625 h1m_init_res(&h1s->res);
1626 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1627 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001628 else
1629 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001630 break;
1631
1632 case HTX_BLK_DATA:
1633 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001634 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001635 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001636 goto copy;
1637 break;
1638
1639 case HTX_BLK_EOD:
1640 if (!chunk_memcat(tmp, "0\r\n", 3))
1641 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001642 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001643 h1m->state = H1_MSG_TRAILERS;
1644 break;
1645
1646 case HTX_BLK_TLR:
Willy Tarreau34d23482019-01-03 17:46:56 +01001647 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001648 if (!chunk_memcat(tmp, "0\r\n", 3))
1649 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001650 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001651 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001652 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001653 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001654 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001655 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001656 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001657 break;
1658
1659 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001660 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001661 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001662 if (!chunk_memcat(tmp, "0\r\n", 3))
1663 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001664 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001665 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001666 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001667 if (!chunk_memcat(tmp, "\r\n", 2))
1668 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001669 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001670 }
1671 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001672 h1m->state = H1_MSG_DONE;
1673 break;
1674
Christopher Faulet9768c262018-10-22 09:34:31 +02001675 default:
1676 h1m->flags |= errflag;
1677 break;
1678 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001679 total += vlen;
1680 count -= vlen;
1681 if (sz == vlen)
1682 blk = htx_remove_blk(chn_htx, blk);
1683 else {
1684 htx_cut_data_blk(chn_htx, blk, vlen);
1685 break;
1686 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001687 }
1688
Christopher Faulet9768c262018-10-22 09:34:31 +02001689 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001690 /* when the output buffer is empty, tmp shares the same area so that we
1691 * only have to update pointers and lengths.
1692 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001693 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001694 h1c->obuf.data = tmp->data;
1695 else
1696 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001697
Willy Tarreau3815b222018-12-11 19:50:43 +01001698 htx_to_buf(chn_htx, buf);
1699 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001700 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001701 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001702 end:
1703 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001704}
1705
Christopher Faulet51dbc942018-09-13 09:05:15 +02001706/*********************************************************/
1707/* functions below are I/O callbacks from the connection */
1708/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001709static void h1_wake_stream_for_recv(struct h1s *h1s)
1710{
1711 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001712 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001713 tasklet_wakeup(h1s->recv_wait->task);
1714 h1s->recv_wait = NULL;
1715 }
1716}
1717static void h1_wake_stream_for_send(struct h1s *h1s)
1718{
1719 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001720 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001721 tasklet_wakeup(h1s->send_wait->task);
1722 h1s->send_wait = NULL;
1723 }
1724}
1725
Christopher Faulet51dbc942018-09-13 09:05:15 +02001726/*
1727 * Attempt to read data, and subscribe if none available
1728 */
1729static int h1_recv(struct h1c *h1c)
1730{
1731 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001732 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001733 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001734 int rcvd = 0;
1735
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001736 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001737 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001738
Olivier Houchard75159a92018-12-03 18:46:09 +01001739 if (!h1_recv_allowed(h1c)) {
1740 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001741 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001742 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001743
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001744 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1745 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001746 goto end;
1747 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001748
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001749 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
1750 if (!b_data(&h1c->ibuf))
1751 h1_wake_stream_for_recv(h1s);
1752 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001753 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001754 }
1755
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001756 /*
1757 * If we only have a small amount of data, realign it,
1758 * it's probably cheaper than doing 2 recv() calls.
1759 */
1760 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1761 b_slow_realign(&h1c->ibuf, trash.area, 0);
1762
Willy Tarreau45f2b892018-12-05 07:59:27 +01001763 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001764 if (max) {
1765 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001766
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001767 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001768 if (!b_data(&h1c->ibuf)) {
1769 /* try to pre-align the buffer like the rxbufs will be
1770 * to optimize memory copies.
1771 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001772 h1c->ibuf.head = sizeof(struct htx);
1773 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001774 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001775 }
Christopher Faulet47365272018-10-31 17:40:50 +01001776 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001777 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001778 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001779 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001780 if (h1s->csinfo.t_idle == -1)
1781 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1782 }
Christopher Faulet47365272018-10-31 17:40:50 +01001783 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001784
Christopher Fauletcf56b992018-12-11 16:12:31 +01001785 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001786 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001787 goto end;
1788 }
1789
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001790 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001791
Christopher Faulet9768c262018-10-22 09:34:31 +02001792 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001793 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1794 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001795
Christopher Faulete6b39942018-12-07 09:42:49 +01001796 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Christopher Faulet87a8f352019-03-22 14:51:36 +01001797 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001798 if (!b_data(&h1c->ibuf))
1799 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001800 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001801 h1c->flags |= H1C_F_IN_FULL;
1802 return rcvd;
1803}
1804
1805
1806/*
1807 * Try to send data if possible
1808 */
1809static int h1_send(struct h1c *h1c)
1810{
1811 struct connection *conn = h1c->conn;
1812 unsigned int flags = 0;
1813 size_t ret;
1814 int sent = 0;
1815
1816 if (conn->flags & CO_FL_ERROR)
1817 return 0;
1818
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001819 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001820 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001821 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001822 return 0;
1823 }
1824
Christopher Faulet51dbc942018-09-13 09:05:15 +02001825 if (!b_data(&h1c->obuf))
1826 goto end;
1827
1828 if (h1c->flags & H1C_F_OUT_FULL)
1829 flags |= CO_SFL_MSG_MORE;
1830
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001831 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001832 if (ret > 0) {
1833 h1c->flags &= ~H1C_F_OUT_FULL;
1834 b_del(&h1c->obuf, ret);
1835 sent = 1;
1836 }
1837
Christopher Faulet145aa472018-12-06 10:56:20 +01001838 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1839 /* error or output closed, nothing to send, clear the buffer to release it */
1840 b_reset(&h1c->obuf);
1841 }
1842
Christopher Faulet51dbc942018-09-13 09:05:15 +02001843 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001844 if (!(h1c->flags & H1C_F_OUT_FULL))
1845 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001846
Christopher Faulet51dbc942018-09-13 09:05:15 +02001847 /* We're done, no more to send */
1848 if (!b_data(&h1c->obuf)) {
1849 h1_release_buf(h1c, &h1c->obuf);
1850 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001851 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001852 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001853 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001854 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001855
1856 return sent;
1857}
1858
Christopher Faulet51dbc942018-09-13 09:05:15 +02001859
1860/* callback called on any event by the connection handler.
1861 * It applies changes and returns zero, or < 0 if it wants immediate
1862 * destruction of the connection.
1863 */
1864static int h1_process(struct h1c * h1c)
1865{
1866 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001867 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001868
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001869 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001870 return -1;
1871
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001872 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001873 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1874 goto end;
1875 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001876 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001877 }
1878
Christopher Fauletfeb11742018-11-29 15:12:34 +01001879 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001880 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001881 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001882 conn_xprt_read0_pending(conn))
1883 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001884 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001885 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001886 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001887 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001888 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001889 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001890 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001891 }
1892
Christopher Fauletfeb11742018-11-29 15:12:34 +01001893 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1894 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1895
Olivier Houchard75159a92018-12-03 18:46:09 +01001896 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1897 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001898 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001899 int flags = 0;
1900
1901 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1902 flags |= CS_FL_ERROR;
1903 if (conn_xprt_read0_pending(conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001904 flags |= CS_FL_REOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001905 h1s->cs->flags |= flags;
1906 h1s->cs->data_cb->wake(h1s->cs);
1907 }
Christopher Faulet47365272018-10-31 17:40:50 +01001908 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001909 if (h1c->task) {
1910 h1c->task->expire = TICK_ETERNITY;
1911 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001912 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 +01001913 ? h1c->shut_timeout
1914 : h1c->timeout));
1915 task_queue(h1c->task);
1916 }
1917 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001918 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001919
1920 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02001921 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01001922 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001923}
1924
1925static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1926{
1927 struct h1c *h1c = ctx;
1928 int ret = 0;
1929
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001930 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001932 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001933 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001934 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001935 h1_process(h1c);
1936 return NULL;
1937}
1938
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001939static void h1_reset(struct connection *conn)
1940{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001941 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001942
1943 /* Reset the flags, and let the mux know we're waiting for a connection */
1944 h1c->flags = H1C_F_CS_WAIT_CONN;
1945}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001946
1947static int h1_wake(struct connection *conn)
1948{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001949 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001950 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001951
Christopher Faulet539e0292018-11-19 10:40:09 +01001952 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001953 ret = h1_process(h1c);
1954 if (ret == 0) {
1955 struct h1s *h1s = h1c->h1s;
1956
1957 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1958 ret = h1s->cs->data_cb->wake(h1s->cs);
1959 }
1960 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001961}
1962
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001963/* Connection timeout management. The principle is that if there's no receipt
1964 * nor sending for a certain amount of time, the connection is closed.
1965 */
1966static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1967{
1968 struct h1c *h1c = context;
1969 int expired = tick_is_expired(t->expire, now_ms);
1970
1971 if (!expired && h1c)
1972 return t;
1973
Olivier Houchard3f795f72019-04-17 22:51:06 +02001974 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001975
1976 if (!h1c) {
1977 /* resources were already deleted */
1978 return NULL;
1979 }
1980
1981 h1c->task = NULL;
1982 /* If a stream is still attached to the mux, just set an error and wait
1983 * for the stream's timeout. Otherwise, release the mux. This is only ok
1984 * because same timeouts are used.
1985 */
1986 if (h1c->h1s && h1c->h1s->cs)
1987 h1c->flags |= H1C_F_CS_ERROR;
1988 else
Christopher Faulet73c12072019-04-08 11:23:22 +02001989 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001990 return NULL;
1991}
1992
Christopher Faulet51dbc942018-09-13 09:05:15 +02001993/*******************************************/
1994/* functions below are used by the streams */
1995/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02001996
Christopher Faulet51dbc942018-09-13 09:05:15 +02001997/*
1998 * Attach a new stream to a connection
1999 * (Used for outgoing connections)
2000 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002001static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002002{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002003 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002004 struct conn_stream *cs = NULL;
2005 struct h1s *h1s;
2006
2007 if (h1c->flags & H1C_F_CS_ERROR)
2008 goto end;
2009
2010 cs = cs_new(h1c->conn);
2011 if (!cs)
2012 goto end;
2013
Olivier Houchardf502aca2018-12-14 19:42:40 +01002014 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002015 if (h1s == NULL)
2016 goto end;
2017
2018 return cs;
2019 end:
2020 cs_free(cs);
2021 return NULL;
2022}
2023
2024/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2025 * this mux, it's easy as we can only store a single conn_stream.
2026 */
2027static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2028{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002029 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002030 struct h1s *h1s = h1c->h1s;
2031
2032 if (h1s)
2033 return h1s->cs;
2034
2035 return NULL;
2036}
2037
Christopher Faulet73c12072019-04-08 11:23:22 +02002038static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002039{
Christopher Faulet73c12072019-04-08 11:23:22 +02002040 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002041
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002042 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002043 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002044}
2045
2046/*
2047 * Detach the stream from the connection and possibly release the connection.
2048 */
2049static void h1_detach(struct conn_stream *cs)
2050{
2051 struct h1s *h1s = cs->ctx;
2052 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002053 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002054 int has_keepalive;
2055 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002056
2057 cs->ctx = NULL;
2058 if (!h1s)
2059 return;
2060
Olivier Houchardf502aca2018-12-14 19:42:40 +01002061 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002062 h1c = h1s->h1c;
2063 h1s->cs = NULL;
2064
Olivier Houchard8a786902018-12-15 16:05:40 +01002065 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2066 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2067 h1s_destroy(h1s);
2068
2069 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002070 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002071 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002072 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002073 h1c->conn->flags |= CO_FL_PRIVATE;
2074
Olivier Houchard44d59142018-12-13 18:46:22 +01002075 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002076 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002077 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2078 h1c->conn->owner = NULL;
2079 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2080 /* The server doesn't want it, let's kill the connection right away */
2081 h1c->conn->mux->destroy(h1c->conn);
2082 else
2083 tasklet_wakeup(h1c->wait_event.task);
2084 return;
2085
2086 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002087 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002088 if (h1c->conn->owner == sess) {
2089 int ret = session_check_idle_conn(sess, h1c->conn);
2090 if (ret == -1)
2091 /* The connection got destroyed, let's leave */
2092 return;
2093 else if (ret == 1) {
2094 /* The connection was added to the server list,
2095 * wake the task so we can subscribe to events
2096 */
2097 tasklet_wakeup(h1c->wait_event.task);
2098 return;
2099 }
2100 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002101 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002102 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002103 struct server *srv = objt_server(h1c->conn->target);
2104
2105 if (srv) {
2106 if (h1c->conn->flags & CO_FL_PRIVATE)
2107 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002108 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002109 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2110 else
2111 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2112 }
2113 }
2114 }
2115
Christopher Faulet51dbc942018-09-13 09:05:15 +02002116 /* We don't want to close right now unless the connection is in error */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002117 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002118 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002119 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002120 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002121 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002122 if (h1c->task) {
2123 h1c->task->expire = TICK_ETERNITY;
2124 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002125 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 +01002126 ? h1c->shut_timeout
2127 : h1c->timeout));
2128 task_queue(h1c->task);
2129 }
2130 }
2131 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002132}
2133
2134
2135static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2136{
2137 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002138 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002139
2140 if (!h1s)
2141 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002142 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002143
Christopher Faulet7f366362019-04-08 10:51:20 +02002144 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2145 goto do_shutr;
2146
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002147 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002148 return;
2149
Christopher Faulet7f366362019-04-08 10:51:20 +02002150 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002151 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2152 if (cs->flags & CS_FL_SHR)
2153 return;
2154 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002155 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2156 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002157 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 +02002158 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159}
2160
2161static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2162{
2163 struct h1s *h1s = cs->ctx;
2164 struct h1c *h1c;
2165
2166 if (!h1s)
2167 return;
2168 h1c = h1s->h1c;
2169
Christopher Faulet7f366362019-04-08 10:51:20 +02002170 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2171 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002172
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002173 if ((h1c->flags & H1C_F_UPG_H2C) ||
2174 ((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 +02002175 return;
2176
Christopher Faulet7f366362019-04-08 10:51:20 +02002177 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002178 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2179 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2180 return;
2181
Christopher Faulet666a0c42019-01-08 11:12:04 +01002182 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002183}
2184
Christopher Faulet666a0c42019-01-08 11:12:04 +01002185static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002186{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002187 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002188
Christopher Faulet666a0c42019-01-08 11:12:04 +01002189 conn_xprt_shutw(conn);
2190 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2191 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2192 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002193}
2194
2195/* Called from the upper layer, to unsubscribe to events */
2196static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2197{
2198 struct wait_event *sw;
2199 struct h1s *h1s = cs->ctx;
2200
2201 if (!h1s)
2202 return 0;
2203
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002204 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002205 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002206 BUG_ON(h1s->recv_wait != sw);
2207 sw->events &= ~SUB_RETRY_RECV;
2208 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002209 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002210 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002211 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002212 BUG_ON(h1s->send_wait != sw);
2213 sw->events &= ~SUB_RETRY_SEND;
2214 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002215 }
2216 return 0;
2217}
2218
2219/* Called from the upper layer, to subscribe to events, such as being able to send */
2220static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2221{
2222 struct wait_event *sw;
2223 struct h1s *h1s = cs->ctx;
2224
2225 if (!h1s)
2226 return -1;
2227
2228 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002229 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002230 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002231 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2232 sw->events |= SUB_RETRY_RECV;
2233 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002234 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002235 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002236 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002237 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2238 sw->events |= SUB_RETRY_SEND;
2239 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002240 return 0;
2241 default:
2242 break;
2243 }
2244 return -1;
2245}
2246
2247/* Called from the upper layer, to receive data */
2248static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2249{
2250 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002251 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002252 size_t ret = 0;
2253
Christopher Faulet539e0292018-11-19 10:40:09 +01002254 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002255 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002256
Christopher Faulete18777b2019-04-16 16:46:36 +02002257 if (flags & CO_RFL_BUF_FLUSH) {
2258 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2259
2260 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2261 h1s->flags |= H1S_F_BUF_FLUSH;
2262 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002263 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2264 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002265 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002266 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002267 }
2268 return ret;
2269}
2270
2271
2272/* Called from the upper layer, to send data */
2273static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2274{
2275 struct h1s *h1s = cs->ctx;
2276 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002277 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002278
2279 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002280 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002281
2282 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002283 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2284 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002285
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002286 while (total != count) {
2287 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002288
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002289 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2290 ret = h1_process_output(h1c, buf, count);
2291 if (!ret)
2292 break;
2293 total += ret;
2294 if (!h1_send(h1c))
2295 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002296 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002297
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002298 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002299}
2300
Willy Tarreaue5733232019-05-22 19:24:06 +02002301#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002302/* Send and get, using splicing */
2303static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2304{
2305 struct h1s *h1s = cs->ctx;
2306 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2307 int ret = 0;
2308
Christopher Faulete18777b2019-04-16 16:46:36 +02002309 if ((h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) ||
2310 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
2311 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002312 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002313 goto end;
2314 }
2315
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002316 if (b_data(&h1s->h1c->ibuf)) {
2317 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002318 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002319 }
2320
2321 h1s->flags &= ~H1S_F_BUF_FLUSH;
2322 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002323 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2324 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002325 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulete18777b2019-04-16 16:46:36 +02002326 if (h1m->state == H1_MSG_DATA && ret > 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002327 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002328 if (!h1m->curr_len)
2329 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2330 }
2331
Christopher Faulet1be55f92018-10-02 15:59:23 +02002332 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002333 if (conn_xprt_read0_pending(cs->conn)) {
2334 cs->flags |= CS_FL_REOS;
2335 if (!pipe->data)
2336 cs->flags |= CS_FL_EOS;
2337 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002338 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002339}
2340
2341static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2342{
2343 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002344 int ret = 0;
2345
2346 if (b_data(&h1s->h1c->obuf))
2347 goto end;
2348
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002349 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002350 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002351 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002352 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002353 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002354 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002355 return ret;
2356}
2357#endif
2358
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002359/* for debugging with CLI's "show fd" command */
2360static void h1_show_fd(struct buffer *msg, struct connection *conn)
2361{
2362 struct h1c *h1c = conn->ctx;
2363 struct h1s *h1s = h1c->h1s;
2364
Christopher Fauletf376a312019-01-04 15:16:06 +01002365 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2366 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002367 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2368 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2369 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2370 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2371
2372 if (h1s) {
2373 char *method;
2374
2375 if (h1s->meth < HTTP_METH_OTHER)
2376 method = http_known_methods[h1s->meth].ptr;
2377 else
2378 method = "UNKNOWN";
2379 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2380 " .meth=%s status=%d",
2381 h1s, h1s->flags,
2382 h1m_state_str(h1s->req.state),
2383 h1m_state_str(h1s->res.state), method, h1s->status);
2384 if (h1s->cs)
2385 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2386 h1s->cs->flags, h1s->cs->data);
2387 }
2388}
2389
Christopher Faulet51dbc942018-09-13 09:05:15 +02002390/****************************************/
2391/* MUX initialization and instanciation */
2392/****************************************/
2393
2394/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002395static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002396 .init = h1_init,
2397 .wake = h1_wake,
2398 .attach = h1_attach,
2399 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002400 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002401 .detach = h1_detach,
2402 .destroy = h1_destroy,
2403 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002404 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002405 .rcv_buf = h1_rcv_buf,
2406 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002407#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002408 .rcv_pipe = h1_rcv_pipe,
2409 .snd_pipe = h1_snd_pipe,
2410#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002411 .subscribe = h1_subscribe,
2412 .unsubscribe = h1_unsubscribe,
2413 .shutr = h1_shutr,
2414 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002415 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002416 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002417 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002418 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002419};
2420
2421
2422/* this mux registers default HTX proto */
2423static struct mux_proto_list mux_proto_htx =
2424{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2425
Willy Tarreau0108d902018-11-25 19:14:37 +01002426INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2427
Christopher Faulet51dbc942018-09-13 09:05:15 +02002428/*
2429 * Local variables:
2430 * c-indent-level: 8
2431 * c-basic-offset: 8
2432 * End:
2433 */