blob: 7eff9d2f139d07dbff1c3f760163d92adfe2073f [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>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010015#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010016#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020017
Christopher Faulet1be55f92018-10-02 15:59:23 +020018#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020019#include <types/proxy.h>
20#include <types/session.h>
21
Christopher Faulet51dbc942018-09-13 09:05:15 +020022#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020023#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020024#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020026#include <proto/stream.h>
27#include <proto/stream_interface.h>
28
29/*
30 * H1 Connection flags (32 bits)
31 */
32#define H1C_F_NONE 0x00000000
33
34/* Flags indicating why writing output data are blocked */
35#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
36#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
37/* 0x00000004 - 0x00000008 unused */
38
39/* Flags indicating why reading input data are blocked. */
40#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
41#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010042#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010043/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020044
45#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
46#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
47#define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020048#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
Christopher Fauletf2824e62018-10-01 12:12:37 +020050#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet129817b2018-09-20 16:14:40 +020051
Christopher Faulet51dbc942018-09-13 09:05:15 +020052/*
53 * H1 Stream flags (32 bits)
54 */
Christopher Faulet129817b2018-09-20 16:14:40 +020055#define H1S_F_NONE 0x00000000
56#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
58#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Christopher Faulet539e0292018-11-19 10:40:09 +010059/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020060#define H1S_F_WANT_KAL 0x00000010
61#define H1S_F_WANT_TUN 0x00000020
62#define H1S_F_WANT_CLO 0x00000040
63#define H1S_F_WANT_MSK 0x00000070
64#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010065#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010066#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010067#define H1S_F_HAVE_I_EOD 0x00000400 /* Set during input process to know the last empty chunk was processed */
68#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
69#define H1S_F_HAVE_O_EOD 0x00001000 /* Set during output process to know the last empty chunk was processed */
70#define H1S_F_HAVE_O_TLR 0x00002000 /* Set during output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020071
72/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073struct h1c {
74 struct connection *conn;
75 struct proxy *px;
76 uint32_t flags; /* Connection flags: H1C_F_* */
77
78 struct buffer ibuf; /* Input buffer to store data before parsing */
79 struct buffer obuf; /* Output buffer to store data after reformatting */
80
81 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
82 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
83
84 struct h1s *h1s; /* H1 stream descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020085};
86
87/* H1 stream descriptor */
88struct h1s {
89 struct h1c *h1c;
90 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010091 struct cs_info csinfo; /* CS info, only used for client connections */
92 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020093
Christopher Faulet51dbc942018-09-13 09:05:15 +020094 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
95 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020096
Olivier Houchardf502aca2018-12-14 19:42:40 +010097 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +020098 struct h1m req;
99 struct h1m res;
100
101 enum http_meth_t meth; /* HTTP resquest method */
102 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103};
104
105/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100106DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
107DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200108
Christopher Faulet51dbc942018-09-13 09:05:15 +0200109static int h1_recv(struct h1c *h1c);
110static int h1_send(struct h1c *h1c);
111static int h1_process(struct h1c *h1c);
112static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
113static void h1_shutw_conn(struct connection *conn);
114
115/*****************************************************/
116/* functions below are for dynamic buffer management */
117/*****************************************************/
118/*
119 * Indicates whether or not the we may call the h1_recv() function to
120 * attempt to receive data into the buffer and/or parse pending data. The
121 * condition is a bit complex due to some API limits for now. The rules are the
122 * following :
123 * - if an error or a shutdown was detected on the connection and the buffer
124 * is empty, we must not attempt to receive
125 * - if the input buffer failed to be allocated, we must not try to receive
126 * and we know there is nothing pending
127 * - if no flag indicates a blocking condition, we may attempt to receive,
128 * regardless of whether the input buffer is full or not, so that only de
129 * receiving part decides whether or not to block. This is needed because
130 * the connection API indeed prevents us from re-enabling receipt that is
131 * already enabled in a polled state, so we must always immediately stop as
132 * soon as the mux can't proceed so as never to hit an end of read with data
133 * pending in the buffers.
134 * - otherwise must may not attempt to receive
135 */
136static inline int h1_recv_allowed(const struct h1c *h1c)
137{
Christopher Fauletcf56b992018-12-11 16:12:31 +0100138 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200139 return 0;
140
Christopher Fauletcf56b992018-12-11 16:12:31 +0100141 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
142 return 0;
143
Christopher Fauletcb55f482018-12-10 11:56:47 +0100144 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200145 return 1;
146
147 return 0;
148}
149
150/*
151 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
152 * flags are used to figure what buffer was requested. It returns 1 if the
153 * allocation succeeds, in which case the connection is woken up, or 0 if it's
154 * impossible to wake up and we prefer to be woken up later.
155 */
156static int h1_buf_available(void *target)
157{
158 struct h1c *h1c = target;
159
160 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
161 h1c->flags &= ~H1C_F_IN_ALLOC;
162 if (h1_recv_allowed(h1c))
163 tasklet_wakeup(h1c->wait_event.task);
164 return 1;
165 }
166
167 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
168 h1c->flags &= ~H1C_F_OUT_ALLOC;
169 tasklet_wakeup(h1c->wait_event.task);
170 return 1;
171 }
172
Christopher Faulet51dbc942018-09-13 09:05:15 +0200173 return 0;
174}
175
176/*
177 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
178 */
179static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
180{
181 struct buffer *buf = NULL;
182
183 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
184 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
185 h1c->buf_wait.target = h1c;
186 h1c->buf_wait.wakeup_cb = h1_buf_available;
187 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
188 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
189 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
190 __conn_xprt_stop_recv(h1c->conn);
191 }
192 return buf;
193}
194
195/*
196 * Release a buffer, if any, and try to wake up entities waiting in the buffer
197 * wait queue.
198 */
199static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
200{
201 if (bptr->size) {
202 b_free(bptr);
203 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
204 }
205}
206
207static int h1_avail_streams(struct connection *conn)
208{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100209 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200210
211 return h1c->h1s ? 0 : 1;
212}
213
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100214static int h1_max_streams(struct connection *conn)
215{
216 return 1;
217}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200218
219/*****************************************************************/
220/* functions below are dedicated to the mux setup and management */
221/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100222static struct conn_stream *h1s_new_cs(struct h1s *h1s)
223{
224 struct conn_stream *cs;
225
226 cs = cs_new(h1s->h1c->conn);
227 if (!cs)
228 goto err;
229 h1s->cs = cs;
230 cs->ctx = h1s;
231
232 if (h1s->flags & H1S_F_NOT_FIRST)
233 cs->flags |= CS_FL_NOT_FIRST;
234
235 if (stream_create_from_cs(cs) < 0)
236 goto err;
237 return cs;
238
239 err:
240 cs_free(cs);
241 h1s->cs = NULL;
242 return NULL;
243}
244
Olivier Houchardf502aca2018-12-14 19:42:40 +0100245static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200246{
247 struct h1s *h1s;
248
249 h1s = pool_alloc(pool_head_h1s);
250 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100251 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200252
253 h1s->h1c = h1c;
254 h1c->h1s = h1s;
255
Olivier Houchardf502aca2018-12-14 19:42:40 +0100256 h1s->sess = sess;
257
Christopher Faulet51dbc942018-09-13 09:05:15 +0200258 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200259 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200260
261 h1s->recv_wait = NULL;
262 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200263
264 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200265 h1s->req.flags |= H1_MF_NO_PHDR;
266
Christopher Faulet129817b2018-09-20 16:14:40 +0200267 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200268 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200269
270 h1s->status = 0;
271 h1s->meth = HTTP_METH_OTHER;
272
Christopher Faulet47365272018-10-31 17:40:50 +0100273 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
274 h1s->flags |= H1S_F_NOT_FIRST;
275 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
276
Christopher Faulet129817b2018-09-20 16:14:40 +0200277 if (!conn_is_back(h1c->conn)) {
278 if (h1c->px->options2 & PR_O2_REQBUG_OK)
279 h1s->req.err_pos = -1;
280 }
281 else {
282 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
283 h1s->res.err_pos = -1;
284 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200285
Christopher Faulet539e0292018-11-19 10:40:09 +0100286 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
287 * create a new one.
288 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200289 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100290 h1s->csinfo.create_date = date;
291 h1s->csinfo.tv_create = now;
292 h1s->csinfo.t_handshake = 0;
293 h1s->csinfo.t_idle = -1;
294
Christopher Fauletf2824e62018-10-01 12:12:37 +0200295 cs->ctx = h1s;
296 h1s->cs = cs;
297 }
Christopher Faulet47365272018-10-31 17:40:50 +0100298 else {
Olivier Houchardf502aca2018-12-14 19:42:40 +0100299 /* For frontend connections we should always have a session */
300 sess = h1c->conn->owner;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100301
302 h1s->csinfo.create_date = sess->accept_date;
303 h1s->csinfo.tv_create = sess->tv_accept;
304 h1s->csinfo.t_handshake = sess->t_handshake;
305 h1s->csinfo.t_idle = -1;
306
Christopher Faulet47365272018-10-31 17:40:50 +0100307 cs = h1s_new_cs(h1s);
308 if (!cs)
309 goto fail;
310 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200311 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100312
313 fail:
314 pool_free(pool_head_h1s, h1s);
315 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200316}
317
318static void h1s_destroy(struct h1s *h1s)
319{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200320 if (h1s) {
321 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200322
Christopher Fauletf2824e62018-10-01 12:12:37 +0200323 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200324
Christopher Fauletf2824e62018-10-01 12:12:37 +0200325 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100326 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200327 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100328 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200329
Christopher Fauletcb55f482018-12-10 11:56:47 +0100330 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100331 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
332 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
333 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200334
Christopher Fauletf2824e62018-10-01 12:12:37 +0200335 cs_free(h1s->cs);
336 pool_free(pool_head_h1s, h1s);
337 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200338}
339
Christopher Fauletfeb11742018-11-29 15:12:34 +0100340static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
341{
342 struct h1s *h1s = cs->ctx;
343
344 if (h1s && !conn_is_back(cs->conn))
345 return &h1s->csinfo;
346 return NULL;
347}
348
Christopher Faulet51dbc942018-09-13 09:05:15 +0200349/*
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100350 * Initialize the mux once it's attached. It is expected that conn->ctx
Christopher Faulet51dbc942018-09-13 09:05:15 +0200351 * points to the existing conn_stream (for outgoing connections) or NULL (for
352 * incoming ones). Returns < 0 on error.
353 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100354static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200355{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200356 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200357
358 h1c = pool_alloc(pool_head_h1c);
359 if (!h1c)
360 goto fail_h1c;
361 h1c->conn = conn;
362 h1c->px = proxy;
363
364 h1c->flags = H1C_F_NONE;
365 h1c->ibuf = BUF_NULL;
366 h1c->obuf = BUF_NULL;
367 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368
Christopher Faulet51dbc942018-09-13 09:05:15 +0200369 LIST_INIT(&h1c->buf_wait.list);
370 h1c->wait_event.task = tasklet_new();
371 if (!h1c->wait_event.task)
372 goto fail;
373 h1c->wait_event.task->process = h1_io_cb;
374 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100375 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200377 if (!(conn->flags & CO_FL_CONNECTED))
378 h1c->flags |= H1C_F_CS_WAIT_CONN;
379
Christopher Fauletf2824e62018-10-01 12:12:37 +0200380 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100381 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200382 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200383
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100384 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200385
Christopher Faulet51dbc942018-09-13 09:05:15 +0200386 /* Try to read, if nothing is available yet we'll just subscribe */
387 if (h1_recv(h1c))
388 h1_process(h1c);
389
390 /* mux->wake will be called soon to complete the operation */
391 return 0;
392
393 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100394 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200395 tasklet_free(h1c->wait_event.task);
396 pool_free(pool_head_h1c, h1c);
397 fail_h1c:
398 return -1;
399}
400
401
402/* release function for a connection. This one should be called to free all
403 * resources allocated to the mux.
404 */
405static void h1_release(struct connection *conn)
406{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100407 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200408
409 LIST_DEL(&conn->list);
410
411 if (h1c) {
412 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
413 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
414 LIST_DEL(&h1c->buf_wait.list);
415 LIST_INIT(&h1c->buf_wait.list);
416 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
417 }
418
419 h1_release_buf(h1c, &h1c->ibuf);
420 h1_release_buf(h1c, &h1c->obuf);
421
Christopher Faulet51dbc942018-09-13 09:05:15 +0200422 if (h1c->wait_event.task)
423 tasklet_free(h1c->wait_event.task);
424
Christopher Fauletf2824e62018-10-01 12:12:37 +0200425 h1s_destroy(h1c->h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100426 if (h1c->wait_event.events != 0)
427 conn->xprt->unsubscribe(conn, h1c->wait_event.events,
Christopher Faulet51dbc942018-09-13 09:05:15 +0200428 &h1c->wait_event);
429 pool_free(pool_head_h1c, h1c);
430 }
431
432 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100433 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434
435 conn_stop_tracking(conn);
436 conn_full_close(conn);
437 if (conn->destroy_cb)
438 conn->destroy_cb(conn);
439 conn_free(conn);
440}
441
442/******************************************************/
443/* functions below are for the H1 protocol processing */
444/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200445/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
446 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200447 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100448static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200449{
Christopher Faulet570d1612018-11-26 11:13:57 +0100450 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200451
Christopher Faulet570d1612018-11-26 11:13:57 +0100452 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200453 (*(p + 5) > '1' ||
454 (*(p + 5) == '1' && *(p + 7) >= '1')))
455 h1m->flags |= H1_MF_VER_11;
456}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200457
Christopher Faulet9768c262018-10-22 09:34:31 +0200458/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
459 * greater or equal to 1.1
460 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100461static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200462{
Christopher Faulet570d1612018-11-26 11:13:57 +0100463 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200464
Christopher Faulet570d1612018-11-26 11:13:57 +0100465 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200466 (*(p + 5) > '1' ||
467 (*(p + 5) == '1' && *(p + 7) >= '1')))
468 h1m->flags |= H1_MF_VER_11;
469}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200470
Christopher Faulet9768c262018-10-22 09:34:31 +0200471/*
472 * Check the validity of the request version. If the version is valid, it
473 * returns 1. Otherwise, it returns 0.
474 */
475static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
476{
477 struct h1c *h1c = h1s->h1c;
478
479 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
480 * exactly one digit "." one digit. This check may be disabled using
481 * option accept-invalid-http-request.
482 */
483 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
484 if (sl.rq.v.len != 8)
485 return 0;
486
487 if (*(sl.rq.v.ptr + 4) != '/' ||
488 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
489 *(sl.rq.v.ptr + 6) != '.' ||
490 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
491 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200492 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200493 else if (!sl.rq.v.len) {
494 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200495
Christopher Faulet9768c262018-10-22 09:34:31 +0200496 /* RFC 1945 allows only GET for HTTP/0.9 requests */
497 if (sl.rq.meth != HTTP_METH_GET)
498 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200499
Christopher Faulet9768c262018-10-22 09:34:31 +0200500 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
501 if (!sl.rq.u.len)
502 return 0;
503
504 /* Add HTTP version */
505 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100506 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200507 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100508
509 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100510 ((*(sl.rq.v.ptr + 5) > '1') ||
511 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100512 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200513 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200514}
515
Christopher Faulet9768c262018-10-22 09:34:31 +0200516/*
517 * Check the validity of the response version. If the version is valid, it
518 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200519 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200520static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200521{
Christopher Faulet9768c262018-10-22 09:34:31 +0200522 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200523
Christopher Faulet9768c262018-10-22 09:34:31 +0200524 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
525 * exactly one digit "." one digit. This check may be disabled using
526 * option accept-invalid-http-request.
527 */
528 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
529 if (sl.st.v.len != 8)
530 return 0;
531
532 if (*(sl.st.v.ptr + 4) != '/' ||
533 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
534 *(sl.st.v.ptr + 6) != '.' ||
535 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
536 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200537 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100538
539 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100540 ((*(sl.st.v.ptr + 5) > '1') ||
541 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100542 h1m->flags |= H1_MF_VER_11;
543
Christopher Faulet9768c262018-10-22 09:34:31 +0200544 return 1;
545}
546/* Remove all "Connection:" headers from the HTX message <htx> */
547static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
548{
549 struct ist hdr = {.ptr = "Connection", .len = 10};
550 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200551
Christopher Faulet9768c262018-10-22 09:34:31 +0200552 while (http_find_header(htx, hdr, &ctx, 1))
553 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200554
Christopher Faulet9768c262018-10-22 09:34:31 +0200555 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
556}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200557
Christopher Faulet9768c262018-10-22 09:34:31 +0200558/* Add a "Connection:" header with the value <value> into the HTX message
559 * <htx>.
560 */
561static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
562{
563 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200564
Christopher Faulet9768c262018-10-22 09:34:31 +0200565 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200566}
567
568/* Deduce the connection mode of the client connection, depending on the
569 * configuration and the H1 message flags. This function is called twice, the
570 * first time when the request is parsed and the second time when the response
571 * is parsed.
572 */
573static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
574{
575 struct proxy *fe = h1s->h1c->px;
576 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
577
578 /* Tunnel mode can only by set on the frontend */
579 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
580 flag = H1S_F_WANT_TUN;
581 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
582 flag = H1S_F_WANT_CLO;
583
584 /* flags order: CLO > SCL > TUN > KAL */
585 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
586 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
587
588 if (h1m->flags & H1_MF_RESP) {
589 /* Either we've established an explicit tunnel, or we're
590 * switching the protocol. In both cases, we're very unlikely to
591 * understand the next protocols. We have to switch to tunnel
592 * mode, so that we transfer the request and responses then let
593 * this protocol pass unmodified. When we later implement
594 * specific parsers for such protocols, we'll want to check the
595 * Upgrade header which contains information about that protocol
596 * for responses with status 101 (eg: see RFC2817 about TLS).
597 */
598 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
599 h1s->status == 101)
600 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100601 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
602 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200603 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
604 }
605 else {
606 if (h1s->flags & H1S_F_WANT_KAL &&
607 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
608 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
609 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
610 }
611
612 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
613 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
614 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
615}
616
617/* Deduce the connection mode of the client connection, depending on the
618 * configuration and the H1 message flags. This function is called twice, the
619 * first time when the request is parsed and the second time when the response
620 * is parsed.
621 */
622static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
623{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100624 struct h1c *h1c = h1s->h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100625 struct session *sess = h1s->sess;
Christopher Fauleta1692f52018-11-22 10:19:50 +0100626 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200627 int flag = H1S_F_WANT_KAL;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100628 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200629
630 /* Tunnel mode can only by set on the frontend */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100631 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200632 flag = H1S_F_WANT_TUN;
633
634 /* For the server connection: server-close == httpclose */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100635 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200636 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Olivier Houchardf502aca2018-12-14 19:42:40 +0100637 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200638 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
639 flag = H1S_F_WANT_CLO;
640
641 /* flags order: CLO > SCL > TUN > KAL */
642 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
643 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
644
645 if (h1m->flags & H1_MF_RESP) {
646 /* Either we've established an explicit tunnel, or we're
647 * switching the protocol. In both cases, we're very unlikely to
648 * understand the next protocols. We have to switch to tunnel
649 * mode, so that we transfer the request and responses then let
650 * this protocol pass unmodified. When we later implement
651 * specific parsers for such protocols, we'll want to check the
652 * Upgrade header which contains information about that protocol
653 * for responses with status 101 (eg: see RFC2817 about TLS).
654 */
655 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
656 h1s->status == 101)
657 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
658 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
659 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
660 else if (h1s->flags & H1S_F_WANT_KAL &&
661 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
662 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
663 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
664 }
Christopher Faulet70033782018-12-05 13:50:11 +0100665 else {
666 if (h1s->flags & H1S_F_WANT_KAL &&
667 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
668 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
669 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
670 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200671
672 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
673 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
674 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200675}
676
Christopher Faulet9768c262018-10-22 09:34:31 +0200677static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
678 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200679{
680 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200681
682 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
683 * token is found
684 */
685 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200686 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200687
688 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200689 if (h1m->flags & H1_MF_CONN_CLO) {
690 if (conn_val)
691 *conn_val = ist("");
692 if (htx)
693 h1_remove_conn_hdrs(h1m, htx);
694 }
695 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
696 if (conn_val)
697 *conn_val = ist("keep-alive");
698 if (htx)
699 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
700 }
Christopher Fauletce851492018-12-07 18:06:59 +0100701 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
702 if (conn_val)
703 *conn_val = ist("");
704 if (htx)
705 h1_remove_conn_hdrs(h1m, htx);
706 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707 }
708 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200709 if (h1m->flags & H1_MF_CONN_KAL) {
710 if (conn_val)
711 *conn_val = ist("");
712 if (htx)
713 h1_remove_conn_hdrs(h1m, htx);
714 }
715 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
716 if (conn_val)
717 *conn_val = ist("close");
718 if (htx)
719 h1_add_conn_hdr(h1m, htx, ist("close"));
720 }
Christopher Fauletce851492018-12-07 18:06:59 +0100721 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
722 if (conn_val)
723 *conn_val = ist("");
724 if (htx)
725 h1_remove_conn_hdrs(h1m, htx);
726 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200727 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200728}
729
Christopher Faulet9768c262018-10-22 09:34:31 +0200730static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
731 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200732{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200733 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
734 * token is found
735 */
736 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200737 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200738
739 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200740 if (h1m->flags & H1_MF_CONN_CLO) {
741 if (conn_val)
742 *conn_val = ist("");
743 if (htx)
744 h1_remove_conn_hdrs(h1m, htx);
745 }
746 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
747 if (conn_val)
748 *conn_val = ist("keep-alive");
749 if (htx)
750 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
751 }
Christopher Fauletce851492018-12-07 18:06:59 +0100752 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
753 if (conn_val)
754 *conn_val = ist("");
755 if (htx)
756 h1_remove_conn_hdrs(h1m, htx);
757 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200758 }
759 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200760 if (h1m->flags & H1_MF_CONN_KAL) {
761 if (conn_val)
762 *conn_val = ist("");
763 if (htx)
764 h1_remove_conn_hdrs(h1m, htx);
765 }
766 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
767 if (conn_val)
768 *conn_val = ist("close");
769 if (htx)
770 h1_add_conn_hdr(h1m, htx, ist("close"));
771 }
Christopher Fauletce851492018-12-07 18:06:59 +0100772 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
773 if (conn_val)
774 *conn_val = ist("");
775 if (htx)
776 h1_remove_conn_hdrs(h1m, htx);
777 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200778 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200779}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200780
Christopher Faulet9768c262018-10-22 09:34:31 +0200781/* Set the right connection mode and update "Connection:" header if
782 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
783 * message is updated accordingly. When <conn_val> is not NULL, it is set with
784 * the new header value.
785 */
786static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
787 struct htx *htx, struct ist *conn_val)
788{
789 if (!conn_is_back(h1s->h1c->conn)) {
790 h1_set_cli_conn_mode(h1s, h1m);
791 if (h1m->flags & H1_MF_RESP)
792 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
793 }
794 else {
795 h1_set_srv_conn_mode(h1s, h1m);
796 if (!(h1m->flags & H1_MF_RESP))
797 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
798 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200799}
800
Christopher Faulete44769b2018-11-29 23:01:45 +0100801
802/* Append the description of what is present in error snapshot <es> into <out>.
803 * The description must be small enough to always fit in a buffer. The output
804 * buffer may be the trash so the trash must not be used inside this function.
805 */
806static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
807{
808 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100809 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
810 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
811 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
812 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
813 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
814 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100815}
816/*
817 * Capture a bad request or response and archive it in the proxy's structure.
818 * By default it tries to report the error position as h1m->err_pos. However if
819 * this one is not set, it will then report h1m->next, which is the last known
820 * parsing point. The function is able to deal with wrapping buffers. It always
821 * displays buffers as a contiguous area starting at buf->p. The direction is
822 * determined thanks to the h1m's flags.
823 */
824static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
825 struct h1m *h1m, struct buffer *buf)
826{
827 struct session *sess = h1c->conn->owner;
828 struct proxy *proxy = h1c->px;
829 struct proxy *other_end = sess->fe;
830 union error_snapshot_ctx ctx;
831
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100832 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100833 other_end = si_strm(h1s->cs->data)->be;
834
835 /* http-specific part now */
836 ctx.h1.state = h1m->state;
837 ctx.h1.c_flags = h1c->flags;
838 ctx.h1.s_flags = h1s->flags;
839 ctx.h1.m_flags = h1m->flags;
840 ctx.h1.m_clen = h1m->curr_len;
841 ctx.h1.m_blen = h1m->body_len;
842
843 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
844 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100845 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
846 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100847}
848
Christopher Fauletadb22202018-12-12 10:32:09 +0100849/* Emit the chunksize followed by a CRLF in front of data of the buffer
850 * <buf>. It goes backwards and starts with the byte before the buffer's
851 * head. The caller is responsible for ensuring there is enough room left before
852 * the buffer's head for the string.
853 */
854static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
855{
856 char *beg, *end;
857
858 beg = end = b_head(buf);
859 *--beg = '\n';
860 *--beg = '\r';
861 do {
862 *--beg = hextab[chksz & 0xF];
863 } while (chksz >>= 4);
864 buf->head -= (end - beg);
865 b_add(buf, end - beg);
866}
867
868/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
869 * ensuring there is enough room left in the buffer for the string. */
870static void h1_emit_chunk_crlf(struct buffer *buf)
871{
872 *(b_peek(buf, b_data(buf))) = '\r';
873 *(b_peek(buf, b_data(buf) + 1)) = '\n';
874 b_add(buf, 2);
875}
876
Christopher Faulet129817b2018-09-20 16:14:40 +0200877/*
878 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200879 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
880 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800881 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200882 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200883static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200884 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200885{
886 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100887 union h1_sl h1sl;
888 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200889 int ret = 0;
890
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100891 if (!max)
892 goto end;
893
Christopher Faulet129817b2018-09-20 16:14:40 +0200894 /* Realing input buffer if necessary */
895 if (b_head(buf) + b_data(buf) > b_wrap(buf))
896 b_slow_realign(buf, trash.area, 0);
897
Christopher Fauletf2824e62018-10-01 12:12:37 +0200898 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100899 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200900 if (ret <= 0) {
901 /* Incomplete or invalid message. If the buffer is full, it's an
902 * error because headers are too large to be handled by the
903 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200904 if (ret < 0 || (!ret && b_full(buf)))
905 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200906 goto end;
907 }
908
909 /* messages headers fully parsed, do some checks to prepare the body
910 * parsing.
911 */
912
913 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200914 if (ret > (b_size(buf) - global.tune.maxrewrite))
915 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200916
Christopher Faulet9768c262018-10-22 09:34:31 +0200917 /* Save the request's method or the response's status, check if the body
918 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200919 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100920 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200921
Christopher Faulet129817b2018-09-20 16:14:40 +0200922 /* Request have always a known length */
923 h1m->flags |= H1_MF_XFER_LEN;
924 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
925 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200926
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100927 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
928 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200929 h1m->err_state = h1m->state;
930 goto vsn_error;
931 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200932 }
933 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100934 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200935
936 if ((h1s->meth == HTTP_METH_HEAD) ||
937 (h1s->status >= 100 && h1s->status < 200) ||
938 (h1s->status == 204) || (h1s->status == 304) ||
939 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
940 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
941 h1m->flags |= H1_MF_XFER_LEN;
942 h1m->curr_len = h1m->body_len = 0;
943 h1m->state = H1_MSG_DONE;
944 }
945 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
946 h1m->flags |= H1_MF_XFER_LEN;
947 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
948 h1m->state = H1_MSG_DONE;
949 }
950 else
951 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200952
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100953 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
954 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200955 h1m->err_state = h1m->state;
956 goto vsn_error;
957 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200958 }
959
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100960 /* Set HTX start-line flags */
961 if (h1m->flags & H1_MF_VER_11)
962 flags |= HTX_SL_F_VER_11;
963 if (h1m->flags & H1_MF_XFER_ENC)
964 flags |= HTX_SL_F_XFER_ENC;
965 if (h1m->flags & H1_MF_XFER_LEN) {
966 flags |= HTX_SL_F_XFER_LEN;
967 if (h1m->flags & H1_MF_CHNK)
968 flags |= HTX_SL_F_CHNK;
969 else if (h1m->flags & H1_MF_CLEN)
970 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100971 if (h1m->state == H1_MSG_DONE)
972 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100973 }
974
Christopher Faulet9768c262018-10-22 09:34:31 +0200975 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100976 struct htx_sl *sl;
977
978 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
979 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200980 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100981 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200982 }
983 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100984 struct htx_sl *sl;
985
986 flags |= HTX_SL_F_IS_RESP;
987 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
988 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200989 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100990 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200991 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100992
Christopher Faulet9768c262018-10-22 09:34:31 +0200993 if (h1m->state == H1_MSG_DONE)
994 if (!htx_add_endof(htx, HTX_BLK_EOM))
995 goto error;
996
997 h1_process_conn_mode(h1s, h1m, htx, NULL);
998
999 /* If body length cannot be determined, set htx->extra to
1000 * ULLONG_MAX. This value is impossible in other cases.
1001 */
1002 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
1003
1004 /* Recheck there is enough space to do headers rewritting */
1005 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1006 goto error;
1007
1008 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001009 end:
1010 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001011
1012 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001013 h1m->err_state = h1m->state;
1014 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001015 vsn_error:
1016 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001017 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001018 ret = 0;
1019 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001020}
1021
1022/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001023 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1024 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001026 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001027 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001028static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001029 struct buffer *buf, size_t *ofs, size_t max,
1030 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001031{
Christopher Faulet9768c262018-10-22 09:34:31 +02001032 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +02001033 size_t total = 0;
1034 int ret = 0;
1035
1036 if (h1m->flags & H1_MF_XFER_LEN) {
1037 if (h1m->flags & H1_MF_CLEN) {
1038 /* content-length: read only h2m->body_len */
1039 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001040 if (ret > data_space)
1041 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001042 if ((uint64_t)ret > h1m->curr_len)
1043 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001044 if (ret > b_contig_data(buf, *ofs))
1045 ret = b_contig_data(buf, *ofs);
1046 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001047 /* very often with large files we'll face the following
1048 * situation :
1049 * - htx is empty and points to <htxbuf>
1050 * - ret == buf->data
1051 * - buf->head == sizeof(struct htx)
1052 * => we can swap the buffers and place an htx header into
1053 * the target buffer instead
1054 */
1055 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1056 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1057 void *raw_area = buf->area;
1058 void *htx_area = htxbuf->area;
1059 struct htx_blk *blk;
1060
1061 buf->area = htx_area;
1062 htxbuf->area = raw_area;
1063 htx = (struct htx *)htxbuf->area;
1064 htx->size = htxbuf->size - sizeof(*htx);
1065 htx_reset(htx);
1066 b_set_data(htxbuf, b_size(htxbuf));
1067
1068 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1069 blk->info += ret;
1070 /* nothing else to do, the old buffer now contains an
1071 * empty pre-initialized HTX header
1072 */
1073 }
1074 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001075 goto end;
1076 h1m->curr_len -= ret;
1077 *ofs += ret;
1078 total += ret;
1079 }
1080
1081 if (!h1m->curr_len) {
1082 if (!htx_add_endof(htx, HTX_BLK_EOM))
1083 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001084 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001085 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001086 }
1087 else if (h1m->flags & H1_MF_CHNK) {
1088 new_chunk:
1089 /* te:chunked : parse chunks */
1090 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001091 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001092 if (ret <= 0)
1093 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001094 h1m->state = H1_MSG_CHUNK_SIZE;
1095
Christopher Faulet129817b2018-09-20 16:14:40 +02001096 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001097 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001098 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001099 }
1100
1101 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1102 unsigned int chksz;
1103
Christopher Fauletf2824e62018-10-01 12:12:37 +02001104 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001105 if (ret <= 0)
1106 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001107 if (!chksz) {
1108 if (!htx_add_endof(htx, HTX_BLK_EOD))
1109 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001110 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001111 h1m->state = H1_MSG_TRAILERS;
1112 }
1113 else
1114 h1m->state = H1_MSG_DATA;
1115
Christopher Faulet129817b2018-09-20 16:14:40 +02001116 h1m->curr_len = chksz;
1117 h1m->body_len += chksz;
1118 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001119 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001120 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001121 }
1122
1123 if (h1m->state == H1_MSG_DATA) {
1124 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001125 if (ret > data_space)
1126 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001127 if ((uint64_t)ret > h1m->curr_len)
1128 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001129 if (ret > b_contig_data(buf, *ofs))
1130 ret = b_contig_data(buf, *ofs);
1131 if (ret) {
1132 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1133 goto end;
1134 h1m->curr_len -= ret;
1135 max -= ret;
1136 *ofs += ret;
1137 total += ret;
1138 }
1139 if (!h1m->curr_len) {
1140 h1m->state = H1_MSG_CHUNK_CRLF;
1141 goto new_chunk;
1142 }
1143 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001144 }
1145
1146 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001147 /* Trailers were alread parsed, only the EOM
1148 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001149 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001150 goto skip_tlr_parsing;
1151
Christopher Fauletf2824e62018-10-01 12:12:37 +02001152 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 if (ret > data_space)
1154 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001155 if (ret <= 0)
1156 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001157
1158 /* Realing input buffer if tailers wrap. For now
1159 * this is a workaroung. Because trailers are
1160 * not split on CRLF, like headers, there is no
1161 * way to know where to split it when trailers
1162 * wrap. This is a limitation of
1163 * h1_measure_trailers.
1164 */
1165 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1166 b_slow_realign(buf, trash.area, 0);
1167
1168 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1169 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001170 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001171 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001172 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001173 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001174
Christopher Faulet17276482018-11-22 11:44:35 +01001175 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001176 if (!htx_add_endof(htx, HTX_BLK_EOM))
1177 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001178 h1m->state = H1_MSG_DONE;
1179 }
1180 }
1181 else {
1182 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1183 * is no body. Switch the message in DONE state
1184 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001185 if (!htx_add_endof(htx, HTX_BLK_EOM))
1186 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001187 h1m->state = H1_MSG_DONE;
1188 }
1189 }
1190 else {
1191 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001192 ret = max;
1193 if (ret > data_space)
1194 ret = data_space;
1195 if (ret > b_contig_data(buf, *ofs))
1196 ret = b_contig_data(buf, *ofs);
1197 if (ret) {
1198 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1199 goto end;
1200
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001201 *ofs += ret;
1202 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001203 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001204 }
1205
1206 end:
1207 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001208 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001210 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001211 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001212 return 0;
1213 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001214 /* update htx->extra, only when the body length is known */
1215 if (h1m->flags & H1_MF_XFER_LEN)
1216 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001217 return total;
1218}
1219
1220/*
1221 * Synchronize the request and the response before reseting them. Except for 1xx
1222 * responses, we wait that the request and the response are in DONE state and
1223 * that all data are forwarded for both. For 1xx responses, only the response is
1224 * reset, waiting the final one. Many 1xx messages can be sent.
1225 */
1226static void h1_sync_messages(struct h1c *h1c)
1227{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001228 struct h1s *h1s = h1c->h1s;
1229
1230 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001231 return;
1232
Christopher Fauletf2824e62018-10-01 12:12:37 +02001233 if (h1s->res.state == H1_MSG_DONE &&
1234 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001235 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001236 /* For 100-Continue response or any other informational 1xx
1237 * response which is non-final, don't reset the request, the
1238 * transaction is not finished. We take care the response was
1239 * transferred before.
1240 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001241 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001242 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001243 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet129817b2018-09-20 16:14:40 +02001244 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001245 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001246 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1247 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001248 h1m_init_req(&h1s->req);
1249 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001250 h1s->req.state = H1_MSG_TUNNEL;
1251 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001252 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001253 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001254 }
1255}
1256
1257/*
1258 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001259 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1260 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001261 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001262static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001263{
Christopher Faulet539e0292018-11-19 10:40:09 +01001264 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001265 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001266 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001267 size_t total = 0;
1268 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001269 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001270 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001271
Christopher Faulet539e0292018-11-19 10:40:09 +01001272 htx = htx_from_buf(buf);
1273 count = b_data(&h1c->ibuf);
1274 max = htx_free_space(htx);
1275 if (flags & CO_RFL_KEEP_RSV) {
1276 if (max < global.tune.maxrewrite)
1277 goto end;
1278 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001279 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001280 if (count > max)
1281 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001282
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001283 if (!count)
1284 goto end;
1285
Christopher Fauletf2824e62018-10-01 12:12:37 +02001286 if (!conn_is_back(h1c->conn)) {
1287 h1m = &h1s->req;
1288 errflag = H1S_F_REQ_ERROR;
1289 }
1290 else {
1291 h1m = &h1s->res;
1292 errflag = H1S_F_RES_ERROR;
1293 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001294
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001295 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001296 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001297 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001298 if (!ret)
1299 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 }
1301 else if (h1m->state <= H1_MSG_TRAILERS) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001302 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1303 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001304 if (!ret)
1305 break;
1306 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001307 else if (h1m->state == H1_MSG_DONE) {
1308 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001309 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001310 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001311 else if (h1m->state == H1_MSG_TUNNEL) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001312 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1313 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001314 if (!ret)
1315 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001316 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001317 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001318 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001319 break;
1320 }
1321
Christopher Faulet539e0292018-11-19 10:40:09 +01001322 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001323 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001324
Christopher Faulet47365272018-10-31 17:40:50 +01001325 if (h1s->flags & errflag)
1326 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001327
Christopher Faulet539e0292018-11-19 10:40:09 +01001328 b_del(&h1c->ibuf, total);
1329
1330 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001331 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001332
Willy Tarreau45f2b892018-12-05 07:59:27 +01001333 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001334 h1c->flags &= ~H1C_F_IN_FULL;
1335 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001336 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001337
Christopher Fauletcf56b992018-12-11 16:12:31 +01001338 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1339
1340 if (!b_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001341 h1_release_buf(h1c, &h1c->ibuf);
1342 h1_sync_messages(h1c);
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001343 }
Christopher Fauletcf56b992018-12-11 16:12:31 +01001344 else if (!htx_is_empty(htx))
1345 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001346
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001347 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1348 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet539e0292018-11-19 10:40:09 +01001349 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001350
Christopher Faulet539e0292018-11-19 10:40:09 +01001351 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001352
1353 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001354 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001355 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001356 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001357 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001358 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001359}
1360
Christopher Faulet129817b2018-09-20 16:14:40 +02001361/*
1362 * Process outgoing data. It parses data and transfer them from the channel buffer into
1363 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1364 * 0 if it couldn't proceed.
1365 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001366static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1367{
Christopher Faulet129817b2018-09-20 16:14:40 +02001368 struct h1s *h1s = h1c->h1s;
1369 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001370 struct htx *chn_htx;
1371 struct htx_blk *blk;
1372 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001373 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001374 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001375 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001376
Christopher Faulet47365272018-10-31 17:40:50 +01001377 if (!count)
1378 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001379
Christopher Faulet9768c262018-10-22 09:34:31 +02001380 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001381 if (htx_is_empty(chn_htx))
1382 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001383
Christopher Faulet51dbc942018-09-13 09:05:15 +02001384 if (!h1_get_buf(h1c, &h1c->obuf)) {
1385 h1c->flags |= H1C_F_OUT_ALLOC;
1386 goto end;
1387 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001388
Christopher Fauletf2824e62018-10-01 12:12:37 +02001389 if (!conn_is_back(h1c->conn)) {
1390 h1m = &h1s->res;
1391 errflag = H1S_F_RES_ERROR;
1392 }
1393 else {
1394 h1m = &h1s->req;
1395 errflag = H1S_F_REQ_ERROR;
1396 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001397
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001398 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001399 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001400
1401 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001402
Willy Tarreau3815b222018-12-11 19:50:43 +01001403 /* Perform some optimizations to reduce the number of buffer copies.
1404 * First, if the mux's buffer is empty and the htx area contains
1405 * exactly one data block of the same size as the requested count,
1406 * then it's possible to simply swap the caller's buffer with the
1407 * mux's output buffer and adjust offsets and length to match the
1408 * entire DATA HTX block in the middle. In this case we perform a
1409 * true zero-copy operation from end-to-end. This is the situation
1410 * that happens all the time with large files. Second, if this is not
1411 * possible, but the mux's output buffer is empty, we still have an
1412 * opportunity to avoid the copy to the intermediary buffer, by making
1413 * the intermediary buffer's area point to the output buffer's area.
1414 * In this case we want to skip the HTX header to make sure that copies
1415 * remain aligned and that this operation remains possible all the
1416 * time. This goes for headers, data blocks and any data extracted from
1417 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001418 */
1419 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001420 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001421
1422 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001423 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001424 htx_get_blk_value(chn_htx, blk).len == count) {
1425 void *old_area = h1c->obuf.area;
1426
1427 h1c->obuf.area = buf->area;
1428 h1c->obuf.data = count;
1429
1430 buf->area = old_area;
1431 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001432
1433 /* The message is chunked. We need to emit the chunk
1434 * size. We have at least the size of the struct htx to
1435 * write the chunk envelope. It should be enough.
1436 */
1437 if (h1m->flags & H1_MF_CHNK) {
1438 h1_emit_chunk_size(&h1c->obuf, count);
1439 h1_emit_chunk_crlf(&h1c->obuf);
1440 }
1441
Willy Tarreau3815b222018-12-11 19:50:43 +01001442 total += count;
1443 goto out;
1444 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001445 tmp->area = h1c->obuf.area + h1c->obuf.head;
1446 }
1447
Christopher Faulet9768c262018-10-22 09:34:31 +02001448 tmp->size = b_room(&h1c->obuf);
1449
Christopher Fauletb2e84162018-12-06 11:39:49 +01001450 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001451 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001452 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001453 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001454 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001455 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001456
Christopher Fauletb2e84162018-12-06 11:39:49 +01001457 vlen = sz;
1458 if (vlen > count) {
1459 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1460 goto copy;
1461 vlen = count;
1462 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001463
Christopher Fauletb2e84162018-12-06 11:39:49 +01001464 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001465 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001466 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001467
1468 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001469 h1m_init_req(h1m);
1470 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001471 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001472 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001473 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001474 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001475 goto copy;
1476 h1m->flags |= H1_MF_XFER_LEN;
1477 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001478 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001479
Christopher Faulet9768c262018-10-22 09:34:31 +02001480 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001481 h1m_init_res(h1m);
1482 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001483 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001484 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001485 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001486 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001487 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001488 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001489 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001490 if (sl->info.res.status < 200 &&
1491 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1492 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001493 h1m->state = H1_MSG_HDR_FIRST;
1494 break;
1495
1496 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001497 h1m->state = H1_MSG_HDR_NAME;
1498 n = htx_get_blk_name(chn_htx, blk);
1499 v = htx_get_blk_value(chn_htx, blk);
1500
1501 if (isteqi(n, ist("transfer-encoding")))
1502 h1_parse_xfer_enc_header(h1m, v);
1503 else if (isteqi(n, ist("connection"))) {
1504 h1_parse_connection_header(h1m, v);
1505 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001506 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001507 if (!v.len)
1508 goto skip_hdr;
1509 }
1510
Christopher Fauletc59ff232018-12-03 13:58:44 +01001511 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001512 goto copy;
1513 skip_hdr:
1514 h1m->state = H1_MSG_HDR_L2_LWS;
1515 break;
1516
1517 case HTX_BLK_PHDR:
1518 /* not implemented yet */
1519 h1m->flags |= errflag;
1520 break;
1521
1522 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001523 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001524 /* There is no "Connection:" header and
1525 * it the conn_mode must be
1526 * processed. So do it */
1527 n = ist("Connection");
1528 v = ist("");
1529 h1_process_conn_mode(h1s, h1m, NULL, &v);
1530 process_conn_mode = 0;
1531 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001532 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001533 goto copy;
1534 }
1535 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001536
1537 if ((h1m->flags & (H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == H1_MF_XFER_LEN) {
1538 /* chunking needed but header not seen */
1539 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1540 goto copy;
1541 h1m->flags |= H1_MF_CHNK;
1542 }
1543
Christopher Faulet9768c262018-10-22 09:34:31 +02001544 h1m->state = H1_MSG_LAST_LF;
1545 if (!chunk_memcat(tmp, "\r\n", 2))
1546 goto copy;
1547
1548 h1m->state = H1_MSG_DATA;
1549 break;
1550
1551 case HTX_BLK_DATA:
1552 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001553 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001554 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001555 goto copy;
1556 break;
1557
1558 case HTX_BLK_EOD:
1559 if (!chunk_memcat(tmp, "0\r\n", 3))
1560 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001561 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001562 h1m->state = H1_MSG_TRAILERS;
1563 break;
1564
1565 case HTX_BLK_TLR:
Willy Tarreau34d23482019-01-03 17:46:56 +01001566 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001567 if (!chunk_memcat(tmp, "0\r\n", 3))
1568 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001569 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001570 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001571 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001572 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001573 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001574 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001575 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001576 break;
1577
1578 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001579 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001580 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001581 if (!chunk_memcat(tmp, "0\r\n", 3))
1582 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001583 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001584 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001585 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001586 if (!chunk_memcat(tmp, "\r\n", 2))
1587 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001588 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001589 }
1590 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001591 h1m->state = H1_MSG_DONE;
1592 break;
1593
1594 case HTX_BLK_OOB:
1595 v = htx_get_blk_value(chn_htx, blk);
1596 if (!chunk_memcat(tmp, v.ptr, v.len))
1597 goto copy;
1598 break;
1599
1600 default:
1601 h1m->flags |= errflag;
1602 break;
1603 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001604 total += vlen;
1605 count -= vlen;
1606 if (sz == vlen)
1607 blk = htx_remove_blk(chn_htx, blk);
1608 else {
1609 htx_cut_data_blk(chn_htx, blk, vlen);
1610 break;
1611 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001612 }
1613
Christopher Faulet9768c262018-10-22 09:34:31 +02001614 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001615 /* when the output buffer is empty, tmp shares the same area so that we
1616 * only have to update pointers and lengths.
1617 */
1618 if (tmp->area == h1c->obuf.area)
1619 h1c->obuf.data = tmp->data;
1620 else
1621 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001622
Willy Tarreau3815b222018-12-11 19:50:43 +01001623 htx_to_buf(chn_htx, buf);
1624 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001625 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001626 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001627 end:
1628 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001629}
1630
Christopher Faulet51dbc942018-09-13 09:05:15 +02001631/*********************************************************/
1632/* functions below are I/O callbacks from the connection */
1633/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001634static void h1_wake_stream_for_recv(struct h1s *h1s)
1635{
1636 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001637 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001638 tasklet_wakeup(h1s->recv_wait->task);
1639 h1s->recv_wait = NULL;
1640 }
1641}
1642static void h1_wake_stream_for_send(struct h1s *h1s)
1643{
1644 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001645 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001646 tasklet_wakeup(h1s->send_wait->task);
1647 h1s->send_wait = NULL;
1648 }
1649}
1650
Christopher Faulet51dbc942018-09-13 09:05:15 +02001651/*
1652 * Attempt to read data, and subscribe if none available
1653 */
1654static int h1_recv(struct h1c *h1c)
1655{
1656 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001657 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001658 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001659 int rcvd = 0;
1660
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001661 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001662 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001663
Olivier Houchard75159a92018-12-03 18:46:09 +01001664 if (!h1_recv_allowed(h1c)) {
1665 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001666 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001667 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001668
Christopher Fauletfeb11742018-11-29 15:12:34 +01001669 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001670 rcvd = 1;
1671 goto end;
1672 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001673
Christopher Faulet51dbc942018-09-13 09:05:15 +02001674 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1675 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001676 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001677 }
1678
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001679 /*
1680 * If we only have a small amount of data, realign it,
1681 * it's probably cheaper than doing 2 recv() calls.
1682 */
1683 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1684 b_slow_realign(&h1c->ibuf, trash.area, 0);
1685
Willy Tarreau45f2b892018-12-05 07:59:27 +01001686 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001687 if (max) {
1688 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001689
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001690 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001691 if (!b_data(&h1c->ibuf)) {
1692 /* try to pre-align the buffer like the rxbufs will be
1693 * to optimize memory copies.
1694 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001695 h1c->ibuf.head = sizeof(struct htx);
1696 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001697 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001698 }
Christopher Faulet47365272018-10-31 17:40:50 +01001699 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001700 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001701 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001702 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001703 if (h1s->csinfo.t_idle == -1)
1704 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1705 }
Christopher Faulet47365272018-10-31 17:40:50 +01001706 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001707
Christopher Fauletcf56b992018-12-11 16:12:31 +01001708 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001709 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001710 goto end;
1711 }
1712
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001713 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001714
Christopher Faulet9768c262018-10-22 09:34:31 +02001715 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001716 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1717 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001718
Christopher Faulete6b39942018-12-07 09:42:49 +01001719 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Olivier Houchard6a2d3342018-12-06 17:41:26 +01001720 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001721 if (!b_data(&h1c->ibuf))
1722 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001723 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001724 h1c->flags |= H1C_F_IN_FULL;
1725 return rcvd;
1726}
1727
1728
1729/*
1730 * Try to send data if possible
1731 */
1732static int h1_send(struct h1c *h1c)
1733{
1734 struct connection *conn = h1c->conn;
1735 unsigned int flags = 0;
1736 size_t ret;
1737 int sent = 0;
1738
1739 if (conn->flags & CO_FL_ERROR)
1740 return 0;
1741
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001742 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001743 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1744 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001745 return 0;
1746 }
1747
Christopher Faulet51dbc942018-09-13 09:05:15 +02001748 if (!b_data(&h1c->obuf))
1749 goto end;
1750
1751 if (h1c->flags & H1C_F_OUT_FULL)
1752 flags |= CO_SFL_MSG_MORE;
1753
1754 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1755 if (ret > 0) {
1756 h1c->flags &= ~H1C_F_OUT_FULL;
1757 b_del(&h1c->obuf, ret);
1758 sent = 1;
1759 }
1760
Christopher Faulet145aa472018-12-06 10:56:20 +01001761 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1762 /* error or output closed, nothing to send, clear the buffer to release it */
1763 b_reset(&h1c->obuf);
1764 }
1765
Christopher Faulet51dbc942018-09-13 09:05:15 +02001766 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001767 if (!(h1c->flags & H1C_F_OUT_FULL))
1768 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001769
Christopher Faulet51dbc942018-09-13 09:05:15 +02001770 /* We're done, no more to send */
1771 if (!b_data(&h1c->obuf)) {
1772 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001773 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001774 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1775 h1_shutw_conn(conn);
1776 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001777 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1778 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001779
1780 return sent;
1781}
1782
Christopher Faulet51dbc942018-09-13 09:05:15 +02001783
1784/* callback called on any event by the connection handler.
1785 * It applies changes and returns zero, or < 0 if it wants immediate
1786 * destruction of the connection.
1787 */
1788static int h1_process(struct h1c * h1c)
1789{
1790 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001791 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001792
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001793 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001794 return -1;
1795
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001796 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001797 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1798 goto end;
1799 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001800 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001801 }
1802
Christopher Fauletfeb11742018-11-29 15:12:34 +01001803 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001804 if (h1c->flags & H1C_F_CS_ERROR ||
1805 conn->flags & CO_FL_ERROR ||
1806 conn_xprt_read0_pending(conn))
1807 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001808 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001809 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001810 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001811 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001812 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001813 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001814 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001815 }
1816
Christopher Fauletfeb11742018-11-29 15:12:34 +01001817 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1818 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1819
Olivier Houchard75159a92018-12-03 18:46:09 +01001820 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1821 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
1822 conn->flags & CO_FL_ERROR)) {
1823 int flags = 0;
1824
1825 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1826 flags |= CS_FL_ERROR;
1827 if (conn_xprt_read0_pending(conn))
Christopher Faulet5f50f5e2018-12-07 11:39:55 +01001828 flags |= CS_FL_EOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001829 h1s->cs->flags |= flags;
1830 h1s->cs->data_cb->wake(h1s->cs);
1831 }
Christopher Faulet47365272018-10-31 17:40:50 +01001832 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001833 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001834
1835 release:
1836 h1_release(conn);
1837 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001838}
1839
1840static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1841{
1842 struct h1c *h1c = ctx;
1843 int ret = 0;
1844
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001845 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001846 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001847 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001848 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001849 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001850 h1_process(h1c);
1851 return NULL;
1852}
1853
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001854static void h1_reset(struct connection *conn)
1855{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001856 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001857
1858 /* Reset the flags, and let the mux know we're waiting for a connection */
1859 h1c->flags = H1C_F_CS_WAIT_CONN;
1860}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001861
1862static int h1_wake(struct connection *conn)
1863{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001864 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001865 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001866
Christopher Faulet539e0292018-11-19 10:40:09 +01001867 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001868 ret = h1_process(h1c);
1869 if (ret == 0) {
1870 struct h1s *h1s = h1c->h1s;
1871
1872 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1873 ret = h1s->cs->data_cb->wake(h1s->cs);
1874 }
1875 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001876}
1877
Christopher Faulet51dbc942018-09-13 09:05:15 +02001878/*******************************************/
1879/* functions below are used by the streams */
1880/*******************************************/
1881/*
1882 * Attach a new stream to a connection
1883 * (Used for outgoing connections)
1884 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001885static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001886{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001887 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001888 struct conn_stream *cs = NULL;
1889 struct h1s *h1s;
1890
1891 if (h1c->flags & H1C_F_CS_ERROR)
1892 goto end;
1893
1894 cs = cs_new(h1c->conn);
1895 if (!cs)
1896 goto end;
1897
Olivier Houchardf502aca2018-12-14 19:42:40 +01001898 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001899 if (h1s == NULL)
1900 goto end;
1901
1902 return cs;
1903 end:
1904 cs_free(cs);
1905 return NULL;
1906}
1907
1908/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1909 * this mux, it's easy as we can only store a single conn_stream.
1910 */
1911static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1912{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001913 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001914 struct h1s *h1s = h1c->h1s;
1915
1916 if (h1s)
1917 return h1s->cs;
1918
1919 return NULL;
1920}
1921
1922static void h1_destroy(struct connection *conn)
1923{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001924 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001925
1926 if (!h1c->h1s)
1927 h1_release(conn);
1928}
1929
1930/*
1931 * Detach the stream from the connection and possibly release the connection.
1932 */
1933static void h1_detach(struct conn_stream *cs)
1934{
1935 struct h1s *h1s = cs->ctx;
1936 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001937 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01001938 int has_keepalive;
1939 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001940
1941 cs->ctx = NULL;
1942 if (!h1s)
1943 return;
1944
Olivier Houchardf502aca2018-12-14 19:42:40 +01001945 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001946 h1c = h1s->h1c;
1947 h1s->cs = NULL;
1948
Olivier Houchard8a786902018-12-15 16:05:40 +01001949 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
1950 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
1951 h1s_destroy(h1s);
1952
1953 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01001954 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001955 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01001956 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01001957 h1c->conn->flags |= CO_FL_PRIVATE;
1958
Olivier Houchard44d59142018-12-13 18:46:22 +01001959 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001960 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01001961 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
1962 h1c->conn->owner = NULL;
1963 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
1964 /* The server doesn't want it, let's kill the connection right away */
1965 h1c->conn->mux->destroy(h1c->conn);
1966 else
1967 tasklet_wakeup(h1c->wait_event.task);
1968 return;
1969
1970 }
Olivier Houchard44d59142018-12-13 18:46:22 +01001971 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001972 if (h1c->conn->owner == sess) {
1973 int ret = session_check_idle_conn(sess, h1c->conn);
1974 if (ret == -1)
1975 /* The connection got destroyed, let's leave */
1976 return;
1977 else if (ret == 1) {
1978 /* The connection was added to the server list,
1979 * wake the task so we can subscribe to events
1980 */
1981 tasklet_wakeup(h1c->wait_event.task);
1982 return;
1983 }
1984 }
Christopher Faulet9400a392018-11-23 23:10:39 +01001985 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01001986 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001987 struct server *srv = objt_server(h1c->conn->target);
1988
1989 if (srv) {
1990 if (h1c->conn->flags & CO_FL_PRIVATE)
1991 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01001992 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01001993 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1994 else
1995 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1996 }
1997 }
1998 }
1999
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000 /* We don't want to close right now unless the connection is in error */
2001 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002002 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002003 h1_release(h1c->conn);
2004 else
2005 tasklet_wakeup(h1c->wait_event.task);
2006}
2007
2008
2009static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2010{
2011 struct h1s *h1s = cs->ctx;
2012
2013 if (!h1s)
2014 return;
2015
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002016 if ((h1s->flags & H1S_F_WANT_KAL) &&
2017 !(cs->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002018 return;
2019
Christopher Faulet51dbc942018-09-13 09:05:15 +02002020 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2021 if (cs->flags & CS_FL_SHR)
2022 return;
2023 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2024 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
2025 if (cs->flags & CS_FL_SHW) {
2026 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2027 conn_full_close(cs->conn);
2028 }
2029}
2030
2031static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2032{
2033 struct h1s *h1s = cs->ctx;
2034 struct h1c *h1c;
2035
2036 if (!h1s)
2037 return;
2038 h1c = h1s->h1c;
2039
Christopher Fauletf2824e62018-10-01 12:12:37 +02002040 if ((h1s->flags & H1S_F_WANT_KAL) &&
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002041 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) &&
2042
Christopher Fauletf2824e62018-10-01 12:12:37 +02002043 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2044 return;
2045
Christopher Faulet51dbc942018-09-13 09:05:15 +02002046 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2047 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2048 return;
2049
2050 h1_shutw_conn(cs->conn);
2051}
2052
2053static void h1_shutw_conn(struct connection *conn)
2054{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002055 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002056
2057 if (conn_xprt_ready(conn) && conn->xprt->shutw)
2058 conn->xprt->shutw(conn, 1);
2059 if (!(conn->flags & CO_FL_SOCK_RD_SH))
2060 conn_sock_shutw(conn, 1);
2061 else {
2062 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2063 conn_full_close(conn);
2064 }
2065}
2066
2067/* Called from the upper layer, to unsubscribe to events */
2068static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2069{
2070 struct wait_event *sw;
2071 struct h1s *h1s = cs->ctx;
2072
2073 if (!h1s)
2074 return 0;
2075
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002076 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002077 sw = param;
2078 if (h1s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002079 sw->events &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002080 h1s->recv_wait = NULL;
2081 }
2082 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002083 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002084 sw = param;
2085 if (h1s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002086 sw->events &= ~SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002087 h1s->send_wait = NULL;
2088 }
2089 }
2090 return 0;
2091}
2092
2093/* Called from the upper layer, to subscribe to events, such as being able to send */
2094static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2095{
2096 struct wait_event *sw;
2097 struct h1s *h1s = cs->ctx;
2098
2099 if (!h1s)
2100 return -1;
2101
2102 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002103 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002104 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002105 if (!(sw->events & SUB_RETRY_RECV)) {
2106 sw->events |= SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002107 sw->handle = h1s;
2108 h1s->recv_wait = sw;
2109 }
2110 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002111 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002112 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002113 if (!(sw->events & SUB_RETRY_SEND)) {
2114 sw->events |= SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002115 sw->handle = h1s;
2116 h1s->send_wait = sw;
2117 }
2118 return 0;
2119 default:
2120 break;
2121 }
2122 return -1;
2123}
2124
2125/* Called from the upper layer, to receive data */
2126static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2127{
2128 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002129 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002130 size_t ret = 0;
2131
Christopher Faulet539e0292018-11-19 10:40:09 +01002132 if (!(h1c->flags & H1C_F_IN_ALLOC))
2133 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002134
2135 if (flags & CO_RFL_BUF_FLUSH)
2136 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002137 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2138 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002139 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002140 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002141 }
2142 return ret;
2143}
2144
2145
2146/* Called from the upper layer, to send data */
2147static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2148{
2149 struct h1s *h1s = cs->ctx;
2150 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002151 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002152
2153 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002154 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002155
2156 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002157 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2158 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002160 while (total != count) {
2161 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002162
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002163 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2164 ret = h1_process_output(h1c, buf, count);
2165 if (!ret)
2166 break;
2167 total += ret;
2168 if (!h1_send(h1c))
2169 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002170 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002171
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002172 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002173}
2174
Christopher Faulet1be55f92018-10-02 15:59:23 +02002175#if defined(CONFIG_HAP_LINUX_SPLICE)
2176/* Send and get, using splicing */
2177static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2178{
2179 struct h1s *h1s = cs->ctx;
2180 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2181 int ret = 0;
2182
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002183 if (b_data(&h1s->h1c->ibuf)) {
2184 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002185 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002186 }
2187
2188 h1s->flags &= ~H1S_F_BUF_FLUSH;
2189 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002190 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2191 count = h1m->curr_len;
2192 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2193 if (h1m->state == H1_MSG_DATA && ret > 0)
2194 h1m->curr_len -= ret;
2195 end:
2196 return ret;
2197
2198}
2199
2200static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2201{
2202 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002203 int ret = 0;
2204
2205 if (b_data(&h1s->h1c->obuf))
2206 goto end;
2207
2208 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002209 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002210 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002211 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
2212 cs->conn->xprt->subscribe(cs->conn, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002213 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002214 return ret;
2215}
2216#endif
2217
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002218/* for debugging with CLI's "show fd" command */
2219static void h1_show_fd(struct buffer *msg, struct connection *conn)
2220{
2221 struct h1c *h1c = conn->ctx;
2222 struct h1s *h1s = h1c->h1s;
2223
2224 chunk_appendf(msg, " h1c.flg=0x%x .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2225 h1c->flags,
2226 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2227 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2228 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2229 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2230
2231 if (h1s) {
2232 char *method;
2233
2234 if (h1s->meth < HTTP_METH_OTHER)
2235 method = http_known_methods[h1s->meth].ptr;
2236 else
2237 method = "UNKNOWN";
2238 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2239 " .meth=%s status=%d",
2240 h1s, h1s->flags,
2241 h1m_state_str(h1s->req.state),
2242 h1m_state_str(h1s->res.state), method, h1s->status);
2243 if (h1s->cs)
2244 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2245 h1s->cs->flags, h1s->cs->data);
2246 }
2247}
2248
Christopher Faulet51dbc942018-09-13 09:05:15 +02002249/****************************************/
2250/* MUX initialization and instanciation */
2251/****************************************/
2252
2253/* The mux operations */
2254const struct mux_ops mux_h1_ops = {
2255 .init = h1_init,
2256 .wake = h1_wake,
2257 .attach = h1_attach,
2258 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002259 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002260 .detach = h1_detach,
2261 .destroy = h1_destroy,
2262 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01002263 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002264 .rcv_buf = h1_rcv_buf,
2265 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002266#if defined(CONFIG_HAP_LINUX_SPLICE)
2267 .rcv_pipe = h1_rcv_pipe,
2268 .snd_pipe = h1_snd_pipe,
2269#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002270 .subscribe = h1_subscribe,
2271 .unsubscribe = h1_unsubscribe,
2272 .shutr = h1_shutr,
2273 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002274 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002275 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002276 .flags = MX_FL_NONE,
2277 .name = "h1",
2278};
2279
2280
2281/* this mux registers default HTX proto */
2282static struct mux_proto_list mux_proto_htx =
2283{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2284
Willy Tarreau0108d902018-11-25 19:14:37 +01002285INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2286
Christopher Faulet51dbc942018-09-13 09:05:15 +02002287/*
2288 * Local variables:
2289 * c-indent-level: 8
2290 * c-basic-offset: 8
2291 * End:
2292 */